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?
Remove the "nargs" keyword argument. Once that argument is defined argparse assumes your argument is a list (nargs=1 meaning a list with 1 element)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With