Creating hidden arguments with Python argparse

*爱你&永不变心* 提交于 2019-11-27 01:26:03

问题


Is it possible to add an Argument to an python argparse.ArgumentParser without it showing up in the usage or help (script.py --help)?


回答1:


Yes, you can set the help option to add_argument to argparse.SUPPRESS. Here's an example from the argparse documentation:

>>> parser = argparse.ArgumentParser(prog='frobble')
>>> parser.add_argument('--foo', help=argparse.SUPPRESS)
>>> parser.print_help()
usage: frobble [-h]

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



回答2:


I do it by adding an option to enable the hidden ones, and grab that by looking at sysv.args.

If you do this, you have to include the special arg you pick out of sys.argv directly in the parse list if you Assume the option is -s to enable hidden options.

parser.add_argument('-a', '-axis',
                    dest="axis", action=store_true, default=False,
                    help="Rotate the earth)
if "-s" in sysv.args:
    parser.add_argument('-s', '-secret',
                        dest="secret", action=store_true, default=False,
                        help="Enable secret options")
    parser.add_argument('-d', '-drill',
                        dest="drill", action=store_true, default=False,
                        help="drill baby, drill)


来源:https://stackoverflow.com/questions/11114589/creating-hidden-arguments-with-python-argparse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!