Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show hidden option using argparse

I'm using argprase to create an option, and it's a very specific option to do one specific job. The script currently has roughly 30 knobs, and most aren't used regularly.

I'm creating an option:

opt.add_argument('-opt',help="Some Help", help=argparse.SUPPRESS)

But i want there to be two ways to show the help for the script:

my_script -help
my_script -help-long

I want the -help-long to also show all the hidden args. I couldn't find a way to do this.

Is there a way to implement this behavior?

like image 296
Blue Granny Avatar asked May 18 '16 15:05

Blue Granny


2 Answers

I don't think there's a builtin way to support this. You can probably hack around it by checking sys.argv directly and using that to modify how you build the parser:

import sys
show_hidden_args = '--help-long' in sys.argv

opt = argparse.ArgumentParser()
opt.add_argument('--hidden-arg', help='...' if show_hidden_args else argparse.SUPPRESS)
opt.add_argument('--help-long', help='Show all options.', action='help')


args = opt.parse_args()

Of course, if writing this over and over is too inconvenient, you can wrap it in a helper function (or subclass ArgumentParser):

def add_hidden_argument(*args, **kwargs):
    if not show_hidden_args:
        kwargs['help'] = argparse.SUPPRESS
    opt.add_argument(*args, **kwargs)

And you'll probably want to add a non-hidden --help-long argument so that users know what it supposedly does...

like image 187
mgilson Avatar answered Oct 21 '22 10:10

mgilson


This is a variation on @mgilson's answer, looking in sys.argv to see whether we should suppress some help or not

import argparse
import sys

def hide_args(arglist):
    for action in arglist:
        action.help=argparse.SUPPRESS

hidelist=[]     
parser = argparse.ArgumentParser()
a1 = parser.add_argument('--foo',help='normal')
a2 = parser.add_argument('--bar',help='hidden')
hidelist.append(a2)

if '-h' in sys.argv[1:]:
    hide_args(hidelist)
args = parser.parse_args()

Here I've chosen to interpret --help as asking for a long help; -h for the short. I could have added a separate --longhelp argument instead.

1207:~/mypy$ python3 stack37303960.py --help
usage: stack37303960.py [-h] [--foo FOO] [--bar BAR]

optional arguments:
  -h, --help  show this help message and exit
  --foo FOO   normal
  --bar BAR   hidden

for a short help

1207:~/mypy$ python3 stack37303960.py -h
usage: stack37303960.py [-h] [--foo FOO]

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

add_argument returns a pointer to the Action object that it created. Here I save selected ones in the hidelist. Then I conditionally iterate through that list and change the help to SUPPRESS. Many of the attributes of an Action can be changed after the initial creation (experiment in an interactive session).

The parser also maintains a list of actions. The default help is the first one on the parser._actions list. It uses this list both for parsing and formatting the help.

In [540]: parser._actions[0]
Out[540]: _HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None)
like image 35
hpaulj Avatar answered Oct 21 '22 10:10

hpaulj