Python Can metavar of argparse option be accessed?

Question:

I was wondering if there was a way to use the metavar from argparse be grabbed from elsewhere. Example, there is a -f FILE option, and a -d DIR option. Can I make the help of -d grab the file.metavar or some such?
Maybe:

 parser.add_argument("-f", "--file",
          metavar = 'FILE',
          action="store", dest="file_name", default="foo.bar",
          help="Name of the {} to be loaded".format(metavar))
parser.add_argument("-d", "--dir",
          metavar = 'DIR',
          action = 'store', dest='file_dir', default=".",
          help="Directory of {} if it is not current directory".format(option.file_name.metavar)

I know this code is wrong (string doesn’t have metavar and options doesn’t get set until the parser.parse_args() is run), but I have a few other times I want to just grab the metavar without having a bunch of:

 meta_file = 'FILE'
 meta_dir = 'DIR'
 meta_inquisition = 'SURPRISE'

floating around.

Thanks.

EDIT: s/add_option/add_argument/

Asked By: Silas Greenback

||

Answers:

According to the docs for the help argument, the help text can contain format specifiers like %(default)s, %(type)s, etc. You can write %(metavar)s and it will be expanded to the value of the metavar (or None if it’s not specified).

I don’t think there’s a way to grab another’s argument metavar.

Answered By: Ismail Badawi

In argparse it is add_argument(). This method returns an Action object (actually a subclass of that depending the action parameter). You can access various parameters of that object, either to use, or even change. For example:

action1 = parser.add_argument(
    "-f",
    "--file",
    metavar = "FILE", 
    dest="file_name",
    default="foo.bar", 
    help="Name of the %(metavar)s to be loaded"
)
action2 = parser.add_argument(
    "-d",
    "--dir",
    metavar="DIR", 
    dest="file_dir",
    default=".", 
    help="Directory of %s if it is not current directory" % action1.metavar
)

print(action1.metavar)  # read the metavar attribute
action2.metvar = "DIRECTORY"  # change the metavar attribute

The help reads:

usage: ipython [-h] [-f FILE] [-d DIR]

optional arguments:
  -h, --help            show this help message and exit
  -f FILE, --file FILE  Name of the FILE to be loaded
  -d DIR, --dir DIR     Directory of FILE if it is not current directory

I removed action="store" since that is the default value (no big deal though).

I changed the help values to use %(metavar)s. This is used to incorporate various action attributes. Most commonly it is used for the default.

From the argparse docs:

The help strings can include various format specifiers to avoid repetition of things like the program name or the argument default. The available specifiers include the program name, %(prog)s and most keyword arguments to add_argument(), e.g. %(default)s, %(type)s, etc.:

I am using action1.metavar to place FILE in the help line for action2. It’s not a common usage, but I don’t see anything wrong with it.

Note that action1.metavar is used once when setting up the parser (to create the action2 help line), and then later when the help is formatted.

In [17]: action2.help
Out[17]: 'Directory of FILE if it is not current directory'    
In [18]: action1.help
Out[18]: 'Name of the %(metavar)s to be loaded'

py3 style formatting can be use for action2:

help2 = "Directory of {} if it is not current directory".format(action2.metavar)
action2.help = help2

but py2 style has to be used for action1. Unless you did:

action1.help = "Name of the {} to be loaded".format(action1.metavar)

After creating both actions you even could use:

action1.help = "Name of the {} to be loaded from {}".format(action1.metavar, action2.metavar)

But that’s just ordinary Python coding.

Answered By: hpaulj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.