Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse: name parameters

I'm writing a program that use argparse, for parsing some arguments that I need.

for now I have this:

parser.add_argument('--rename', type=str, nargs=2, help='some help')

when I run this script I see this:

optional arguments:   -h, --help            show this help message and exit   --rename RENAME RENAME                         some help 

How can I change my code in that way that the help "page" will show me:

--rename OLDFILE NEWFILE 

Can I then use OLDFILE and NEWFILE value in this way?

args.rename.oldfile args.rename.newfile 
like image 491
Wolfy Avatar asked Aug 22 '13 09:08

Wolfy


People also ask

What is action =' Store_true in Python?

parser.add_argument('-o', '--output', action='store_true', help="shows output") An argument is added with add_argument . The action set to store_true will store the argument as True , if present. The help option gives argument help. args = parser.parse_args()

How do you add arguments in Argparse?

To add your arguments, use parser. add_argument() . Some important parameters to note for this method are name , type , and required . The name is exactly what it sounds like — the name of the command line field.

What is ArgumentParser in Python?

argparse — parse the arguments. Using argparse is how you let the user of your program provide values for variables at runtime. It's a means of communication between the writer of a program and the user. That user might be your future self.

What is Metavar in Python?

Metavar: It provides a different name for optional argument in help messages.


2 Answers

If you set metavar=('OLDFILE', 'NEWFILE'):

import argparse parser = argparse.ArgumentParser() parser.add_argument('--rename', type=str, nargs=2, help='some help',                     metavar=('OLDFILE', 'NEWFILE')) args = parser.parse_args() print(args) 

Then test.py -h yields

usage: test.py [-h] [--rename OLDFILE NEWFILE]  optional arguments:   -h, --help            show this help message and exit   --rename OLDFILE NEWFILE                         some help 

You can then access the arguments with

oldfile, newfile = args.rename 

If you really want to access the oldfile with args.rename.oldfile you could set up a custom action:

import argparse class RenameAction(argparse.Action):     def __call__(self, parser, namespace, values, option_string=None):         setattr(namespace, self.dest,                 argparse.Namespace(                     **dict(zip(('oldfile', 'newfile'),                                values))))  parser = argparse.ArgumentParser() parser.add_argument('--rename', type=str, nargs=2, help='some help',                     metavar=('OLDFILE', 'NEWFILE'),                     action=RenameAction) args = parser.parse_args()  print(args.rename.oldfile) 

but it extra code does not really seem worth it to me.

like image 62
unutbu Avatar answered Sep 29 '22 05:09

unutbu


Read the argparse documentation (http://docs.python.org/2.7/library/argparse.html#metavar):

Different values of nargs may cause the metavar to be used multiple times. Providing a tuple to metavar specifies a different display for each of the arguments:

>>> parser = argparse.ArgumentParser(prog='PROG') >>> parser.add_argument('-x', nargs=2) >>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz')) >>> parser.print_help() usage: PROG [-h] [-x X X] [--foo bar baz]  optional arguments:  -h, --help     show this help message and exit  -x X X  --foo bar baz 
like image 32
njzk2 Avatar answered Sep 29 '22 06:09

njzk2