args4j icon indicating copy to clipboard operation
args4j copied to clipboard

Missing multiValued attribute in @Option

Open pfichtner opened this issue 11 years ago • 11 comments

After updating to the newest args4j version I do have compile errors because of the removed "multiValued" attribute. I found the commit message which told, that it was removed because it would make no sense to have multi-value settings.

What about this case (which is simlar to mine):

@Option(name = "-in", multiValued = true, required = true)
private List<File> inFiles;

where I do support multiple ouccrences of -in (e.g. FooMain -in fileA.txt -in fileB.txt -in fileC.txt). How can this be done with the new release of args4j?

pfichtner avatar Jun 26 '13 20:06 pfichtner

We're also using multiValued. If the option doesn't hurt, i think you should left it to the user whether it makes sense or not.

micheljung avatar Aug 07 '13 16:08 micheljung

Hello, I was using the multiValues attribute, too.

I guess I could use just a single value argument and pass multiple values separated by a comma. In example, instead: -in fileA -in fileB -in fileC You could use: -in fileA,fileB,fileC

Of course you need to take into account if the file have a comma in the name but you can just force your users to escape the comma: -in file,A,fileB,fileC

devent avatar Dec 06 '13 11:12 devent

Is there an alternative to using multiValued in there, or a reason it was taken out? Is there interest a contribution to add it back in?

arahuja avatar Jan 22 '14 16:01 arahuja

Who knows if anyone here cares or not still, but seems like multivalue just works if you use a list... see here -> multivalued test for more info.

jaredpetker avatar Jan 08 '15 08:01 jaredpetker

@jaredpetker

Thanks for the info, I've been looking for that.

abreksa4 avatar Jan 08 '15 16:01 abreksa4

Finally figured out how to do this. It should really be added to the main example, considering how useful it is, (and considering I had to do work to figure out how to do it) but here's the solution:

@Option(name = "-in", handler=StringArrayOptionHandler.class")
private List<String> inFiles;

Then you can use, -in <item1> <item2>

dessalines avatar May 14 '15 15:05 dessalines

+1 for adding this to the doc. Thanks!

AshwinJay avatar Nov 17 '15 00:11 AshwinJay

I had to pass

-in "United States" "United Kingdom"

Found that the handler is breaking it into four words : [United, States, United, Kingdom] but I expected two words : [United States, United Kingdom]

Warning:Stay away from this if your argument values consists of multiwords with spaces.

thammegowda avatar Dec 11 '15 15:12 thammegowda

@thammegowda note that the Javadoc for StringArrayOptionHandler indicates this is expected behavior:

All of [these examples] result in a single string array that contains three tokens: banan, hruska, and jablko.

If you need proper quoting support it might be better to use the default behavior and use the flag repeatedly, i.e.:

-in "United States" -in "United Kingdom"

dimo414 avatar Apr 11 '16 05:04 dimo414

Hi all,

I found maybe a bug when I configure an option like that:

@Option(name = "-in", handler=StringArrayOptionHandler.class")
private List<String> inFiles;

When I do this call (no operands): myApp -in

There are no exceptions thrown to tell that there are no operands.

StringArrayOptionHandler.parseArguments will return 0 and the test in CmdLineParser.getParameter(int ) does not fail like it does with a simple string option

70m4 avatar Aug 29 '16 09:08 70m4

There are no exceptions thrown to tell that there are no operands. StringArrayOptionHandler.parseArguments will return 0

I would consider this the correct behaviour, since an empty list looks totally fine to me

ooxi avatar Jan 31 '18 12:01 ooxi