Python argparse: default value or specified value

后端 未结 3 826
我寻月下人不归
我寻月下人不归 2020-11-27 10:27

I would like to have a optional argument that will default to a value if only the flag is present with no value specified, but store a user-specified value instead of the de

3条回答
  •  萌比男神i
    2020-11-27 11:13

    The difference between:

    parser.add_argument("--debug", help="Debug", nargs='?', type=int, const=1, default=7)
    

    and

    parser.add_argument("--debug", help="Debug", nargs='?', type=int, const=1)
    

    is thus:

    myscript.py => debug is 7 (from default) in the first case and "None" in the second

    myscript.py --debug => debug is 1 in each case

    myscript.py --debug 2 => debug is 2 in each case

提交回复
热议问题