I used to work with Apache Commons Cli 1.2. I wanted the parser to ignore arguments if they are unknown (not added to an Options-Object).
Example (pseudocode):
Options specialOptions;
specialOptions.addOption(null, "help", false, "shows help");
specialOptions.addOption(null, "version", false, "show version");
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args); //no third argument, since i dont want the program to stop parsing.
// run program with args: --help --unknown --version
// program shall parse --help AND --version, but ignore --unknown
I used this the solution by Pascal Schäfer: Can Apache Commons CLI options parser ignore unknown command-line options?
This worked fine for me on 1.2, and it works fine on 1.3.1 as well. But its deprecated. The parser I used got replaced by the DefaultParser
. I looked up the functionalities, but there is no such method processOptions
.
I really would like to use code that is not going to be deleted in later releases. Does anyone have an idea how to solve this problem?
Thanks, @kjp, for the suggestion; but the solutions doesn't work for arguments with values.
I've tried improving upon kjp's solution:
public class RelaxedParser extends DefaultParser {
@Override
public CommandLine parse(final Options options, final String[] arguments) throws ParseException {
final List<String> knownArgs = new ArrayList<>();
for (int i = 0; i < arguments.length; i++) {
if (options.hasOption(arguments[i])) {
knownArgs.add(arguments[i]);
if (i + 1 < arguments.length && options.getOption(arguments[i]).hasArg()) {
knownArgs.add(arguments[i + 1]);
}
}
}
return super.parse(options, knownArgs.toArray(new String[0]));
}
}
The same approach as given in the solution by Pascal can be used here.
public class RelaxedParser extends DefaultParser {
@Override
public CommandLine parse(Options options, String[] arguments) throws ParseException {
List<String> knownArguments = new ArrayList<>();
for (String arg : arguments) {
if (options.hasOption(arg)) {
knownArguments.add(arg);
}
}
return super.parse(options, knownArguments.toArray(new String[knownArguments.size()]));
}
}
Or alternatively, remove unknown options from args as above and use the DefaultParser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With