0

I'm using argparse to handle command-line arguments inside an application I'm developing. As it stands, everything works as advertised. However, I'm having an issue with the individual argument's help formatting.

optional arguments:
  -k API_KEY, --key API_KEY
                        API Key to use when querying/updating records
  -e EMAIL, --email EMAIL
                        Email to use when querying/updating records

In the help formatting above, I'm looking at how to solve the duplicate argument variable names (i.e. -k API_KEY, --key API_KEY). This seems redundant and leads to the help formatting being on multiple lines vs a single, continuous line. Here is the relevant code.

if __name__ == '__main__':
    parser=argparse.ArgumentParser(add_help=False)
    parser_required=parser.add_argument_group('Required Arguments')
    parser_required.add_argument('-k', '--key', dest='api_key', type=str, required=False, help='API Key to use when querying/updating records')
    parser_required.add_argument('-e', '--email', dest='email', type=str, required=False, help='Email to use when querying/updating records')

I'm not totally sure how I can achieve this, though. Here is the desired output.

optional arguments:
  -k, --key             API Key to use when querying/updating records
  -e, --email           Email to use when querying/updating records
Ryan Foley
  • 115
  • 2
  • 5
  • http://stackoverflow.com/questions/9366369/python-argparse-lots-of-choices-results-in-ugly-help-output discusses various options. Besides the simplest `metavar` solution, you can customize the `HelpFormatter` class. Suggestions on how to do that have been given in several SO questions. – hpaulj Aug 01 '16 at 01:43

1 Answers1

1

Just set metavar='' in each argument:

add_argument('-k', '--key', metavar='', ...)

Metavars are API_KEY and EMAIL in your example.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436