Argparse positioning arguments with prefix

My python3 script works with the input and output file given on the command line. Usage should look like this:

xxxx.py [-h] --input=file --output=file

      

In the code I am using

parser.add_argument("input", help='Input file');
parser.add_argument("output", help='Output file');

      

but the arguments are without the required prefix. Is there a way to specify a prefix for each argument?

+3


source to share


1 answer


Just turn on the double dash:

parser.add_argument("--input", help='Input file');
parser.add_argument("--output", help='Output file');

      



Arguments are either positional or optional; arguments starting with --

are always optional. You cannot create prefixed positional arguments --

, and you really shouldn't. The prefix --

is a UI convention that you really don't want to break.

+5


source







All Articles