Reputation: 903
I am using the Common CLI for a personal project. One thing I didn't find from the documentation is that how to enforce a certain argument to present.
To clarify my question, can I define the different between argument and option, the command:
mycommand file.txt -b 2
mycommand is the command,
file.txt is the argument
-b 2 is the option where 2 is the option value
With Common CLI, I can add -b 2 as an option like this:
options.addOption( "b", true, "Some message" );
And parse the arguments using:
CommandLineParser commandParser = new GnuParser();
CommandLine result = commandParser.parse(options, args)
but how can I specify file.txt is also required?
Many thanks
Upvotes: 3
Views: 3129
Reputation: 18403
No, is not possible with current API, but I think you can extends GnuParser with your own implementation of Parser.parse()
if mandatory parameter name EVER file.txt.
Else, if file name can change, you can override Parser.processArgs()
(for not Option args, your filename, I mean) and Parser.processOption()
(to set a flag indicates you found a valid Option): if you enter in Parser.processArgs()
when flag is set you found an invalid unnamed arg)
public class MyGnuParser extends GnuParser {
private int optionIndex;
private String filename;
public MyGnuParser() {
this.optionIndex = 0;
this.filename = null;
}
public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException {
CommandLine cmdLine = super.parse(options, arguments, properties, false);
if(this.filename == null) throw new ParseException(Missing mandatory filename argument);
}
@Override
public void processArgs(Option opt, ListIterator iter) throws ParseException {
super.processArgs(opt, item);
++this.optionIndex;
}
@Override
protected void processOption(final String arg, final ListIterator iter) throws ParseException {
if(this.optionIndex > 0) {
throw new ParseException(non-opt arg must be the first);
}
if(this.filename != null) {
throw new ParseException(non-opt invalid argument);
}
this.filename = arg;
++this.optionIndex;
}
}
MyGnuParser p = new MyGnuParser();
CommandLine cmdLine = p.parse(options, args, properties);
and in p.filename
(or cmdLine.getArgs[0]
) you can get filename.
It's not intuitive, but with CLI API I don't known any other way
Upvotes: 1
Reputation: 31648
EDIT: I didn't realize you meant make the target (not an option) to be required.
If you use the full parse methodCommandLineParser.parse(Options, String[], boolean)
with the optional flag set to false, then the parser will skip over unknown arguments.
You can later retrieve them by the method getArgs()
which returns a String[]
You can then go through those Strings to make sure there is a string called file.txt
Options options = new Options();
options.addOption("b", true, "some message");
String[] myArgs = new String[]{"-b","2", "file.txt"};
CommandLineParser commandParser = new GnuParser();
CommandLine commandline = commandParser.parse(options, myArgs, false);
System.out.println(Arrays.toString(commandline.getArgs()));
Will print [file.txt]
to the screen.
so you add an extra check to search through that array to find any required targets:
boolean found=false;
for(String unparsedTargets : commandline.getArgs()){
if("file.txt".equals(unparsedTargets)){
found =true;
}
}
if(!found){
throw new IllegalArgumentException("must provide a file.txt");
}
I agree it's messy but I don't think CLI provides a clean way to do this.
Upvotes: 2