Creating hidden arguments with Python argparse

PythonArgparse

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter SmitView Question on Stackoverflow
Solution 1 - PythonsrgergView Answer on Stackoverflow
Solution 2 - Pythonrob boudrieView Answer on Stackoverflow