I cannot understand this argparse behavior 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 result with and without the -host argument:
>> python demo.py Namespace(host='localhost') >> python demo.py --host host Namespace(host=['host'])
In particular: why is the "-host" argument stored in the list when it is specified, but not when the default value is used?
python command-line-arguments argparse
Schemer
source share