What if I wanted to parse this:
java MyProgram -r opt1 -S opt2 arg1 arg2 arg3 arg4 --test -A opt3
And the result I want in my program is:
regular Java args[] of size=4
org.apache.commons.cli.Options[] of size=3
org.apache.commons.cli.Options[] #2 of size=1
I would prefer to use Apache Commons CLI, but the documentation is a little unclear about the case I present above. Specifically, the documentation doesn't tell you how to handle options of the 3rd type I specify below:
1. options with a "-" char 2. options with a "--" char 3. options without any marker, or "bare args"
I wish that Apache Commons CLI would work but STILL be able to pass regular args to the program if those args didn't have a option prefix. Maybe it does but the documentation doesnt say so as I read through it...
Use the Apache Commons CLI library commandline.getArgs() to get arg1, arg2, arg3, and arg4. Here is some code:
You could use the
refcodes-console
artifact at refcodes-console on REFCODES.ORG:Create your arguments parser
ArgsParserImpl
with your root condition:Above you define your syntax, below you invoke the parser:
In case you provided some good descriptions,
theArgsParser.printUsage()
will even show you the pretty printed usage:theRoot = new AndConditionImpl( r, s, a, arg1, arg2, arg3, arg4, new OptionalImpl( test ) );
Then your syntax looks as follows:
The full example for your case you find in the StackOverFlowExamle. You can use AND, OR, XOR conditions and any kind of nesting ... hope this helps.
Ok, thanks to Charles Goodwin for the concept. Here is the answer:
You could use https://github.com/jankroken/commandline , here's how to do that:
To make this example work, I must make assumptions about what the arguments means - just picking something here...
this can then be set up this way:
and then in the main method, you can just do:
Simple code for command line in java:
You could just do it manually.
NB: might be better to use a HashMap instead of an inner class for the opts.