There are few ways you can do this: using nargs or using action=append:
import argparse
parser = argparse.ArgumentParser()
# You can specify number of elements in an array.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of elements to accept.
parser.add_argument('--nargs', nargs='+')
# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)
# Using `action=append`. But out must provide the flag for every
# input. And you can use type=int here as well.
parser.add_argument('--append-action', action='append')
# To show the results
for _, value in parser.parse_args()._get_kwargs():
if value is not None:
print(value)
And the results will look like this:
$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']
$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]
$ # Negative numbers are also handled
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]
$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
Reference: https://docs.python.org/3/library/argparse.html#nargs