@@ -29,6 +29,106 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2929 */
3030public class DefaultParser implements CommandLineParser {
3131
32+ /**
33+ * A nested builder class to create {@code DefaultParser} instances
34+ * using descriptive methods.
35+ *
36+ * Example usage:
37+ * <pre>
38+ * DefaultParser parser = Option.builder()
39+ * .setAllowPartialMatching(false)
40+ * .setStripLeadingAndTrailingQuotes(false)
41+ * .build();
42+ * </pre>
43+ *
44+ * @since 1.5
45+ */
46+ public static final class Builder {
47+
48+ /** Flag indicating if partial matching of long options is supported. */
49+ private boolean allowPartialMatching = true ;
50+
51+ /** Flag indicating if balanced leading and trailing double quotes should be stripped from option arguments. */
52+ private Boolean stripLeadingAndTrailingQuotes ;
53+
54+ /**
55+ * Constructs a new {@code Builder} for a {@code DefaultParser} instance.
56+ *
57+ * Both allowPartialMatching and stripLeadingAndTrailingQuotes are true by default,
58+ * mimicking the argument-less constructor.
59+ */
60+ private Builder () {
61+ }
62+
63+ /**
64+ * Builds an DefaultParser with the values declared by this {@link Builder}.
65+ *
66+ * @return the new {@link DefaultParser}
67+ * @since 1.5
68+ */
69+ public DefaultParser build () {
70+ return new DefaultParser (allowPartialMatching , stripLeadingAndTrailingQuotes );
71+ }
72+
73+ /**
74+ * Sets if partial matching of long options is supported.
75+ *
76+ * By "partial matching" we mean that given the following code:
77+ *
78+ * <pre>
79+ * {
80+ * @code
81+ * final Options options = new Options();
82+ * options.addOption(new Option("d", "debug", false, "Turn on debug."));
83+ * options.addOption(new Option("e", "extract", false, "Turn on extract."));
84+ * options.addOption(new Option("o", "option", true, "Turn on option with argument."));
85+ * }
86+ * </pre>
87+ *
88+ * If "partial matching" is turned on, {@code -de} only matches the {@code "debug"} option. However, with
89+ * "partial matching" disabled, {@code -de} would enable both {@code debug} as well as {@code extract}
90+ *
91+ * @param allowPartialMatching whether to allow partial matching of long options
92+ * @return this builder, to allow method chaining
93+ * @since 1.5
94+ */
95+ public Builder setAllowPartialMatching (final boolean allowPartialMatching ) {
96+ this .allowPartialMatching = allowPartialMatching ;
97+ return this ;
98+ }
99+
100+ /**
101+ * Sets if balanced leading and trailing double quotes should be stripped from option arguments.
102+ *
103+ * If "stripping of balanced leading and trailing double quotes from option arguments" is true,
104+ * the outermost balanced double quotes of option arguments values will be removed.
105+ * For example, {@code -o '"x"'} getValue() will return {@code x}, instead of {@code "x"}
106+ *
107+ * If "stripping of balanced leading and trailing double quotes from option arguments" is null,
108+ * then quotes will be stripped from option values separated by space from the option, but
109+ * kept in other cases, which is the historic behaviour.
110+ *
111+ * @param stripLeadingAndTrailingQuotes whether balanced leading and trailing double quotes should be stripped from option arguments.
112+ * @return this builder, to allow method chaining
113+ * @since 1.5
114+ */
115+ public Builder setStripLeadingAndTrailingQuotes (final Boolean stripLeadingAndTrailingQuotes ) {
116+ this .stripLeadingAndTrailingQuotes = stripLeadingAndTrailingQuotes ;
117+ return this ;
118+ }
119+ }
120+
121+ /**
122+ * Creates a new {@link Builder} to create an {@link DefaultParser} using descriptive
123+ * methods.
124+ *
125+ * @return a new {@link Builder} instance
126+ * @since 1.5
127+ */
128+ public static Builder builder () {
129+ return new Builder ();
130+ }
131+
32132 /** The command-line instance. */
33133 protected CommandLine cmd ;
34134
@@ -624,28 +724,6 @@ public CommandLine parse(final Options options, final String[] arguments, final
624724 return cmd ;
625725 }
626726
627- /**
628- * Removes the option or its group from the list of expected elements.
629- *
630- * @param option
631- */
632- private void updateRequiredOptions (final Option option ) throws AlreadySelectedException {
633- if (option .isRequired ()) {
634- expectedOpts .remove (option .getKey ());
635- }
636-
637- // if the option is in an OptionGroup make that option the selected option of the group
638- if (options .getOptionGroup (option ) != null ) {
639- final OptionGroup group = options .getOptionGroup (option );
640-
641- if (group .isRequired ()) {
642- expectedOpts .remove (group );
643- }
644-
645- group .setSelected (option );
646- }
647- }
648-
649727 /**
650728 * Strips balanced leading and trailing quotes if the stripLeadingAndTrailingQuotes is set
651729 * If stripLeadingAndTrailingQuotes is null, then do not strip
@@ -675,102 +753,24 @@ private String stripLeadingAndTrailingQuotesDefaultOn(final String token) {
675753 }
676754
677755 /**
678- * Creates a new {@link Builder} to create an {@link DefaultParser} using descriptive
679- * methods.
680- *
681- * @return a new {@link Builder} instance
682- * @since 1.5
683- */
684- public static Builder builder () {
685- return new Builder ();
686- }
687-
688- /**
689- * A nested builder class to create {@code DefaultParser} instances
690- * using descriptive methods.
691- *
692- * Example usage:
693- * <pre>
694- * DefaultParser parser = Option.builder()
695- * .setAllowPartialMatching(false)
696- * .setStripLeadingAndTrailingQuotes(false)
697- * .build();
698- * </pre>
756+ * Removes the option or its group from the list of expected elements.
699757 *
700- * @since 1.5
758+ * @param option
701759 */
702- public static final class Builder {
703-
704- /** Flag indicating if partial matching of long options is supported. */
705- private boolean allowPartialMatching = true ;
706-
707- /** Flag indicating if balanced leading and trailing double quotes should be stripped from option arguments. */
708- private Boolean stripLeadingAndTrailingQuotes ;
709-
710- /**
711- * Constructs a new {@code Builder} for a {@code DefaultParser} instance.
712- *
713- * Both allowPartialMatching and stripLeadingAndTrailingQuotes are true by default,
714- * mimicking the argument-less constructor.
715- */
716- private Builder () {
760+ private void updateRequiredOptions (final Option option ) throws AlreadySelectedException {
761+ if (option .isRequired ()) {
762+ expectedOpts .remove (option .getKey ());
717763 }
718764
719- /**
720- * Sets if partial matching of long options is supported.
721- *
722- * By "partial matching" we mean that given the following code:
723- *
724- * <pre>
725- * {
726- * @code
727- * final Options options = new Options();
728- * options.addOption(new Option("d", "debug", false, "Turn on debug."));
729- * options.addOption(new Option("e", "extract", false, "Turn on extract."));
730- * options.addOption(new Option("o", "option", true, "Turn on option with argument."));
731- * }
732- * </pre>
733- *
734- * If "partial matching" is turned on, {@code -de} only matches the {@code "debug"} option. However, with
735- * "partial matching" disabled, {@code -de} would enable both {@code debug} as well as {@code extract}
736- *
737- * @param allowPartialMatching whether to allow partial matching of long options
738- * @return this builder, to allow method chaining
739- * @since 1.5
740- */
741- public Builder setAllowPartialMatching (final boolean allowPartialMatching ) {
742- this .allowPartialMatching = allowPartialMatching ;
743- return this ;
744- }
765+ // if the option is in an OptionGroup make that option the selected option of the group
766+ if (options .getOptionGroup (option ) != null ) {
767+ final OptionGroup group = options .getOptionGroup (option );
745768
746- /**
747- * Sets if balanced leading and trailing double quotes should be stripped from option arguments.
748- *
749- * If "stripping of balanced leading and trailing double quotes from option arguments" is true,
750- * the outermost balanced double quotes of option arguments values will be removed.
751- * For example, {@code -o '"x"'} getValue() will return {@code x}, instead of {@code "x"}
752- *
753- * If "stripping of balanced leading and trailing double quotes from option arguments" is null,
754- * then quotes will be stripped from option values separated by space from the option, but
755- * kept in other cases, which is the historic behaviour.
756- *
757- * @param stripLeadingAndTrailingQuotes whether balanced leading and trailing double quotes should be stripped from option arguments.
758- * @return this builder, to allow method chaining
759- * @since 1.5
760- */
761- public Builder setStripLeadingAndTrailingQuotes (final Boolean stripLeadingAndTrailingQuotes ) {
762- this .stripLeadingAndTrailingQuotes = stripLeadingAndTrailingQuotes ;
763- return this ;
764- }
769+ if (group .isRequired ()) {
770+ expectedOpts .remove (group );
771+ }
765772
766- /**
767- * Builds an DefaultParser with the values declared by this {@link Builder}.
768- *
769- * @return the new {@link DefaultParser}
770- * @since 1.5
771- */
772- public DefaultParser build () {
773- return new DefaultParser (allowPartialMatching , stripLeadingAndTrailingQuotes );
773+ group .setSelected (option );
774774 }
775775 }
776776}
0 commit comments