Python Forum
argparse --help in one line.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
argparse --help in one line.
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug argparse io issue pyDream 8 594 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Not able to figure out what is wrong with argparse radioactive9 31 8,474 Mar-16-2022, 07:50 PM
Last Post: deanhystad
  Argparse error when inputting values tqader 2 2,870 Sep-11-2020, 07:42 PM
Last Post: buran
  Why this pycharm warning for argparse formatter_class value? pjfarley3 2 2,116 Sep-09-2020, 05:23 AM
Last Post: pjfarley3
  Can argparse support undocumented options? pjfarley3 3 2,186 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  In ArgParse, can two compatible formatter_class values be used? pjfarley3 2 2,578 Jul-31-2020, 02:01 PM
Last Post: pjfarley3
  Why am I getting KeyError 'file' when using argparse? Mike Ru 1 3,067 Jun-09-2019, 04:48 PM
Last Post: metulburr
  How can I get some arguments using argparse? Mike Ru 0 1,870 Jun-05-2019, 12:57 PM
Last Post: Mike Ru
  argparse 2skywalkers 4 3,114 May-15-2019, 07:00 PM
Last Post: Gribouillis
  argparse and imported modules spatialdawn 2 5,438 Dec-01-2018, 12:35 PM
Last Post: spatialdawn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020