specifying a list as a command line argument in python

前端 未结 6 1786
滥情空心
滥情空心 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:04

    Maybe you should just enclose the argument in quotes?

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

    Otherwise every space will be treated as a separator for arguments.

    0 讨论(0)
  • 2020-12-03 14:10

    If I can't use a standard parser (optparse or argparse) to my application then I use the ast.literal_eval function to parse input arguments of type list as follows:

    import sys, ast
    
    inputList = ast.literal_eval( sys.argv[1] )
    print type( inputList )
    print inputList
    

    Let suppose that this code is stored in testParser.py file. By executing the script:

    $ python testParser.py  "[1,2,3,4, [123, 456, 789], 'asdasd']"
    

    we get the following output:

    <type 'list'>
    [1, 2, 3, 4, [123, 456, 789], 'asdasd']
    

    So, using the secure enough ast.literal_eval function and inserting the list as a string of code we have the desirable result.

    Useful links:

    Using python's eval() vs. ast.literal_eval()?

    http://docs.python.org/2/library/functions.html?highlight=eval#eval

    0 讨论(0)
  • 2020-12-03 14:15

    An updated way is to use the argparse library and add the list as a command line argument. This way, you don't have to do any manual parsing.

    Example:

    parser = argparse.ArgumentParser()
    parser.add_argument(
            "-values",
            nargs="*",  # expects ≥ 0 arguments
            type=int,
            default=[35, 40, 50, 60, 70, 80, 90],  # default list if no arg value
        )
    

    Which you would then call like this:

    python3 someprogram.py -values 1 2 3
    
    0 讨论(0)
  • 2020-12-03 14:22

    There are two options that I can think of:

    • Use optparse, and use append action to specify what you want to do as: foo.py --my_list=1 --my_list=2 ....
    • Specify your commandline as foo.py --my_list='1,2,3,4,5', and then use x.split(',') to get your values in a list. You can use getopt or optparse for this method.

    The advantage of the first method is that you can get integer values in the list directly, at the expense of the commandline being longer (but you can add a single-charecter option for --my_list if you want). The advantage of the second is shorter command line, but after the split(), you need to convert the string values '1', '2', etc., to integers (pretty easy as well).

    0 讨论(0)
  • 2020-12-03 14:24

    I have been redirected from the dup question:
    How to pass an array to python through command line - Stack Overflow

    I’m answering that question:
    The arguments on the command line are strings, they're not parsed like literals in the program.
    argv construct the strings automatically to a list from command line arguments (as separated by spaces), in short, sys.argv is a list.

        from sys import argv
        cmd = "arr = {sys.argv[1]}".format(argv)
        exec(cmd)
        print(arr[2])
    
    0 讨论(0)
  • 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"
    

    "

    0 讨论(0)
提交回复
热议问题