@@ -57,8 +57,12 @@ public class DefaultParser implements CommandLineParser
5757 /** Flag indicating if partial matching of long options is supported. */
5858 private final boolean allowPartialMatching ;
5959
60+ /** Flag indicating if balanced leading and trailing double quotes should be stripped from option arguments. */
61+ private final boolean stripLeadingAndTrailingQuotes ;
62+
6063 /**
61- * Creates a new DefaultParser instance with partial matching enabled.
64+ * Creates a new DefaultParser instance with partial matching and
65+ * stripping of balanced leading and trailing double quotes from option arguments enabled.
6266 *
6367 * By "partial matching" we mean that given the following code:
6468 * <pre>
@@ -73,13 +77,19 @@ public class DefaultParser implements CommandLineParser
7377 * <code>"debug"</code> option. However, with "partial matching" disabled,
7478 * <code>-de</code> would enable both <code>debug</code> as well as
7579 * <code>extract</code> options.
80+ * with "stripping of balanced leading and trailing double quotes from option arguments" turned
81+ * on, the outermost balanced double quotes of option arguments values will be removed.
82+ * ie.
83+ * for <code>-o '"x"'</code> getValue() will return <code>x</code>, instead of <code>"x"</code>
7684 */
7785 public DefaultParser () {
7886 this .allowPartialMatching = true ;
87+ this .stripLeadingAndTrailingQuotes = true ;
7988 }
8089
8190 /**
82- * Create a new DefaultParser instance with the specified partial matching policy.
91+ * Create a new DefaultParser instance with the specified partial matching policy and
92+ * stripping of balanced leading and trailing double quotes from option arguments enabled.
8393 *
8494 * By "partial matching" we mean that given the following code:
8595 * <pre>
@@ -94,13 +104,48 @@ public DefaultParser() {
94104 * <code>"debug"</code> option. However, with "partial matching" disabled,
95105 * <code>-de</code> would enable both <code>debug</code> as well as
96106 * <code>extract</code> options.
97- *
107+ * with "stripping of balanced leading and trailing double quotes from option arguments" turned
108+ * on, the outermost balanced double quotes of option arguments values will be removed.
109+ * ie.
110+ * for <code>-o '"x"'</code> getValue() will return <code>x</code>, instead of <code>"x"</code>
98111 * @param allowPartialMatching if partial matching of long options shall be enabled
99112 */
100113 public DefaultParser (final boolean allowPartialMatching ) {
101114 this .allowPartialMatching = allowPartialMatching ;
115+ this .stripLeadingAndTrailingQuotes = true ;
116+ }
117+
118+ /**
119+ * Create a new DefaultParser instance with the specified partial matching and quote
120+ * stripping policy.
121+ *
122+ * By "partial matching" we mean that given the following code:
123+ * <pre>
124+ * {@code
125+ * final Options options = new Options();
126+ * options.addOption(new Option("d", "debug", false, "Turn on debug."));
127+ * options.addOption(new Option("e", "extract", false, "Turn on extract."));
128+ * options.addOption(new Option("o", "option", true, "Turn on option with argument."));
129+ * }
130+ * </pre>
131+ * with "partial matching" turned on, <code>-de</code> only matches the
132+ * <code>"debug"</code> option. However, with "partial matching" disabled,
133+ * <code>-de</code> would enable both <code>debug</code> as well as
134+ * <code>extract</code> options.
135+ * with "stripping of balanced leading and trailing double quotes from option arguments" turned
136+ * on, the outermost balanced double quotes of option arguments values will be removed.
137+ * ie.
138+ * for <code>-o '"x"'</code> getValue() will return <code>x</code>, instead of <code>"x"</code>
139+ * @param allowPartialMatching if partial matching of long options shall be enabled
140+ * @param stripLeadingAndTrailingQuotes if balanced outer double quoutes should be stripped
141+ */
142+ public DefaultParser (final boolean allowPartialMatching ,
143+ final boolean stripLeadingAndTrailingQuotes ) {
144+ this .allowPartialMatching = allowPartialMatching ;
145+ this .stripLeadingAndTrailingQuotes = stripLeadingAndTrailingQuotes ;
102146 }
103147
148+
104149 @ Override
105150 public CommandLine parse (final Options options , final String [] arguments ) throws ParseException
106151 {
@@ -280,7 +325,7 @@ else if ("--".equals(token))
280325 }
281326 else if (currentOption != null && currentOption .acceptsArg () && isArgument (token ))
282327 {
283- currentOption .addValueForProcessing (Util . stripLeadingAndTrailingQuotes (token ));
328+ currentOption .addValueForProcessing (conditionallyStripLeadingAndTrailingQuotes (token ));
284329 }
285330 else if (token .startsWith ("--" ))
286331 {
@@ -777,4 +822,12 @@ protected void handleConcatenatedOptions(final String token) throws ParseExcepti
777822 }
778823 }
779824 }
825+
826+ protected String conditionallyStripLeadingAndTrailingQuotes (final String token ) {
827+ if (stripLeadingAndTrailingQuotes ) {
828+ return Util .stripLeadingAndTrailingQuotes (token );
829+ } else {
830+ return token ;
831+ }
832+ }
780833}
0 commit comments