Python argparse-必选参数-位置或可选 [英] Python argparse - mandatory argument - either positional or optional

查看:110
本文介绍了Python argparse-必选参数-位置或可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够使用位置或可选参数将强制性参数传递给'argparse'.

I want the user to be able to pass a mandatory argument to 'argparse', with either positional, or optional argument.

即 以下两种形式均有效:

I.e., both following forms are valid:

my_prog arg
my_prog -m arg

我见过 Argparse可选位置参数?

但是那里的建议使两者形式都是可选的.我希望其中的一个 是强制性的.

But the suggestions there make both form optional. I want that one of them will be mandatory.

当然,我可以在解析至少已设置其中之一后添加手动检查.但是我有一种预感,那就是必须有一个更好的解决方案.

Of course, I can add a manual checking after parsing that at least one of them has been set. But I got a hunch that there must be a better solution.

(即使使用我的手动方法,帮助"部分也会将它们显示为可选)

(And even with my manual approach, the 'help' section shows both of them as optional)

推荐答案

mutually exclusive group机制可以采用required参数.它也可以与一个?位置以及可选参数(带有标记的参数)一起使用. (一个以上的?"位置没有意义).

The mutually exclusive group mechanism can take a required parameter. It also works with one ? positional along with the optionals (flagged arguments). (more than one '?' positional doesn't make sense).

关于help显示屏,有2个默认组,positonaloptional.因此,即使optional(标记)设置为required,它也默认显示在optional组中. usage行是关于是否需要参数的更好指南.如果您不喜欢帮助部分中的组标签,请定义自己的参数组.

As for the help display there are 2 default groups, positonal and optional. So even if an optional (flagged) is set to required it is, by default, displayed in the optional group. The usage line is a better guide as to whether an argument is required or not. If you don't like the group labels in the help section, define your own argument groups.

In [146]: import argparse
In [147]: parser = argparse.ArgumentParser()
In [148]: gp = parser.add_mutually_exclusive_group(required=True)
In [149]: gp.add_argument('pos', nargs='?', default='foo');
In [150]: gp.add_argument('-f','--foo', default='bar');

In [151]: parser.parse_args('arg'.split())
Out[151]: Namespace(foo='bar', pos='arg')

In [152]: parser.parse_args('-f arg'.split())
Out[152]: Namespace(foo='arg', pos='foo')

In [153]: parser.parse_args('arg -f arg'.split())
usage: ipython3 [-h] [-f FOO] [pos]
ipython3: error: argument -f/--foo: not allowed with argument pos

In [154]: parser.parse_args(''.split())
usage: ipython3 [-h] [-f FOO] [pos]
ipython3: error: one of the arguments pos -f/--foo is required


In [155]: parser.parse_args('-h'.split())
usage: ipython3 [-h] [-f FOO] [pos]

positional arguments:
  pos

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

糟糕,用法未在互斥组中显示-fpos.有时候usage格式很脆弱.

Oops, usage isn't showing the -f and pos in a mutually exlusive group. Sometimes that usage formatting is brittle.

切换参数定义的顺序可以更好地使用

Switching the order in which the arguments are defined gives a better usage

In [156]: parser = argparse.ArgumentParser()
In [157]: gp = parser.add_mutually_exclusive_group(required=True)
In [158]: gp.add_argument('-f','--foo', default='bar');
In [159]: gp.add_argument('pos', nargs='?', default='foo');
In [160]: 
In [160]: parser.parse_args('-h'.split())
usage: ipython3 [-h] (-f FOO | pos)

positional arguments:
  pos

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

使用用户定义的参数组:

With a user defined argument group:

In [165]: parser = argparse.ArgumentParser()
In [166]: gp = parser.add_argument_group('Mutually exclusive')
In [167]: gpm = gp.add_mutually_exclusive_group(required=True)
In [168]: gpm.add_argument('-f','--foo', default='bar');
In [169]: gpm.add_argument('pos', nargs='?', default='foo');
In [170]: 
In [170]: parser.parse_args('-h'.split())
usage: ipython3 [-h] (-f FOO | pos)

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

Mutually exclusive:
  -f FOO, --foo FOO
  pos

这是一般规则的一个例外,argument_groups和互斥_groups不是为嵌套而设计的.

This is the one exception to the general rule argument_groups and mutually_exclusive_groups aren't designed for nesting.

不需要m-x-group,用法将使用[]

The m-x-group was not required, usage would use []

usage: ipython3 [-h] [-f FOO | pos]

这篇关于Python argparse-必选参数-位置或可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆