[New-bugs-announce] [issue29030] argparse: choices override metavar

Cyker Way report at bugs.python.org
Tue Dec 20 19:52:26 EST 2016


New submission from Cyker Way:

Using `choices` option in `argparse` module caused unwanted behavior.

# Without `choices`

    parser = argparse.ArgumentParser()
    parser.add_argument('length')
    parser.print_help()

## Output

    usage: demo.py [-h] length

    positional arguments:
      length

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

# With choices

    parser = argparse.ArgumentParser()
    parser.add_argument('length', choices=[1,2,3])
    parser.print_help()

## Output

    usage: demo.py [-h] {1,2,3}

    positional arguments:
      {1,2,3}

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

I think the doc says ArgumentParser objects use the `dest` value as the “name” of each object. And the doc doesn't mention anything about `dest` be changed by `choices`. The help text looks ugly when there is a long list of choices.

Currently the problem can be remedied by adding a `metavar` option. But I hope it will be fixed in the library itself.

----------
components: Library (Lib)
messages: 283713
nosy: cyker
priority: normal
severity: normal
status: open
title: argparse: choices override metavar
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29030>
_______________________________________


More information about the New-bugs-announce mailing list