I would create an argument for each of the fruits, but I would do it in the DRYest way I could:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--insecticide")
for fruit, nargs in (
('Apple', 10),
('Banana', 7),
('Orange', 9),
):
parser.add_argument(
"--" + fruit.lower(), nargs=nargs, metavar='FLIES',
help="specify {} species of {} pests".format(nargs, fruit))
args = parser.parse_args()
print(args)
Here is the resulting help message:
$ python x.py -h
usage: x.py [-h] [-i INSECTICIDE]
[--apple FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES]
[--banana FLIES FLIES FLIES FLIES FLIES FLIES FLIES]
[--orange FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES]
optional arguments:
-h, --help show this help message and exit
-i INSECTICIDE, --insecticide INSECTICIDE
--apple FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES
specify 10 species of Apple pests
--banana FLIES FLIES FLIES FLIES FLIES FLIES FLIES
specify 7 species of Banana pests
--orange FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES FLIES
specify 9 species of Orange pests
If there were a variable number of fruits (controlled by an environment variable, or the presence of configuration files, for example), then my loop wouldn't be hardcoded, but instead it might be:
for config_section in config_data():
parser.add_argument("--" + config_section.name, ...)
If I wanted to name the parameters sequentially, as OP's comment suggests, I might code the loop:
for i, fruit in enumerate(fruit_names, 1):
parser.add_argument("--x{}".format(i),
nargs="+",
help="{} files".format(fruit))
And here is the resulting help message:
$ python x2.py -h
usage: x2.py [-h] [-i INSECTICIDE] [--x1 X1 [X1 ...]] [--x2 X2 [X2 ...]]
[--x3 X3 [X3 ...]]
optional arguments:
-h, --help show this help message and exit
-i INSECTICIDE, --insecticide INSECTICIDE
--x1 X1 [X1 ...] Apple files
--x2 X2 [X2 ...] Banana files
--x3 X3 [X3 ...] Orange files
python script.py -x1 a b c -x2 d e f -x3 g h i? Except that I could keep going until x100, x500, x10000000, etc.fruits/flags? And who wants to type in, via commandline, 50 fruits with 10 file names each?