I'm about to migrate some legacy code to contain less deprecated warnings from 3rd-party libraries. For Apache commons-cli
library (version: 1.3.1) I detected in the official JavaDoc that GnuParser
is deprecated and DefaultParser
should be used instead:
@deprecated since 1.3, use the
{@link DefaultParser}
instead
However, the following code snippet stops working as expected:
Options options = new Options();
Option optionGSTypes = new Option(
"gst","gs-types", true,
"the supported types, comma-separated: article, category, template, all");
optionGSTypes.setArgs(3);
optionGSTypes.setValueSeparator(',');
options.addOption(optionGSTypes);
// ... other options
// parsed option values are correct, yet this is deprecated
CommandLineParser parser = new GnuParser();
CommandLine commands = parser.parse(options, args);
// ... interpret parsed 'commands' and related actual values via CLI
Note that setValueSeparator(',')
is used here to define a custom separator char ,
to enable the CLI to support sevaral gst-types (see code snippet).
As input the following program arguments are used to call the CLI:
java -jar MyCLI.jar -gst category -gsd 4
Obviously, several other arguments might also have been added after the gsd parameter. The expected and correctly parsed options for the separator-less use of the "gst" argument are (via GnuParser
):
- "category" (and nothing else)
However, when I change my code and switch towards the recommended parser via:
CommandLineParser parser = new DefaultParser();
the resulting, parsed values are detected incorrectly as:
- "category"
- "-gsd"
- "4"
Hint: I used a debugger to verify the incorrect result of the parse process via inspecting the field values
in org.apache.commons.cli.Option
via the returned commands
variable.
My expectation would be that the internal change of the parser should not yield different results, as this breaks existing code. Has anyone ever encountered the same behavior with Apache Commons-CLI when switching to DefaultParser
and several option values and custom separators?
Is there a difference in the construction/usage of DefaultParser
that I might have overseen?