question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Repeating argument inside [options]

See original GitHub issue

Hi.

I was trying to simplify some scripts by using [options] but i got into a issue when i had to have repeating arguments.

The following code is the old way where i have to define the repeating arguments in the usage section

from docopt import docopt

__docopt__ = """
usage:
  script foo <bar> [-v ...] [options]

Options:
  <bar>               Key identifier username
  -v                  increase verbosity
"""

args = docopt(__docopt__)
print(args)

that works fine when callde with

$ python repeat_broken.py foo asd -vvv

{'-v': 3,
 '<bar>': 'asd',
 'foo': True}

but when trying to move the repeating part down to the options definition i get the following

from docopt import docopt

__docopt__ = """
usage:
  script foo <bar> [options]

Options:
  <bar>               Key identifier username
  -v ...              increase verbosity
"""

args = docopt(__docopt__)
print(args)

and called with

$ python repeat_broken.py foo asd -vvv

{'-v': 'vv',
 '<bar>': 'asd',
 'foo': True}

In this case it interpretates the ... as something different then when defined in the usage section.

Is it intended that ... should not be possible to use in the options section or is this a bug?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:8
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
alexcrichtoncommented, May 6, 2015

Cargo also recently ran into this problem, our doc string should in theory allow commands of the form:

cargo build --bin foo --bin bar

but unfortunately you can only pass one --bin for now. We could in theory modify the usage string to include [--bin NAME ...], but we’d have to repeat that for --test, --example, etc, and also repeat that across the other subcommands that Cargo has. Overall I agree with @BurntSushi that modifying the options section would be what I’d love to see as well!

1reaction
BurntSushicommented, Jul 28, 2015

I have an experimental implementation of this working and I think it works quite nicely. I basically just took the idea in the OP and ran with it. If ... proceeds a flag name/argument in an option description (separated by exactly one space, just like the argument), then that flag is allowed to be repeated an arbitrary number of times. Namely, this:

Usage: prog [-a ...]

is equivalent to

Usage: prog [options]

Options:
  -a ...

I’ve written down some test cases that should clarify the rest:

r"""Usage: prog [options]

Options:
  -a ...  Foo
"""
$ program
{"-a": 0}
$ program -a
{"-a": 1}
$ program -a -a
{"-a": 2}
$ program -aa
{"-a": 2}
$ program -a -a -a
{"-a": 3}
$ program -aaa
{"-a": 3}

r"""Usage: prog [options]

Options:
  -a, --all ...  Foo
"""
$ program
{"-a": 0}
$ program -a
{"-a": 1}
$ program -a --all
{"-a": 2}
$ program -aa --all
{"-a": 3}
$ program --all
{"-a": 1}
$ program --all --all
{"-a": 2}

r"""Usage: prog [options]

Options:
  -a, --all ARG ...  Foo
"""
$ program
{"-a": []}
$ program -a 1
{"-a": ["1"]}
$ program -a 2 --all 3
{"-a": ["2", "3"]}
$ program -a4 -a5 --all 6
{"-a": ["4", "5", "6"]}
$ program --all 7
{"-a": ["7"]}
$ program --all 8 --all 9
{"-a": ["8", "9"]}

r"""Usage: prog [options]

Options:
  --all ...  Foo
"""
$ program
{"--all": 0}
$ program --all
{"--all": 1}
$ program --all --all
{"--all": 2}

r"""Usage: prog [options]

Options:
  --all=ARG ...  Foo
"""
$ program
{"--all": []}
$ program --all 1
{"--all": ["1"]}
$ program --all 2 --all 3
{"--all": ["2", "3"]}

r"""Usage: prog [options]

Options:
  --all  ...  Foo
"""
$ program --all --all
"user-error"

r"""Usage: prog [options]

Options:
  --all ARG  ...  Foo
"""
$ program --all foo --all bar
"user-error"

Two notes:

  1. This prevents flag argument names from beginning with a .. I don’t think I’ve ever seen such a flag argument name in the wild, so I think that’s OK.
  2. I’ve left out any ability to emulated bounded repetition, which can still only be expressed in a usage pattern (e.g., -aa). I’m not convinced we want that much flexibility in the option descriptions, and if we did, it could always be added later.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Repeating arguments specified in options list - docopt
I'm aware that having repeating optional arguments is possible, I would just really prefer to specify so inside the options description ...
Read more >
Declare function argument validation - MATLAB arguments
Repeating arguments are single arguments or groups of arguments that can be repeated zero or more times in a function call. The fRepeat...
Read more >
Multiple CLI Options - Typer - tiangolo
You can declare a CLI option that can be used multiple times, and then get all the values. For example, let's say you...
Read more >
Difference between multiple arguments and options object
Example: This is an example of multiple arguments in javascript. Actually arguments is not like an arrays, It does not follow the array ......
Read more >
Command-line syntax overview for System.CommandLine
CommandLine can be configured to accept multiple arguments for one option without repeating the option name. In the following example, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found