python-argparse: assign a choice to each argument

Federico

This is what i want to do:

parser.add_argument('-io',
    nargs   = 2,
    type    = int,
    metavar = ('input','output'),
    choices = (range(1,65),range(1,5)),
    help    = 'set input -> output.'
)

So my input range is (1 to 64) and my output range is from (1 to 4).

Thanks!

All Workers Are Essential

You should split it into 2 separate arguments:

parser.add_argument('-i', type=int, metavar='input', choices=range(1,65), help='set input.')
parser.add_argument('-o', type=int, metavar='output', choices=range(1,5), help='set output.')

Or, if you really want to use one argument, you can do the checking manually:

parser.add_argument('--io', nargs=2, type=int, metavar=('input', 'output'), help='set input (1 to 64) -> output (1 to 4).')
# ...
args = parser.parse_args(argv[1:])

input, output = args.io
if input < 1 or 64 < input:
    raise ValueError("input:{!r} must be between 1 and 64.".format(input))
if output < 1 or 4 < output:
    raise ValueError("output:{!r} must be between 1 and 4.".format(output))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

`argparse` multiple choice argument?

From Dev

Reorder Python argparse argument groups

From Dev

Accessing argument values for argparse in Python

From Dev

Understanding argument parsing with argparse in Python

From Dev

Argument parsing python using ArgParse

From Dev

python argparse add_argument_group required

From Dev

Multiple files for one argument in argparse Python 2.7

From Dev

How to skip a positional argument in Python Argparse

From Dev

Use argparse module in python to parse a single argument

From Dev

Python argparse: Complex Argument Parsing Scenario

From Dev

Python argparse: default argument stored as string, not list

From Dev

How to access a python argparse argument with a dot in the name

From Dev

Python argparse set 1 or more parameters to an argument

From Dev

How to access a python argparse argument with a dot in the name

From Dev

How to skip a positional argument in Python Argparse

From Dev

python argparse with mandatory input file argument

From Dev

Python argparse accept variable argument (even flags)

From Dev

Assign each index argument different function

From Dev

ArgParse Python Module: Change default argument value for inherted argument

From Dev

Python: Using one argument to handle choice between a number and a string?

From Dev

Python: Using one argument to handle choice between a number and a string?

From Dev

How can I easily create a python argparse argument with an inverse?

From Dev

How to do a Python argparse mutually required argument group

From Dev

Python argparse : mutually exclusive arguments with optional and positional argument

From Dev

How to make a short and long version of a required argument using Python Argparse?

From Dev

Python argparse - Mutually exclusive group with default if no argument is given

From Dev

Python's argparse: How to use keyword as argument's name

From Dev

How have a variable number of parameters for one argument in Python with argparse?

From Dev

[Python argparse]set the behaviour of an argument to be interactive if no value is given

Related Related

  1. 1

    `argparse` multiple choice argument?

  2. 2

    Reorder Python argparse argument groups

  3. 3

    Accessing argument values for argparse in Python

  4. 4

    Understanding argument parsing with argparse in Python

  5. 5

    Argument parsing python using ArgParse

  6. 6

    python argparse add_argument_group required

  7. 7

    Multiple files for one argument in argparse Python 2.7

  8. 8

    How to skip a positional argument in Python Argparse

  9. 9

    Use argparse module in python to parse a single argument

  10. 10

    Python argparse: Complex Argument Parsing Scenario

  11. 11

    Python argparse: default argument stored as string, not list

  12. 12

    How to access a python argparse argument with a dot in the name

  13. 13

    Python argparse set 1 or more parameters to an argument

  14. 14

    How to access a python argparse argument with a dot in the name

  15. 15

    How to skip a positional argument in Python Argparse

  16. 16

    python argparse with mandatory input file argument

  17. 17

    Python argparse accept variable argument (even flags)

  18. 18

    Assign each index argument different function

  19. 19

    ArgParse Python Module: Change default argument value for inherted argument

  20. 20

    Python: Using one argument to handle choice between a number and a string?

  21. 21

    Python: Using one argument to handle choice between a number and a string?

  22. 22

    How can I easily create a python argparse argument with an inverse?

  23. 23

    How to do a Python argparse mutually required argument group

  24. 24

    Python argparse : mutually exclusive arguments with optional and positional argument

  25. 25

    How to make a short and long version of a required argument using Python Argparse?

  26. 26

    Python argparse - Mutually exclusive group with default if no argument is given

  27. 27

    Python's argparse: How to use keyword as argument's name

  28. 28

    How have a variable number of parameters for one argument in Python with argparse?

  29. 29

    [Python argparse]set the behaviour of an argument to be interactive if no value is given

HotTag

Archive