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
483 stars 33 forks source link

Allow omitting mandatory parameters #40

Open masics opened 6 years ago

masics commented 6 years ago

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

You cannot specify only "--version" in command line - it will complain about "missing CONFIGFILE"

We need an option for allowing printing version.

Alternatively allow multiple configurations: usage: --config CONFIGFILE usage: --version

xenomachina commented 6 years ago

I've been working on a change to add "subcommand" support (see #2 ). Do you think that would be sufficient for what you're trying to do?

The user-interface would look slightly different. Instead of:

program --config CONFIGFILE
program --version

it would be:

program config CONFIGFILE
program version

In general, the format is:

program [global options...] SUB-COMMAND-NAME [sub-command-specific options and arguments]

This is similar to (and is based on) tools like git where one top-level command (git) has multiple sub-commands (checkout, clone, push, reset, etc.) that take different options/arguments.

davidschreiber commented 6 years ago

Sub-command support seems like the cleaner approach than omission of required parameters. I would really love to see this, as right now most of my commands are optional (although at least one should be probably mandatory at any time).

joffrey-bion commented 6 years ago

Sub-command support seems like the cleaner approach than omission of required parameters.

Subcommands are great, but I believe that it's not the right tool for the job here. Most programs don't work with subcommands and have instead some sort of "multiple usages" mindset, which I believe can be reasonably named the "anonymous subcommands" or "implicit subcommands" pattern.

I believe a lot of programs want usages like this:

Usage: program -h
Usage: program -v
Usage: program [standard args]

which corresponds to implicit "help", "version", and "default" subcommands.

There is actually one such implicit subcommand already implemented in kotlin-argparser: the help flag. It'd be nice to generalize this behaviour, maybe by using mutually exclusive option groups, that would act like anonymous subcommands.

A more simple implementation would be a special option type "standalone option" which would at least allow separating a single option from the rest, exactly like the help flag but for any custom option.

This feature would be very useful to implement for instance:

pcoltau commented 5 years ago

Regarding the version flag, then I've recently made this PR: https://github.com/xenomachina/kotlin-argparser/pull/70