Real.Cryptc
Real.Cryptc

Reputation: 75

Formatting the ArgParse help menu

I am trying to create a more attractive menu for ArgParse mainly just consistent formatting when the help menu is displayed. I understand how to use various attributes to make something required or not, but am not sure how to format the text so that they are consistent.

My current code that is relevant is shown below

import argparse

parser = argparse.ArgumentParser(prog='myprogram', description='Select which files to parser and where to save them.')

parser.add_argument('-f', metavar='--file', type=str, help='select a single file to process', required=False)
parser.add_argument('-d', metavar='--dir', type=str, help='filepath directory', required=False)

args = parser.parse_args()

The output of this code is when running the command filename.py -h is Screen capture of terminal output

What I would like to do is simply add a comma between -f and --file as well as any additional commands. I understand this is fairly small detail, but what can I say I like to format things consistently.

I am running Python v3.8.2 on my machine and Argparse v1.4.0

Upvotes: 2

Views: 1863

Answers (1)

hpaulj
hpaulj

Reputation: 231738

This should illustrate the main help formatting possibilities (without subclassing the helpformatter):

In [1]: import argparse
In [2]: parser=argparse.ArgumentParser()
In [3]: parser.add_argument('-f','--foo',action='store_true', help='a store true
   ...: ');
In [4]: parser.add_argument('-b','--bar',help='basic store');
In [5]: parser.add_argument('-x','--xxx', metavar='',help='blank metavar');
In [6]: parser.print_help()
usage: ipython3 [-h] [-f] [-b BAR] [-x]

optional arguments:
  -h, --help         show this help message and exit
  -f, --foo          a store true
  -b BAR, --bar BAR  basic store
  -x , --xxx         blank metavar

Upvotes: 3

Related Questions