Python argparse print usage text after description

Is there a way to print usage text after description text using python argparse? I have a cmd line desktop, but I want to print version information before use.

Edit:

version: 1.0 usage: blahcmd [-h] [-help] some lovely help 
+7
python argparse
source share
3 answers

The argparse module argparse not provide any option to add a prolog. When help is displayed, it always starts with usage: The best you can do is customize the usage text by adding the version number using the usage parameter when creating the ArgumentParser instance:

 import argparse parser = argparse.ArgumentParser(usage='Any text you want\n') 

Please note that help will begin with usage:

The wrong workaround that might work is to start the usage message with \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 ab usage: etc. optional arguments: -h, --help show this help message and exit 

I don't think this use of \r portable. There are probably some terminals where this trick does not work. I ljust edited the version line to make sure that when the trick works, the whole line usage: disappears from the line and you don't get things like v1.2e: when using short version lines.

Note. Now you must manually create all the usage text.

+2
source share

Here is an ugly hack (see my comment on the original question):

Define your own HelpFormatter subclass to navigate to the parser using the formatter_class parameter. The subclass should probably override the _format_usage method. This is not entirely recommended, as the interface for defining your own formatting class has never been published.

 from argparse import ArgumentParser, HelpFormatter from gettext import gettext as _ class VersionedHelp(HelpFormatter): def _format_usage(self, usage, actions, groups, prefix=None): if prefix is None: prefix = _('Version: xy\n\nusage: ') return HelpFormatter._format_usage(self, usage, actions, groups, prefix) p = ArgumentParser(formatter_class=VersionedHelp) p.parse_args() 
+4
source share

A tough solution is to add version text to your usage string. This is not ideal (note the additional usage text), but its beginning

 In [64]: parser=argparse.ArgumentParser(description='description') # 'usage' parameter just sets the 'usage' attribute In [67]: parser.usage='version 1.0.1\n'+parser.format_usage() In [68]: parser.print_help() usage: version 1.0.1 usage: ipython [-h] description optional arguments: -h, --help show this help message and exit 

The order of the components in help is determined by the ArgumentParser.format_help method (quoted from the argparse.py file):

 def format_help(self): formatter = self._get_formatter() # usage formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) # description formatter.add_text(self.description) # positionals, optionals and user-defined groups for action_group in self._action_groups: formatter.start_section(action_group.title) formatter.add_text(action_group.description) formatter.add_arguments(action_group._group_actions) formatter.end_section() # epilog formatter.add_text(self.epilog) # determine help from format above return formatter.format_help() 

I can imagine how to write a custom method that adds your version information, for example.

 def format_help(self): formatter = self._get_formatter() # version info formatter.add_text('version 1.0.1') # usage formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) ... 

In ipython this function works:

 In [74]: def format_help(parser): formatter=parser._get_formatter() formatter.add_text('version 1.0.1') formatter.add_usage(parser.usage, parser._actions, parser._mutually_exclusive_groups) formatter.add_text(parser.description) return formatter.format_help() In [75]: print format_help(parser) version 1.0.1 usage: ipython [-h] description 
+1
source share

All Articles