Reputation: 867
I'm using subprocess
to call command line. For example,
subprocess.check_output(["echo", "Hello World!"])
This is for one argument. I want to call an executable called "myFuntion" and pass several arguments to it, by their names. The command line I would have written is:
./myFunction --arg1 a1 --arg2 a2
How do I "translate" this?
Upvotes: 2
Views: 39
Reputation: 365717
Simple: each argument is a separate element in the list.
So, if you understand how the shell would separate out this command line:
./myFunction --arg1 a1 --arg2 a2
… you know what the arguments are. When there are no quotes or escapes or anything else fancy, the shell just separates on spaces. So this is just 5 arguments:
subprocess.check_output(["./myFunction", "--arg1", "a1", "--arg2", "a2"])
If you have a command line that you're not sure how to translate, you can ask your shell to do it for you… or just use the shlex
module in Python:
>>> print(shlex.split('./myFunction --arg1 a1 --arg2 a2'))
['./myFunction', '--arg1', 'a1', '--arg2', 'a2']
Upvotes: 4