Pregunta

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!

¿Fue útil?

Solución

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))

Otros consejos

The easiest way would be to add these as seperate arguments to your parser.

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.')

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top