13

For some reason I do not use positional arguments in my program but accept "optional" arguments only, controlling whether an argument is truly optional by facilities like narg='?' or action='store_true'. Thus the "optional arguments" in the help text will be misleading. Can I display it simply as "arguments"? Thank you.

4

2 回答 2

14

好吧,看看argparse源代码,在我看来它就像覆盖titleof一样简单parser._optionals,如下所示:

parser._optionals.title = "my mandatory arguments, they are actually optionals, but I'll check for their presence"

可能我应该提到这是一个肮脏的黑客,你的整个想法有点疯狂,因为切换到位置参数很容易做到,可选参数是可选的。

于 2013-06-07T10:15:13.267 回答
4
import argparse

parser = argparse.ArgumentParser()
for grp in parser._action_groups:
    if grp.title == 'optional arguments':
        grp.title = 'arguments'
...
于 2013-06-07T10:14:32.953 回答