I have function like this -
def my_func(my_list_arg=["one", "two"]):
Which I'm trying to call from command line using python-fire
.
I have tried several ways -
Attempt 1:
fire_runner.py my_func [one, two]
Results in two separate arguments "[one"
and "two]"
.
Attempt 2:
fire_runner.py my_func ["one", "two"]
which has the same result.
Attempt 3:
fire_runner.py my_func [\"one\", \"two\"]
same result.
In Python, you can unpack list , tuple , dict (dictionary) and pass its elements to function as arguments by adding * to list or tuple and ** to dictionary when calling function.
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
Fire() The easiest way to use Fire is to take any Python program, and then simply call fire. Fire() at the end of the program. This will expose the full contents of the program to the command line.
Your list will first be parsed by the command line shell. This means you need to force it as a single argument. There are many ways to do this:
No space: [one,two]
Escape the space: [one,\ two]
Quotes: "[one, two]"
.
Note that you could modify your function to take a list of arguments using the following syntax:
def my_func(*args):
The name args
here is convention, but it is just a variable name, so it can be anything. This will allow you to call your function as
fire_runner.py my_func one two
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With