specifying a list as a command line argument in python

前端 未结 6 1798
滥情空心
滥情空心 2020-12-03 13:46

I am using getopt to process a command line optional argument, which should accept a list. Something like this:

foo.py --my_list=[1, 2, 3, 4,5] 
         


        
6条回答
  •  佛祖请我去吃肉
    2020-12-03 14:28

    From the python optparse help page: "

    parser.add_option("-f")
    parser.add_option("-p", type="float", nargs=3, dest="point")
    

    As it parses the command line

    -f foo.txt -p 1 -3.5 4 -fbar.txt
    

    optparse will set

    options.f = "foo.txt"
    options.point = (1.0, -3.5, 4.0)
    options.f = "bar.txt"
    

    "

提交回复
热议问题