If "'required' is an invalid argument for positionals" appears in Python's argparse, you can delete required.

Note that the document was a little confusing.

Argparse module that can conveniently handle startup arguments

Python has an argparse module that can conveniently handle startup arguments.

Positional and optional arguments

There are the following two types of arguments handled by argparse.

--Position arguments (such as infile1) --Optional arguments (such as -f and --bar) (= flag) (It is written in official document argparse "name or flags")

add_argument () method

You can specify what arguments you can specify with the ʻadd_argument ()` method, but because positional arguments are required

argument.add_argument(
    'infile1',
    required=True,
    help='input file'
)

If you write like this, you will get an error "'required' is an invalid argument for positionals "and you will not be able to execute it.

" Required "can be specified only as an optional argument

Since " required "is specified for an optional argument, an error will occur if it is specified for a positional argument. Originally, the positional argument is required, so if you delete the corresponding option "required = True", the expected behavior will be obtained.

Recommended Posts

If "'required' is an invalid argument for positionals" appears in Python's argparse, you can delete required.