java - commons cli complex argument list -


i trying build complex list of arguments without chaining multiple parsers using commons-cli project...

basically trying understand how arguments , optional arguments working together...

sample command help

$ admin <endpoint> <update> <name> [<type>] [<endpoint>] [<descriptions>]  //sample option creation     options.addoption(optionbuilder.hasargs(3).hasoptionalargs(2)                 .withargname("name> <type> <uri> [<description>] [<endpoint>]")                 .withvalueseparator(' ')                 .create("add"));  commandline line = parser.parse(options, args, true); 

the commandline not differentiate between required , optional arguments... how can retrieve them without have chain second parser optional options?

i'm not sure commons cli works unnamed, position-dependent arguments which, seems like, you're looking for. way write be:

option endpoint    = optionbuilder.hasargs(2)   .isrequired(true) .create("endpoint"); option update      = optionbuilder.hasarg(false).isrequired(false).create("update"); option name        = optionbuilder.hasarg(true) .isrequired(true) .create("name"); option type        = optionbuilder.hasarg(true) .isrequired(false).create("type"); option description = optionbuilder.hasarg(true) .isrequired(false).create("description"); 

i'm not 100% sure if first one, endpoint, require 2 arguments or require 1 can accept two; clearer use 2 different arguments completely.

this result in line looking like:

usage: admin -endpoint <point> [<point>] -update -name <name> [-type <type>] [-description <description>] 

i use constant string name:

public static final string endpoint = "endpoint"; ... option endpoint = optionbuilder.hasarg().create(endpoint); 

so way can refer later:

commandline opts = parser.parse(myopts, argv); string endpoint = opts.getoptionvalue(endpoint); 

hope helps!


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -