Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow omitting mandatory parameters #40

Open
masics opened this issue Jan 5, 2018 · 4 comments
Open

Allow omitting mandatory parameters #40

masics opened this issue Jan 5, 2018 · 4 comments

Comments

@masics
Copy link

masics commented Jan 5, 2018

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
Copy link
Owner

xenomachina commented Jan 18, 2018

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
Copy link

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
Copy link

joffrey-bion commented Aug 28, 2018

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:

  • a version flag
  • a path to a configuration file that provides all mandatory arguments

@pcoltau
Copy link

pcoltau commented Apr 11, 2019

Regarding the version flag, then I've recently made this PR: #70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants