Mutually exclusive args with required values

6 views
Skip to first unread message

RaleighJava

unread,
Aug 3, 2017, 10:17:27 PM8/3/17
to jopt-simple
Hi,

Using 5.0.4 of the api, its very compact and allows you to express complex arg requirements neatly:) I am pretty new to it and i have not been able to figure out how to create a parser which will require the user to pass a host flag with a required value or a host.file flag with a required value, but not both.

This should pass:
--host="host1,host2"

This should pass
--host.file="c:/aldjfsdfj"

This should fail
--host.file="c:/aldjfsdfj" --host="host1,host2"

This should fail because either host or host.file is required.
--blah


I assumed I could use 
parser.mutuallyExclusive(parser.accepts(HOSTS).withRequiredArg().required(),parser.accepts(HOSTS_FILE).withRequiredArg().required());



This doesn't compile :) I also tried the below approach which require hosts even if host.file is present. I couldn't find a unit test that matched this scenario in the codebase.


parser.mutuallyExclusive(parser.accepts(HOSTS),parser.accepts(HOSTS_FILE));

parser
.accepts(HOSTS_FILE).withRequiredArg().required();

parser
.accepts(HOSTS).withRequiredArg().required();

Any advice would be greatly appreciated.
Thanks
Geoff

Paul Holser

unread,
Aug 7, 2017, 12:39:01 PM8/7/17
to jopt-simple
Hi Geoff,

You're right: the functionality you're looking for isn't handled.

It might be helpful to have `mutuallyExclusive()` return a result, that can further be qualified. Something like:

    parser.mutuallyExclusive(
        parser.accepts(HOSTS_FILE).withRequiredArg(),
        parser.accepts(HOSTS).withRequiredArg()
    ).required();

--p
Reply all
Reply to author
Forward
0 new messages