简体   繁体   English

Python argparse-强制参数-位置或可选

[英]Python argparse - mandatory argument - either positional or optional

I want the user to be able to pass a mandatory argument to 'argparse', with either positional, or optional argument. 我希望用户能够使用位置或可选参数将强制性参数传递给“ argparse”。

Ie, both following forms are valid: 即,以下两种形式均有效:

my_prog arg
my_prog -m arg

I've seen Argparse optional positional arguments? 我看过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) (即使使用我的手动方法,“帮助”部分也会将二者显示为可选)

The mutually exclusive group mechanism can take a required parameter. mutually exclusive group机制可以采用required参数。 It also works with one ? 它也可以用? positional along with the optionals (flagged arguments). 位置以及可选参数(带有标记的参数)。 (more than one '?' positional doesn't make sense). (多个“?”位置没有意义)。

As for the help display there are 2 default groups, positonal and optional . 至于help显示,有2个默认组,即positonaloptional So even if an optional (flagged) is set to required it is, by default, displayed in the optional group. 因此,即使将optional (标记)设置为required ,默认情况下,它也会显示在optional组中。 The usage line is a better guide as to whether an argument is required or not. usage行是关于是否需要参数的更好指南。 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

Oops, usage isn't showing the -f and pos in a mutually exlusive group. 糟糕,用法未在互斥组中显示-fpos Sometimes that usage formatting is brittle. 有时, usage格式很脆弱。

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

This is the one exception to the general rule argument_groups and mutually_exclusive_groups aren't designed for nesting. 这是一般规则的一个例外,argument_groups和Mutual_exclusive_groups不是为嵌套而设计的。

The mx-group was not required, usage would use [] 不需要mx-group,用法将使用[]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM