I have a function:
def x(a,b,c)
How can I collect variable values from the command line that fit this pattern?
python test.py --x_center a --y_center b c (c has, for example, 3, 4 or more values )
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can do something like that like this:
Try it out:
To use the
argparsemodule, you’ll normally want to start with amainfunction (and some boilerplate that calls it). In themainfunction, you’ll want to create anArgumentParser. After that, you’ll want to add some arguments.To add an argument, you use
add_argument.Here, we’re adding an option,
-x, which also has a long option variant,--x-center. Thetypewe pass toadd_argumenttells it to require it to be afloat(and error if it’s not a valid float). We also tellargparsethat it’s required; if it’s not provided, error.This is just like before, but since the string we pass to it does not begin with a dash, it assumes it is not an option, but rather a non-option argument. Again, we tell it we want
floats.nargsallows you to specify that it takes more than one argument.*specifies that we want any amount of arguments.Finally, we parse the command line with
parse_args. This returns an object that we’ll store.You can then access the options and arguments on that
argsobject and do relevant things in your program.