-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the possibility to show the default values for parameters #135
Changes from all commits
9a768d7
d921670
ab56caa
0b94605
130e71c
6717f34
9a65ac3
5b5cd21
1b616d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,14 +86,15 @@ private List<MagikIssue> runCheckOnFile(final MagikFile magikFile, final MagikCh | |
} | ||
|
||
/** | ||
* Show checks active and inactive checks. | ||
* Show enabled and disabled checks. | ||
* | ||
* @param writer Writer Write to write output to. | ||
* @param showDisabled boolean Boolean to show disabled checks or not. | ||
* @param showDefaultValues boolean Boolean to show default values for parameters or not. | ||
* @throws ReflectiveOperationException - | ||
* @throws IOException - | ||
*/ | ||
void showChecks(final Writer writer, final boolean showDisabled) | ||
void showChecks(final Writer writer, final boolean showDisabled, boolean showDefaultValues) | ||
throws ReflectiveOperationException, IOException { | ||
final MagikChecksConfiguration checksConfig = | ||
new MagikChecksConfiguration(CheckList.getChecks(), this.properties); | ||
|
@@ -106,42 +107,68 @@ void showChecks(final Writer writer, final boolean showDisabled) | |
continue; | ||
} | ||
|
||
for (final MagikCheckHolder.Parameter parameter : holder.getParameters()) { | ||
writer.write( | ||
"\t" | ||
+ parameter.getName() | ||
+ ":\t" | ||
+ parameter.getValue() | ||
+ " " | ||
+ "(" | ||
+ parameter.getDescription() | ||
+ ")\n"); | ||
this.showParameters(writer, holder, showDefaultValues); | ||
} | ||
} | ||
|
||
/** | ||
* Show parameters for a check. | ||
* | ||
* @param writer Writer Write to write output to. | ||
* @param showDefaultValues boolean Boolean to show default values for parameters or not. | ||
* @throws ReflectiveOperationException - | ||
* @throws IOException - | ||
*/ | ||
private void showParameters( | ||
final Writer writer, final MagikCheckHolder holder, final boolean showDefaultValues) | ||
throws ReflectiveOperationException, IOException { | ||
for (final MagikCheckHolder.Parameter parameter : holder.getParameters()) { | ||
if (!parameter.isExplicitlySet() && !showDefaultValues) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if no value is set, and the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was the behaviour before this PR as well. Only user-changed parameters were shown. I kept that behaviour, but if a user uses |
||
continue; | ||
} | ||
final String defaultIndicator = !parameter.isExplicitlySet() ? " (default)" : ""; | ||
writer.write( | ||
" ".repeat(2) | ||
+ "*" | ||
+ defaultIndicator | ||
+ " " | ||
+ parameter.getName() | ||
+ ":" | ||
+ " " | ||
+ parameter.getValue() | ||
+ " " | ||
+ "(" | ||
+ parameter.getDescription() | ||
+ ")\n"); | ||
} | ||
} | ||
|
||
/** | ||
* Show enabled checks. | ||
* | ||
* @param writer Writer Write to write output to. | ||
* @param showDefaultValues boolean Boolean to show default values for parameters or not. | ||
* @throws ReflectiveOperationException - | ||
* @throws IOException - | ||
*/ | ||
void showEnabledChecks(final Writer writer) throws ReflectiveOperationException, IOException { | ||
void showEnabledChecks(final Writer writer, final Boolean showDefaultValues) | ||
throws ReflectiveOperationException, IOException { | ||
writer.write("Enabled checks:\n"); | ||
this.showChecks(writer, false); | ||
this.showChecks(writer, false, showDefaultValues); | ||
} | ||
|
||
/** | ||
* Show disabled checks. | ||
* | ||
* @param writer Writer Write to write output to. | ||
* @param showDefaultValues boolean Boolean to show default values for parameters or not. | ||
* @throws ReflectiveOperationException - | ||
* @throws IOException - | ||
*/ | ||
void showDisabledChecks(final Writer writer) throws ReflectiveOperationException, IOException { | ||
void showDisabledChecks(final Writer writer, final boolean showDefaultValues) | ||
throws ReflectiveOperationException, IOException { | ||
writer.write("Disabled checks:\n"); | ||
this.showChecks(writer, true); | ||
this.showChecks(writer, true, showDefaultValues); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,11 @@ public final class Main { | |
.build(); | ||
private static final Option OPTION_SHOW_CHECKS = | ||
Option.builder().longOpt("show-checks").desc("Show checks and exit").build(); | ||
private static final Option OPTION_WITH_DEFAULT_VALUES = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better name would be |
||
Option.builder() | ||
.longOpt("with-default-values") | ||
.desc("Show default values when showing checks") | ||
.build(); | ||
private static final Option OPTION_COLUMN_OFFSET = | ||
Option.builder() | ||
.longOpt("column-offset") | ||
|
@@ -64,26 +69,28 @@ public final class Main { | |
.hasArg() | ||
.type(PatternOptionBuilder.NUMBER_VALUE) | ||
.build(); | ||
private static final Option OPTION_APPLY_FIXES = | ||
Option.builder().longOpt("apply-fixes").desc("Apply fixes automatically").build(); | ||
|
||
private static final Option OPTION_DEBUG = | ||
Option.builder().longOpt("debug").desc("Enable showing of debug information").build(); | ||
private static final Option OPTION_VERSION = | ||
Option.builder().longOpt("version").desc("Show version and exit").build(); | ||
private static final Option OPTION_HELP = | ||
Option.builder().longOpt("help").desc("Show this help and exit").build(); | ||
private static final Option OPTION_APPLY_FIXES = | ||
Option.builder().longOpt("apply-fixes").desc("Apply fixes automatically").build(); | ||
|
||
static { | ||
OPTIONS = new Options(); | ||
OPTIONS.addOption(OPTION_HELP); | ||
OPTIONS.addOption(OPTION_MSG_TEMPLATE); | ||
OPTIONS.addOption(OPTION_RCFILE); | ||
OPTIONS.addOption(OPTION_SHOW_CHECKS); | ||
OPTIONS.addOption(OPTION_WITH_DEFAULT_VALUES); | ||
OPTIONS.addOption(OPTION_COLUMN_OFFSET); | ||
OPTIONS.addOption(OPTION_MAX_INFRACTIONS); | ||
OPTIONS.addOption(OPTION_APPLY_FIXES); | ||
OPTIONS.addOption(OPTION_DEBUG); | ||
OPTIONS.addOption(OPTION_VERSION); | ||
OPTIONS.addOption(OPTION_APPLY_FIXES); | ||
} | ||
|
||
private static final Map<String, Integer> SEVERITY_EXIT_CODE_MAPPING = | ||
|
@@ -175,6 +182,13 @@ public static void main(final String[] args) | |
Main.initDebugLogger(); | ||
} | ||
|
||
if (commandLine.hasOption(OPTION_WITH_DEFAULT_VALUES) | ||
&& !commandLine.hasOption(OPTION_SHOW_CHECKS)) { | ||
final PrintStream errStream = Main.getErrStream(); | ||
errStream.println("--with-default-values can only be used with --show-checks"); | ||
System.exit(1); | ||
} | ||
|
||
if (commandLine.hasOption(OPTION_VERSION)) { | ||
final String version = Main.class.getPackage().getImplementationVersion(); | ||
final PrintStream errStream = Main.getErrStream(); | ||
|
@@ -183,24 +197,7 @@ public static void main(final String[] args) | |
} | ||
|
||
// Read configuration. | ||
final MagikToolsProperties properties; | ||
if (commandLine.hasOption(OPTION_RCFILE)) { | ||
final File rcfile = (File) commandLine.getParsedOptionValue(OPTION_RCFILE); | ||
final Path path = rcfile.toPath(); | ||
if (!Files.exists(path)) { | ||
final PrintStream errStream = Main.getErrStream(); | ||
errStream.println("RC File does not exist: " + path); | ||
|
||
System.exit(1); | ||
} | ||
properties = new MagikToolsProperties(path); | ||
} else { | ||
final Path currentWorkingPath = Path.of("."); | ||
final Path path = ConfigurationLocator.locateConfiguration(currentWorkingPath); | ||
properties = | ||
path != null ? new MagikToolsProperties(path) : MagikToolsProperties.DEFAULT_PROPERTIES; | ||
} | ||
|
||
final MagikToolsProperties properties = Main.loadProperties(commandLine); | ||
// Copy configuration from command line. | ||
Main.copyOptionsToConfig(commandLine, properties); | ||
|
||
|
@@ -210,8 +207,9 @@ public static void main(final String[] args) | |
final MagikLint lint = new MagikLint(properties, reporter); | ||
final PrintStream outStream = Main.getOutStream(); | ||
final Writer writer = new PrintWriter(outStream); | ||
lint.showEnabledChecks(writer); | ||
lint.showDisabledChecks(writer); | ||
final boolean showDefaultValues = commandLine.hasOption(OPTION_WITH_DEFAULT_VALUES); | ||
lint.showEnabledChecks(writer, showDefaultValues); | ||
lint.showDisabledChecks(writer, showDefaultValues); | ||
writer.flush(); | ||
System.exit(0); | ||
} | ||
|
@@ -246,6 +244,27 @@ public static void main(final String[] args) | |
System.exit(exitCode); | ||
} | ||
|
||
private static MagikToolsProperties loadProperties(final CommandLine commandLine) | ||
throws ParseException, IOException { | ||
if (commandLine.hasOption(OPTION_RCFILE)) { | ||
final File rcfile = (File) commandLine.getParsedOptionValue(OPTION_RCFILE); | ||
final Path path = rcfile.toPath(); | ||
if (!Files.exists(path)) { | ||
final PrintStream errStream = Main.getErrStream(); | ||
errStream.println("RC File does not exist: " + path); | ||
|
||
System.exit(1); | ||
} | ||
return new MagikToolsProperties(path); | ||
} else { | ||
final Path currentWorkingPath = Path.of("."); | ||
final Path path = ConfigurationLocator.locateConfiguration(currentWorkingPath); | ||
return path != null | ||
? new MagikToolsProperties(path) | ||
: MagikToolsProperties.DEFAULT_PROPERTIES; | ||
} | ||
} | ||
|
||
private static void copyOptionsToConfig( | ||
final CommandLine commandLine, final MagikToolsProperties properties) { | ||
if (commandLine.hasOption(OPTION_MAX_INFRACTIONS)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to state that it is a boolean. Java includes types for their parameters etc. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was copied from the method above (showChecks), but I probably introduced that as well.