I cannot figure out this behaviour of argparse from the documentation:
import argparse
parser.add_argument("--host", metavar="", dest="host", nargs=1, default="localhost", help="Name of host for database. Default is 'localhost'.")
args = parser.parse_args()
print(args)
Here is the output with and without an argument for "--host":
>> python demo.py
Namespace(host='localhost')
>> python demo.py --host host
Namespace(host=['host'])
In particular: why does the argument to "--host" get stored in a list when it is specified but not when the default is used?
nargs=1. Whennargsis present, because you can setnargsto+or a larger number, the results are stored in alist. But the default is given as a string. You can writedefault=["localhost"]and the default will be a list as well.defaultis added to the namespace as is (apart from any conversion that thetypemight do).nargsandactiondon't, for the most part, affect it.