149

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

Peter Smit
  • 27,696
  • 33
  • 111
  • 170

2 Answers2

213

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
srgerg
  • 18,719
  • 4
  • 57
  • 39
  • 7
    Then it just shows up as `test ==SUPPRESS==`. At least when used with `add_parser`. – Thomas Ahle Apr 23 '20 at 11:39
  • @ThomasAhle you can, at least partially, [hide subparsers added with `.add_parser()` by omitting the help argument](https://github.com/python/cpython/issues/67037#issuecomment-1093669037). – Ari Cooper-Davis Sep 13 '22 at 10:31
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 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")
Community
  • 1
  • 1