Option accepted with and without value
问题 I have a small script and I need it to be able to accept parameter with value and withou value. ./cha.py --pretty-xml ./cha.py --pretty-xml=5 I have this. parser.add_argument('--pretty-xml', nargs='?', dest='xml_space', default=4) But when I use --pretty-xml in xml_space will be 'none'. If I dont write this parameter in xml_space is stored the default value. I would need the exact opposite. 回答1: Leave out the default parameter and use a custom Action instead: class PrettyXMLAction(argparse