The argparse
module does not provide any option to add a "prolog". When the help is displayed it always start with usage:
. The best you can do is to customize the usage text adding the version number, using the usage
parameter when you instantiate the ArgumentParser
:
import argparse
parser = argparse.ArgumentParser(usage='Any text you want\n')
Note that the help will still start with usage:
.
A dirty workaround that might work is to start the usage
message with a \r
:
>>> import argparse
>>> usage = '\r{}\nusage: %(prog)s etc.'.format('Version a b'.ljust(len('usage:')))
>>> parser = argparse.ArgumentParser(usage=usage)
>>> parser.parse_args(['-h'])
Version a b
usage: etc.
optional arguments:
-h, --help show this help message and exit
I don't think that this usage of \r
is portable. There are probably some terminals where this trick doesn't work. I've ljust
ed the version string to make sure that when the trick works, the whole usage:
string disappears from string and you don't get things like v1.2e:
when using short version strings.
Note: you must manually create the whole usage
text now.