Handle spaces in argparse input

后端 未结 5 1983
逝去的感伤
逝去的感伤 2020-12-28 12:52

Using python and argparse, the user could input a file name with -d as the flag.

parser.add_argument(\"-d\", \"--dmp\", default=None)

Howev

5条回答
  •  甜味超标
    2020-12-28 13:32

    For those who can't parse arguments and still get "error: unrecognized arguments:" I found a workaround:

    parser.add_argument('-d', '--dmp', nargs='+', ...)
    opts = parser.parse_args()
    

    and then when you want to use it just do

    ' '.join(opts.dmp)
    

提交回复
热议问题