Why do optional arguments appear before positional arguments in the help output from argparse?

55 Views Asked by At

Code:

import argparse
import sys
from fontbro import Font

def get_font_format(font_path, ignore_flavor):
    """
    Gets the font format: otf, ttf, woff, woff2.

    :param font_path: Path to the font file.
    :param ignore_flavor: If True, the original format without compression will be returned.
    :returns: The format.
    :rtype: str or None
    """
    font = Font(font_path)
    return font.get_format(ignore_flavor=ignore_flavor)

def main():
    parser = argparse.ArgumentParser(description='Get the font format of a font file.')
    parser.add_argument('font_path', type=str, help='The path to the font file')
    parser.add_argument('--ignore_flavor', action='store_true', help='Return original format without compression if set to True')
    # Print help if no arguments supplied
    if len(sys.argv)==1:
        parser.print_help(sys.stderr)
        sys.exit(1)
    args = parser.parse_args()
    
    # Get and print the font format
    format = get_font_format(args.font_path, args.ignore_flavor)
    print(format)

if __name__ == "__main__":
    main()

If I run the script without any arguments I get help/usage:

usage: FontOpsGetFontFormat.py [-h] [--ignore_flavor] font_path

Get the font format of a font file.

positional arguments:
  font_path        The path to the font file

options:
  -h, --help       show this help message and exit
  --ignore_flavor  Return original format without compression if set to True

Why isn't the usage info formatted like this instead?

usage: FontOpsGetFontFormat.py font_path [-h] [--ignore_flavor]

From my limited understanding, positional arguments come first and others come after - is this incorrect?

0

There are 0 best solutions below