Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

argparse required argument list competes with optional argument list

I have a Python program with argparse. The required arguments are list of filenames, and optional are a list of strings that are to be removed from all files.

import argparse

parser = argparse.ArgumentParser("File formatter")
parser.add_argument("filename", type=str, nargs="+")

parser.add_argument("-r", "--remove", metavar="STRING", nargs="+")

args = parser.parse_args()
print(args.filename)
print(args.remove)

The above gives

usage: filefmt [-h] [-r STRING [STRING ...]] filename [filename ...]

The problem: In a call like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

filefmt -r str1x gg24 a.txt b.dat

the program has no way of knowing which of the arguments is a remove STRING and which a filename. I’d have to call it like this

filefmt a.txt b.dat -r str1x gg24

but sometimes, environments require me that the filenames come last.

Any idea how to fix this? I’d like to avoid having to prefix the filenames with an option string, this is the default use:

filefmt a.txt b.txt

>Solution :

To resolve the ambiguity you’ll have to require the -r option for each STRING

For example:

import argparse

parser = argparse.ArgumentParser("File formatter")
parser.add_argument("filename", type=str, nargs="+")

parser.add_argument("-r", "--remove", metavar="STRING", default=[], action='append', help='string to remove (repeat this option for each string)')

args = parser.parse_args()
print(args.filename)
print(args.remove)

Run like this:

args1.py -r a -r b qwe asd zcx

output:

['qwe', 'asd', 'zcx']
['a', 'b']

The usage doesn’t show you can repeat the -r so I added that in the help for -r:

usage: File formatter [-h] [-r STRING] filename [filename ...]

positional arguments:
  filename

optional arguments:
  -h, --help            show this help message and exit
  -r STRING, --remove STRING
                        string to remove (repeat this option for each string)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading