xenomachina / kotlin-argparser

Easy to use and concise yet powerful and robust command line argument parsing for Kotlin
GNU Lesser General Public License v2.1
485 stars 33 forks source link

Help message bug #41

Closed masics closed 6 years ago

masics commented 6 years ago

Assume you have following configuration:

val configFile by parser.storing("--config", "-c", argName = "CONFIGFILE", help = "Configuration file") { asPath }
val version by parser.flagging("--version", help = "print version and exit")

Invoking command like script --config filename --help will print:

usage: script [-h] [--config CONFIGFILE] [--version]

optional arguments:
  -h, --help             show this help message and exit

  --config CONFIGFILE,   Excel file with MLM configuration
  -c CONFIGFILE

  --version              print version and exit

As you can see "--config" is under optional arguments. It must be under mandatory ones.

If you execute: script --help The result is correct (config is under required):

usage: script [-h] --config CONFIGFILE [--version]

required arguments:
  --config CONFIGFILE,   Excel file with MLM configuration
  -c CONFIGFILE

optional arguments:
  -h, --help             show this help message and exit

  --version              print version and exit
xenomachina commented 6 years ago

Believe it or not, this is actually intentional behavior.

The motivation for this behavior is that you could create a wrapper scripts that supplied the option. eg:

#!/bin/bash
# autumn-script.sh
./script --config /usr/lib/script/autumn.config "$@"

and:

#!/bin/bash
# winter-script.sh
./script --config /usr/lib/script/winter.config "$@"

Now invoking autumn-script.sh or winter-script.sh will report --config as being optional.

I admit that this behavior is somewhat surprising. My assumption was that it would be pretty rare that a user would supply other options in addition to --help, except in the case where they were using a wrapper script that passed through options like the above examples.