0

Why when I do a simple store, does it goof up the help menu? It's adding the all caps destination for some reason and I dont know how to turn it off. Below you can see the difference. The first is using store_const which doesn't goof up the help menu, but of course doesn't suit my needs. The second is the simple store. See the difference?

Constant Store

dev@taco:~/argparse$ python3 arg-0.0.1.py -h
usage: arg-0.0.1.py [-h] [-s] [-e] [-v]

Example list of options

optional arguments:
  -h, --help     show this help message and exit
  -s, --start    enter the starting value
  -e, --end      enter the ending value
  -v, --version  show program's version number and exit

Simple Store

dev@taco:~/argparse$ python3 arg-0.0.1.py -h
usage: arg-0.0.1.py [-h] [-s START] [-e END] [-v]

Example list of options

optional arguments:
  -h, --help            show this help message and exit
  -s START, --start START
                        enter the starting value
  -e END, --end END     enter the ending value
  -v, --version         show program's version number and exit

Here is the actual code for both simple and constant:

#! /usr/bin/env python3
import argparse
parser = argparse.ArgumentParser(description='Example list of options', add_help=True)
parser.add_argument('-s', '--start', dest='start', action='store_const', const='1', help='enter the starting value')
parser.add_argument('-e', '--end', dest='end', action='store_const', const='1000', help='enter the ending value')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.0.1')
results = parser.parse_args()
print ('Starting value = ', results.start)
print ('Ending value = ', results.end)

#! /usr/bin/env python3
import argparse
parser = argparse.ArgumentParser(description='Example list of options', add_help=True)
parser.add_argument('-s', '--start', dest='start', action='store', help='enter the starting value')
parser.add_argument('-e', '--end', dest='end', action='store', help='enter the ending value')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.0.1')
results = parser.parse_args()
print ('Starting value = ', results.start)
print ('Ending value = ', results.end)

Perhaps this is just expected and the reason it is adding START and END is to tell the user that the flag expects an argument. Its just a little confusing. Also, since it word wraps the menu, it makes it difficult to read at first.

How can I clean up the menu? Perhaps suggest a way to increase the size of the help menu to fix the word wrap issue first and then if possible, maybe also a way to change START and END to something else that is easier to understand. Maybe <value>?

6
  • I've tried to adjust the wording here, to move away from (what appears to be) a naked request for someone to take over your work (Can anyone help me clean the menu up please?). It's worth remembering that most readers here are volunteers, and thus your expectation should be that people will give you clues and ideas for you to implement yourself. Occasionally you will have someone do a large chunk of work for you, which is a lovely surprise, but it should never be requested or expected. Commented Apr 3, 2018 at 19:45
  • "Perhaps this is just expected and the reason it is adding START and END is to tell the user that the flag expects an argument." - yes, that is why it's doing that. Otherwise, the option will look like it doesn't take an argument. Commented Apr 3, 2018 at 19:46
  • The display width is taken from os.environ['COLUMNS'] Commented Apr 3, 2018 at 20:00
  • Use a metavar='<value>' parameter to change the default uppercase form. docs.python.org/3/library/argparse.html#metavar Commented Apr 3, 2018 at 20:03
  • Yup I got it! See below and thanks for responding! Commented Apr 3, 2018 at 20:04

1 Answer 1

1

The string is indeed a placeholder for an expected argument, and it is called a metavariable. The string used is controlled by the metavar keyword argument to add_argument. The default (None) specifies the destination name in all caps. Use the empty string to suppress it, or pass any other string (such as '<value>') to change it.

parser.add_argument('-s', '--start',
                     dest='start',
                     action='store',
                     metavar='',
                     help='enter the starting value')
Sign up to request clarification or add additional context in comments.

4 Comments

Just found this on Google too. Thank you for the reply! Testing it now.
That def solves the expected argument issue. The help menu is still trying to word wrap though. Is there no way to increase the width of the table a tiny bit to prevent this? Thanks!
The pre-defined formatter classes all take a width parameter to specify how wide the help text should be (default is 80). I don't recall exactly how you make use of that, though. It's not terribly well documented.
Looks like I need to use RawDescriptionHelpFormatter. Trying to read up on it and find some examples to help format the help menu.

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.