Reorder Python argparse argument groups

Logan

I'm using argparse and I have a custom argument group required arguments. Is there any way to change the order of the argument groups in the help message? I think it is more logical to have the required arguments before optional arguments, but haven't found any documentation or questions to help.

For example, changing this:

usage: foo.py [-h] -i INPUT [-o OUTPUT]

Foo

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                      Output file name

required arguments:
  -i INPUT, --input INPUT
                      Input file name

to this:

usage: foo.py [-h] -i INPUT [-o OUTPUT]

Foo

required arguments:
  -i INPUT, --input INPUT
                      Input file name

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                      Output file name

(example taken from this question)

taras

You might consider adding an explicit optional arguments group:

import argparse

parser = argparse.ArgumentParser(description='Foo', add_help=False)

required = parser.add_argument_group('required arguments')
required.add_argument('-i', '--input', help='Input file name', required=True)

optional = parser.add_argument_group('optional arguments')
optional.add_argument("-h", "--help", action="help", help="show this help message and exit")
optional.add_argument('-o', '--output', help='Output file name', default='stdout')

parser.parse_args(['-h'])

You can move the help action to your optional group as described here: Move "help" to a different Argument Group in python argparse

As you can see, the code produces the required output:

usage: code.py -i INPUT [-h] [-o OUTPUT]

Foo

required arguments:
  -i INPUT, --input INPUT
                        Input file name

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output file name

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python argparse groups confusion

From Dev

Python argparse : How can I get Namespace objects for argument groups separately?

From Dev

Python argparse : How can I get Namespace objects for argument groups separately?

From Dev

Using python argparse on repeating groups

From Dev

Accessing argument values for argparse in Python

From Dev

Understanding argument parsing with argparse in Python

From Dev

Argument parsing python using ArgParse

From Dev

argparse with argument_groups and mututally_exclusive_group

From Dev

python argparse add_argument_group required

From Dev

Multiple files for one argument in argparse Python 2.7

From Dev

How to skip a positional argument in Python Argparse

From Dev

Use argparse module in python to parse a single argument

From Dev

python-argparse: assign a choice to each argument

From Dev

Python argparse: Complex Argument Parsing Scenario

From Dev

Python argparse: default argument stored as string, not list

From Dev

How to access a python argparse argument with a dot in the name

From Dev

Python argparse set 1 or more parameters to an argument

From Dev

How to access a python argparse argument with a dot in the name

From Dev

How to skip a positional argument in Python Argparse

From Dev

python argparse with mandatory input file argument

From Dev

Python argparse accept variable argument (even flags)

From Dev

ArgParse Python Module: Change default argument value for inherted argument

From Dev

How can I easily create a python argparse argument with an inverse?

From Dev

How to do a Python argparse mutually required argument group

From Dev

Python argparse : mutually exclusive arguments with optional and positional argument

From Dev

How to make a short and long version of a required argument using Python Argparse?

From Dev

Python argparse - Mutually exclusive group with default if no argument is given

From Dev

Python's argparse: How to use keyword as argument's name

From Dev

How have a variable number of parameters for one argument in Python with argparse?

Related Related

  1. 1

    Python argparse groups confusion

  2. 2

    Python argparse : How can I get Namespace objects for argument groups separately?

  3. 3

    Python argparse : How can I get Namespace objects for argument groups separately?

  4. 4

    Using python argparse on repeating groups

  5. 5

    Accessing argument values for argparse in Python

  6. 6

    Understanding argument parsing with argparse in Python

  7. 7

    Argument parsing python using ArgParse

  8. 8

    argparse with argument_groups and mututally_exclusive_group

  9. 9

    python argparse add_argument_group required

  10. 10

    Multiple files for one argument in argparse Python 2.7

  11. 11

    How to skip a positional argument in Python Argparse

  12. 12

    Use argparse module in python to parse a single argument

  13. 13

    python-argparse: assign a choice to each argument

  14. 14

    Python argparse: Complex Argument Parsing Scenario

  15. 15

    Python argparse: default argument stored as string, not list

  16. 16

    How to access a python argparse argument with a dot in the name

  17. 17

    Python argparse set 1 or more parameters to an argument

  18. 18

    How to access a python argparse argument with a dot in the name

  19. 19

    How to skip a positional argument in Python Argparse

  20. 20

    python argparse with mandatory input file argument

  21. 21

    Python argparse accept variable argument (even flags)

  22. 22

    ArgParse Python Module: Change default argument value for inherted argument

  23. 23

    How can I easily create a python argparse argument with an inverse?

  24. 24

    How to do a Python argparse mutually required argument group

  25. 25

    Python argparse : mutually exclusive arguments with optional and positional argument

  26. 26

    How to make a short and long version of a required argument using Python Argparse?

  27. 27

    Python argparse - Mutually exclusive group with default if no argument is given

  28. 28

    Python's argparse: How to use keyword as argument's name

  29. 29

    How have a variable number of parameters for one argument in Python with argparse?

HotTag

Archive