Python Forum

Full Version: argparse --help in one line.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

Following is the part of my code:
help_msg = 'Encode video file(s) recursively to HEVC 10Bit.'                        
parser = argparse.ArgumentParser(description=help_msg)                              
parser.add_argument('-l', '--list', nargs='?', const=PWD, metavar='PATH', help='List all video files.')
parser.add_argument('-c', '--hevc', nargs='?', const=PWD, metavar='PATH', help='List all HEVC video files.')
parser.add_argument('-x', '--other', nargs='?', const=PWD, metavar='PATH', help='List all video files other than HEVC.')
args = parser.parse_args()
and this is the output:
Output:
$ hvc -h usage: hvc [-h] [-l [PATH]] [-c [PATH]] [-x [PATH]] Encode video file(s) recursively to HEVC 10Bit. optional arguments: -h, --help show this help message and exit -l [PATH], --list [PATH] List all video files. -c [PATH], --hevc [PATH] List all HEVC video files. -x [PATH], --other [PATH] List all video files other than HEVC.
I want the 'optional arguments' part of help output be printed in the same line. Like this:
Output:
optional arguments: -h, --help show this help message and exit -l [PATH], --list [PATH] List all video files. -c [PATH], --hevc [PATH] List all HEVC video files. -x [PATH], --other [PATH] List all video files other than HEVC.
How do I make it?

Thanks
import argparse
import os

PWD = os.getcwd()
help_msg = 'Encode video file(s) recursively to HEVC 10Bit.'

parser = argparse.ArgumentParser(description=help_msg,
    formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=30))
parser.add_argument('-l', '--list', nargs='?', const=PWD, metavar='PATH', help='List all video files.')
parser.add_argument('-c', '--hevc', nargs='?', const=PWD, metavar='PATH', help='List all HEVC video files.')
parser.add_argument('-x', '--other', nargs='?', const=PWD, metavar='PATH', help='List all video files other than HEVC.')
args = parser.parse_args()

parser.print_help()
help(parser.formatter_class)
Output:
Help on class HelpFormatter in module argparse: class HelpFormatter(builtins.object) | HelpFormatter(prog, indent_increment=2, max_help_position=24, width=None) | | Formatter for generating usage messages and argument help strings. | | Only the name of this class is considered a public API. All the methods | provided by the class are considered an implementation detail.
You could also write your own HelpFormatter class.

Personally I don't mind the extra lines and would prefer that all help use the same formatting.