How to Customize the New 2.7 ArgumentParser Library Class (module argparse)?

Peter Otten __peter__ at
Fri Aug 20 22:19:42 CEST 2010

Tom Browder wrote:

> I have converted from OptionParser to ArgumentParser (new in version
> 2.7) to great advantage, and I think it's a great improvement!  But
> now I want to customize the help formatting just a bit.
> The documentation is sketchy here, but I started by subclassing
> ArgumentParser and attempted to redefine format_usage, but after
> looking at the detailed instructions with pydoc I see "the API of the
> formatter objects is still considered an implementation detail."
> I take that to mean I should abandon such efforts at the moment.
> So, using the defined class as is, and following instructions in the
> online docs, I have been able to get my output to look like this:
> ==============>
> $ -h
> usage: [options]
> A program to manipulate user programs.
> optional arguments:
>   -h, --help      Show this help message and exit.
>   -v, --version  Show program's version number and exit.
>   -s, --show     Show a list of user post-processing programs.
> Version 2010-08-20.01
> <==============
> To beautify things I would like to change two strings which are
> auto-generated by the standard parser:
> 1.  change "usage:"
> to  "Usage:"   # capitalize 'usage'
> 2.  change "optional arguments:"
> to "Optional arguments:"  # capitalize 'Optional'
> Does anyone know how to do it without modifying source code?

You can use internationalization via

or take a shortcut:

$ cat
lookup = {
    "usage: ": "Usage: ",
    "optional arguments": "Optional arguments"}

def gettext(s):
    return lookup.get(s, s)

import argparse
argparse._ = gettext

if __name__ == "__main__":
    p = argparse.ArgumentParser()

$ python -h
Usage: [-h]

Optional arguments:
  -h, --help  show this help message and exit


More information about the Python-list mailing list