È possibile aggiungere un argomento a un python argparse.ArgumentParsersenza che venga visualizzato nell'uso o nella guida ( script.py --help)?
È possibile aggiungere un argomento a un python argparse.ArgumentParsersenza che venga visualizzato nell'uso o nella guida ( script.py --help)?
Risposte:
Sì, puoi impostare l' helpopzione add_argumentsu argparse.SUPPRESS. Ecco un esempio dalla documentazione di argparse :
>>> 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
Lo faccio aggiungendo un'opzione per abilitare quelli nascosti e lo afferro guardando sysv.args.
Se lo fai, devi includere l'argomento speciale che scegli sys.argvdirettamente nell'elenco di analisi se presumi che l'opzione sia -sdi abilitare le opzioni nascoste.
parser.add_argument('-a', '-axis',
dest="axis", action="store_true", default=False,
help="Rotate the earth")
if "-s" in sys.argv or "-secret" in sys.argv:
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")
sysv.argsun errore di battitura per sys.argv?
test ==SUPPRESS==. Almeno se usato conadd_parser.