How to pass and parse a list of strings from command line with argparse.ArgumentParser in Python?

You need to define --names-list to take an arbitrary number of arguments.

parser.add_argument('-n', '--names-list', nargs="+", default=[])

Note that options with arbitrary number of arguments don’t typically play well with positional arguments, though:

# Is this 4 arguments to -n, or
# 3 arguments and a single positional argument, or ...
myprog.py -n a b c d

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)