This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-cli.git
commit 15e12379bea9bb9ce112fcfbab0a0a9c8899845a Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Oct 18 17:32:00 2021 -0400 Sort some members. --- .../java/org/apache/commons/cli/DefaultParser.java | 224 ++++++++++----------- .../org/apache/commons/cli/BasicParserTest.java | 24 +-- .../org/apache/commons/cli/DefaultParserTest.java | 80 ++++---- .../org/apache/commons/cli/ParserTestCase.java | 72 +++---- 4 files changed, 200 insertions(+), 200 deletions(-) diff --git a/src/main/java/org/apache/commons/cli/DefaultParser.java b/src/main/java/org/apache/commons/cli/DefaultParser.java index d5e11f6..81dbc20 100644 --- a/src/main/java/org/apache/commons/cli/DefaultParser.java +++ b/src/main/java/org/apache/commons/cli/DefaultParser.java @@ -29,6 +29,106 @@ import java.util.Properties; */ public class DefaultParser implements CommandLineParser { + /** + * A nested builder class to create {@code DefaultParser} instances + * using descriptive methods. + * + * Example usage: + * <pre> + * DefaultParser parser = Option.builder() + * .setAllowPartialMatching(false) + * .setStripLeadingAndTrailingQuotes(false) + * .build(); + * </pre> + * + * @since 1.5 + */ + public static final class Builder { + + /** Flag indicating if partial matching of long options is supported. */ + private boolean allowPartialMatching = true; + + /** Flag indicating if balanced leading and trailing double quotes should be stripped from option arguments. */ + private Boolean stripLeadingAndTrailingQuotes; + + /** + * Constructs a new {@code Builder} for a {@code DefaultParser} instance. + * + * Both allowPartialMatching and stripLeadingAndTrailingQuotes are true by default, + * mimicking the argument-less constructor. + */ + private Builder() { + } + + /** + * Builds an DefaultParser with the values declared by this {@link Builder}. + * + * @return the new {@link DefaultParser} + * @since 1.5 + */ + public DefaultParser build() { + return new DefaultParser(allowPartialMatching, stripLeadingAndTrailingQuotes); + } + + /** + * Sets if partial matching of long options is supported. + * + * By "partial matching" we mean that given the following code: + * + * <pre> + * { + * @code + * final Options options = new Options(); + * options.addOption(new Option("d", "debug", false, "Turn on debug.")); + * options.addOption(new Option("e", "extract", false, "Turn on extract.")); + * options.addOption(new Option("o", "option", true, "Turn on option with argument.")); + * } + * </pre> + * + * If "partial matching" is turned on, {@code -de} only matches the {@code "debug"} option. However, with + * "partial matching" disabled, {@code -de} would enable both {@code debug} as well as {@code extract} + * + * @param allowPartialMatching whether to allow partial matching of long options + * @return this builder, to allow method chaining + * @since 1.5 + */ + public Builder setAllowPartialMatching(final boolean allowPartialMatching) { + this.allowPartialMatching = allowPartialMatching; + return this; + } + + /** + * Sets if balanced leading and trailing double quotes should be stripped from option arguments. + * + * If "stripping of balanced leading and trailing double quotes from option arguments" is true, + * the outermost balanced double quotes of option arguments values will be removed. + * For example, {@code -o '"x"'} getValue() will return {@code x}, instead of {@code "x"} + * + * If "stripping of balanced leading and trailing double quotes from option arguments" is null, + * then quotes will be stripped from option values separated by space from the option, but + * kept in other cases, which is the historic behaviour. + * + * @param stripLeadingAndTrailingQuotes whether balanced leading and trailing double quotes should be stripped from option arguments. + * @return this builder, to allow method chaining + * @since 1.5 + */ + public Builder setStripLeadingAndTrailingQuotes(final Boolean stripLeadingAndTrailingQuotes) { + this.stripLeadingAndTrailingQuotes = stripLeadingAndTrailingQuotes; + return this; + } + } + + /** + * Creates a new {@link Builder} to create an {@link DefaultParser} using descriptive + * methods. + * + * @return a new {@link Builder} instance + * @since 1.5 + */ + public static Builder builder() { + return new Builder(); + } + /** The command-line instance. */ protected CommandLine cmd; @@ -625,28 +725,6 @@ public class DefaultParser implements CommandLineParser { } /** - * Removes the option or its group from the list of expected elements. - * - * @param option - */ - private void updateRequiredOptions(final Option option) throws AlreadySelectedException { - if (option.isRequired()) { - expectedOpts.remove(option.getKey()); - } - - // if the option is in an OptionGroup make that option the selected option of the group - if (options.getOptionGroup(option) != null) { - final OptionGroup group = options.getOptionGroup(option); - - if (group.isRequired()) { - expectedOpts.remove(group); - } - - group.setSelected(option); - } - } - - /** * Strips balanced leading and trailing quotes if the stripLeadingAndTrailingQuotes is set * If stripLeadingAndTrailingQuotes is null, then do not strip * @@ -675,102 +753,24 @@ public class DefaultParser implements CommandLineParser { } /** - * Creates a new {@link Builder} to create an {@link DefaultParser} using descriptive - * methods. - * - * @return a new {@link Builder} instance - * @since 1.5 - */ - public static Builder builder() { - return new Builder(); - } - - /** - * A nested builder class to create {@code DefaultParser} instances - * using descriptive methods. - * - * Example usage: - * <pre> - * DefaultParser parser = Option.builder() - * .setAllowPartialMatching(false) - * .setStripLeadingAndTrailingQuotes(false) - * .build(); - * </pre> + * Removes the option or its group from the list of expected elements. * - * @since 1.5 + * @param option */ - public static final class Builder { - - /** Flag indicating if partial matching of long options is supported. */ - private boolean allowPartialMatching = true; - - /** Flag indicating if balanced leading and trailing double quotes should be stripped from option arguments. */ - private Boolean stripLeadingAndTrailingQuotes; - - /** - * Constructs a new {@code Builder} for a {@code DefaultParser} instance. - * - * Both allowPartialMatching and stripLeadingAndTrailingQuotes are true by default, - * mimicking the argument-less constructor. - */ - private Builder() { + private void updateRequiredOptions(final Option option) throws AlreadySelectedException { + if (option.isRequired()) { + expectedOpts.remove(option.getKey()); } - /** - * Sets if partial matching of long options is supported. - * - * By "partial matching" we mean that given the following code: - * - * <pre> - * { - * @code - * final Options options = new Options(); - * options.addOption(new Option("d", "debug", false, "Turn on debug.")); - * options.addOption(new Option("e", "extract", false, "Turn on extract.")); - * options.addOption(new Option("o", "option", true, "Turn on option with argument.")); - * } - * </pre> - * - * If "partial matching" is turned on, {@code -de} only matches the {@code "debug"} option. However, with - * "partial matching" disabled, {@code -de} would enable both {@code debug} as well as {@code extract} - * - * @param allowPartialMatching whether to allow partial matching of long options - * @return this builder, to allow method chaining - * @since 1.5 - */ - public Builder setAllowPartialMatching(final boolean allowPartialMatching) { - this.allowPartialMatching = allowPartialMatching; - return this; - } + // if the option is in an OptionGroup make that option the selected option of the group + if (options.getOptionGroup(option) != null) { + final OptionGroup group = options.getOptionGroup(option); - /** - * Sets if balanced leading and trailing double quotes should be stripped from option arguments. - * - * If "stripping of balanced leading and trailing double quotes from option arguments" is true, - * the outermost balanced double quotes of option arguments values will be removed. - * For example, {@code -o '"x"'} getValue() will return {@code x}, instead of {@code "x"} - * - * If "stripping of balanced leading and trailing double quotes from option arguments" is null, - * then quotes will be stripped from option values separated by space from the option, but - * kept in other cases, which is the historic behaviour. - * - * @param stripLeadingAndTrailingQuotes whether balanced leading and trailing double quotes should be stripped from option arguments. - * @return this builder, to allow method chaining - * @since 1.5 - */ - public Builder setStripLeadingAndTrailingQuotes(final Boolean stripLeadingAndTrailingQuotes) { - this.stripLeadingAndTrailingQuotes = stripLeadingAndTrailingQuotes; - return this; - } + if (group.isRequired()) { + expectedOpts.remove(group); + } - /** - * Builds an DefaultParser with the values declared by this {@link Builder}. - * - * @return the new {@link DefaultParser} - * @since 1.5 - */ - public DefaultParser build() { - return new DefaultParser(allowPartialMatching, stripLeadingAndTrailingQuotes); + group.setSelected(option); } } } diff --git a/src/test/java/org/apache/commons/cli/BasicParserTest.java b/src/test/java/org/apache/commons/cli/BasicParserTest.java index 2526e3f..fe7a823 100644 --- a/src/test/java/org/apache/commons/cli/BasicParserTest.java +++ b/src/test/java/org/apache/commons/cli/BasicParserTest.java @@ -75,6 +75,12 @@ public class BasicParserTest extends ParserTestCase { @Override @Test @Ignore("not supported by the BasicParser") + public void testLongOptionWithEqualsQuoteHandling() throws Exception { + } + + @Override + @Test + @Ignore("not supported by the BasicParser") public void testLongWithEqualDoubleDash() throws Exception { } @@ -123,6 +129,12 @@ public class BasicParserTest extends ParserTestCase { @Override @Test @Ignore("not supported by the BasicParser") + public void testShortOptionConcatenatedQuoteHandling() throws Exception { + } + + @Override + @Test + @Ignore("not supported by the BasicParser") public void testShortWithEqual() throws Exception { } @@ -173,16 +185,4 @@ public class BasicParserTest extends ParserTestCase { @Ignore("not supported by the BasicParser") public void testUnrecognizedOptionWithBursting() throws Exception { } - - @Override - @Test - @Ignore("not supported by the BasicParser") - public void testShortOptionConcatenatedQuoteHandling() throws Exception { - } - - @Override - @Test - @Ignore("not supported by the BasicParser") - public void testLongOptionWithEqualsQuoteHandling() throws Exception { - } } diff --git a/src/test/java/org/apache/commons/cli/DefaultParserTest.java b/src/test/java/org/apache/commons/cli/DefaultParserTest.java index a161cd6..afba651 100644 --- a/src/test/java/org/apache/commons/cli/DefaultParserTest.java +++ b/src/test/java/org/apache/commons/cli/DefaultParserTest.java @@ -31,93 +31,93 @@ public class DefaultParserTest extends ParserTestCase { parser = new DefaultParser(); } - @Override @Test - public void testShortOptionConcatenatedQuoteHandling() throws Exception { - final String[] args = {"-b\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - //This is behaviour is not consistent with the other parsers, but is required for backwards compatibility - assertEquals("Confirm -b\"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); + public void testBuilder() throws Exception { + parser = DefaultParser.builder() + .setStripLeadingAndTrailingQuotes(false) + .setAllowPartialMatching(false) + .build(); + assertEquals(DefaultParser.class, parser.getClass()); } - @Override @Test - public void testLongOptionWithEqualsQuoteHandling() throws Exception { - final String[] args = {"--bfile=\"quoted string\""}; + public void testLongOptionQuoteHandlingWithoutStrip() throws Exception { + parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build(); + final String[] args = {"--bfile", "\"quoted string\""}; final CommandLine cl = parser.parse(options, args); - assertEquals("Confirm --bfile=\"arg\" strips quotes", "\"quoted string\"", cl.getOptionValue("b")); + assertEquals("Confirm --bfile \"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); } @Test - public void testShortOptionQuoteHandlingWithStrip() throws Exception { + public void testLongOptionQuoteHandlingWithStrip() throws Exception { parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build(); - final String[] args = {"-b", "\"quoted string\""}; + final String[] args = {"--bfile", "\"quoted string\""}; final CommandLine cl = parser.parse(options, args); - assertEquals("Confirm -b \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); + assertEquals("Confirm --bfile \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); } + @Override @Test - public void testShortOptionQuoteHandlingWithoutStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build(); - final String[] args = {"-b", "\"quoted string\""}; + public void testLongOptionWithEqualsQuoteHandling() throws Exception { + final String[] args = {"--bfile=\"quoted string\""}; final CommandLine cl = parser.parse(options, args); - assertEquals("Confirm -b \"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); + assertEquals("Confirm --bfile=\"arg\" strips quotes", "\"quoted string\"", cl.getOptionValue("b")); } @Test - public void testLongOptionQuoteHandlingWithStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build(); - final String[] args = {"--bfile", "\"quoted string\""}; + public void testLongOptionWithEqualsQuoteHandlingWithoutStrip() throws Exception { + parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build(); + final String[] args = {"--bfile=\"quoted string\""}; final CommandLine cl = parser.parse(options, args); - assertEquals("Confirm --bfile \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); + assertEquals("Confirm --bfile=\"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); } @Test - public void testLongOptionQuoteHandlingWithoutStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build(); - final String[] args = {"--bfile", "\"quoted string\""}; + public void testLongOptionWithEqualsQuoteHandlingWithStrip() throws Exception { + parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build(); + final String[] args = {"--bfile=\"quoted string\""}; final CommandLine cl = parser.parse(options, args); - assertEquals("Confirm --bfile \"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); + assertEquals("Confirm --bfile=\"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); } + @Override @Test - public void testLongOptionWithEqualsQuoteHandlingWithStrip() throws Exception { - parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build(); - final String[] args = {"--bfile=\"quoted string\""}; + public void testShortOptionConcatenatedQuoteHandling() throws Exception { + final String[] args = {"-b\"quoted string\""}; final CommandLine cl = parser.parse(options, args); - assertEquals("Confirm --bfile=\"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); + //This is behaviour is not consistent with the other parsers, but is required for backwards compatibility + assertEquals("Confirm -b\"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); } @Test - public void testLongOptionWithEqualsQuoteHandlingWithoutStrip() throws Exception { + public void testShortOptionQuoteHandlingWithoutStrip() throws Exception { parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build(); - final String[] args = {"--bfile=\"quoted string\""}; + final String[] args = {"-b", "\"quoted string\""}; final CommandLine cl = parser.parse(options, args); - assertEquals("Confirm --bfile=\"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); + assertEquals("Confirm -b \"arg\" keeps quotes", "\"quoted string\"", cl.getOptionValue("b")); } @Test - public void testBuilder() throws Exception { - parser = DefaultParser.builder() - .setStripLeadingAndTrailingQuotes(false) - .setAllowPartialMatching(false) - .build(); - assertEquals(DefaultParser.class, parser.getClass()); + public void testShortOptionQuoteHandlingWithStrip() throws Exception { + parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build(); + final String[] args = {"-b", "\"quoted string\""}; + + final CommandLine cl = parser.parse(options, args); + + assertEquals("Confirm -b \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); } } diff --git a/src/test/java/org/apache/commons/cli/ParserTestCase.java b/src/test/java/org/apache/commons/cli/ParserTestCase.java index fbe0471..5a6e5a0 100644 --- a/src/test/java/org/apache/commons/cli/ParserTestCase.java +++ b/src/test/java/org/apache/commons/cli/ParserTestCase.java @@ -211,6 +211,24 @@ public abstract class ParserTestCase { } @Test + public void testLongOptionQuoteHandling() throws Exception { + final String[] args = {"--bfile", "\"quoted string\""}; + + final CommandLine cl = parser.parse(options, args); + + assertEquals("Confirm --bfile \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); + } + + @Test + public void testLongOptionWithEqualsQuoteHandling() throws Exception { + final String[] args = {"--bfile=\"quoted string\""}; + + final CommandLine cl = parser.parse(options, args); + + assertEquals("Confirm --bfile=\"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); + } + + @Test public void testLongWithEqualDoubleDash() throws Exception { final String[] args = {"--foo=bar"}; @@ -714,6 +732,24 @@ public abstract class ParserTestCase { } @Test + public void testShortOptionConcatenatedQuoteHandling() throws Exception { + final String[] args = {"-b\"quoted string\""}; + + final CommandLine cl = parser.parse(options, args); + + assertEquals("Confirm -b\"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); + } + + @Test + public void testShortOptionQuoteHandling() throws Exception { + final String[] args = {"-b", "\"quoted string\""}; + + final CommandLine cl = parser.parse(options, args); + + assertEquals("Confirm -b \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); + } + + @Test public void testShortWithEqual() throws Exception { final String[] args = {"-f=bar"}; @@ -956,42 +992,6 @@ public abstract class ParserTestCase { } @Test - public void testShortOptionQuoteHandling() throws Exception { - final String[] args = {"-b", "\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("Confirm -b \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); - } - - @Test - public void testLongOptionQuoteHandling() throws Exception { - final String[] args = {"--bfile", "\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("Confirm --bfile \"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); - } - - @Test - public void testLongOptionWithEqualsQuoteHandling() throws Exception { - final String[] args = {"--bfile=\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("Confirm --bfile=\"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); - } - - @Test - public void testShortOptionConcatenatedQuoteHandling() throws Exception { - final String[] args = {"-b\"quoted string\""}; - - final CommandLine cl = parser.parse(options, args); - - assertEquals("Confirm -b\"arg\" strips quotes", "quoted string", cl.getOptionValue("b")); - } - - @Test public void testWithRequiredOption() throws Exception { final String[] args = {"-b", "file"};