1

I am working on a small Python program that needs to get some command line parameters and use argparse to display usage message . I have these 2 lines

parser.add_argument("-r",type=int,default=1)
parser.add_argument("-c",type=int,default=2)

And the requirement is that I show the user this message :

*usage: myprogram.py [-h]  [-r ROWS] [-c COLUMNS]* 

However what I show the user is -

*usage: myprogram.py [-h] [-r R] [-c C]*

How can I turn [-r R] into [-r ROW] (and in the same manner [-c C] to [-c COLUMNS])?

I have looked at the argsparse docs quite a bit with no avail...

1 Answer 1

5

Use the metavar parameter:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-r", type=int, default=1, metavar='ROWS')
parser.add_argument("-c", type=int, default=2, metavar='COLUMNS')
args = parser.parse_args()

Then, python test.py -h yields

usage: test.py [-h] [-r ROWS] [-c COLUMNS]

optional arguments:
  -h, --help  show this help message and exit
  -r ROWS
  -c COLUMNS
Sign up to request clarification or add additional context in comments.

1 Comment

Yep ..that does it . Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.