2

I'm trying to use Apache Commons CLI to recognize the arguments of my java program. So, there is a bunch of options (such as "-p" as en example) but mostly, there is the name of the file that should be used as an input for the java program. Note that all options have no order restriction.

Example of the launch of the program :

decac -p -r 5 test.deca

I have no problems parsing the -p and -r (with argument "5") options but how do I create an option for "test.deca" knowing that it can be anything. It could also be something like pathTo/anotherTest.deca

Aaron
  • 24,009
  • 2
  • 33
  • 57
Fitz
  • 327
  • 1
  • 6
  • 19
  • @Aaron It seems it would solve my problem. Still wouldn't fix the problem in a more general way (multiple filenames) but should work for me! Thank you! – Fitz Jan 09 '17 at 14:14
  • You're welcome, I find it strange this wasn't documented in their usage scenario – Aaron Jan 09 '17 at 14:15
  • I think it fixes the problem in a general way : this list should contain any token that couldn't be parsed by the `CommandLineParser`, so for `decac -p other.file -r 5 test.deca and.another` it should contain `other.file`, `test.deca` and `and.another`. – Aaron Jan 09 '17 at 14:35
  • @Aaron Yes yes but I was thinking more of an Option with regular expressions in order to use them as normal options. thanks again. – Fitz Jan 09 '17 at 14:57
  • i think this option is added recently. try this https://stackoverflow.com/questions/7449798/trailing-args-using-apache-commons-cli-library – Prasanna Bableshwar Apr 15 '22 at 15:48

1 Answers1

0

I think it is not possible out-of-the-box with ApacheCommonsCLI. You could do some tricks according to this answer or you can use an argument option as described in the apache CLI documentation

Option decafile   = OptionBuilder.withArgName( "file.deca" )
                            .hasArg()
                            .withDescription(  "target deca file" )
                            .create( "decafile" );

That can be used later on with -dfile.deca or --decafile=file.deca

Community
  • 1
  • 1
DrHopfen
  • 752
  • 3
  • 13
  • Not a solution to me as I can't change the way the arguments are entered. Thanks for the answer! – Fitz Jan 09 '17 at 14:17