How can I use python's argparse with a predefined argument string?

前端 未结 4 1632
星月不相逢
星月不相逢 2020-12-09 09:23

I want to use the pythons argparse module to parse my cli parameter string. This works for the parameters a pass from terminal, but not with a given string.

         


        
4条回答
  •  一整个雨季
    2020-12-09 09:46

    parser.parse_args() expects a sequence in the same form as sys.argv[1:]. If you treat a string like a sys.argv sequence, you get ['s', 'o', 'm', 'e', 'T', 'e', 's', 't', 'F', 'i', 'l', 'e']. 's' becomes the relevant argument, and then the rest of the string is unparseable.

    Instead, you probably want to pass in parser.parse_args(['someTestFile'])

提交回复
热议问题