I am writing a program which processes all files in a predefined folder. Optionally, it shall process only certain specified files in a specified folder. For this I want to use an optional command line parameter -s
or --specify-files
.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--specify-files", type=str, metavar="FOLDER FILES", nargs="+",
help="Will process specified FILES in specified FOLDER. Specify at least one file. Folder path and each file name must be separated by space")
args = parser.parse_args("-s ~/custom/folder file1.txt file2.txt file34.txt".split())
print(args)
Output:
Namespace(select_files=['~/custom/folder', 'file1.txt', 'file2.txt', 'file34.txt'])
From here the program knows that args.select_files[0]
is the folder path and args.select_files[1:]
is a list of files to process.
Thanks to nargs="+"
the ArgumentParser warns if the user does not specify anything, but I wonder how I could error out and warn the user if he enters less than two arguments:
args = parser.parse_args("-s ~/custom/folder".split())
Current Output:
Namespace(specify_files=['~/custom/folder'])
Expected:
usage: minimal.py [-h] [-s [FOLDER FILES ...]]
minimal.py: error: argument -s/--specify-files: expected at least two arguments
For my program, anythin less than two arguments for -s
does not make sense. I could easily write a check for this and raise an error after parser
has parsed the arguments, but I'd prefer if the error would already be raised before parser.parse_args
outputs the Namespace object. Something like a nargs="2+"
would be convenient here, if you know what I mean.