Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

args4j: How to make an argument required if another argument is/isn't given?

@Options(name="--in-file")
String fileName;

@Option(name="--table")
String table;

I would like to make the --table option be required if and only if no value is given for --in-file. How might I go about doing this? I know that there are some solutions (I think at least) where I can do this with multiple classes, but that seems like overkill for only two arguments in a simple program.

I know I can also manually check the values after the parsing is completed, but that seems to defeat the purpose of using args4j.

like image 221
Matthew Herbst Avatar asked Mar 18 '23 06:03

Matthew Herbst


1 Answers

You can use forbids as you mentioned.

@Options(name="--in-file", forbids{"--table"})
String fileName;

@Option(name="--table", forbids={"--in-file"})
String table;

And you can add check-condition in your class.

if (fileName == null && table == null) {
    throw new CmdLineException();
}

You can set exception message same with message shown when required option set.

like image 199
Ilya Zakirzyanov Avatar answered Apr 06 '23 03:04

Ilya Zakirzyanov