python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

Strings args do not need to be parsed.

Open hxse opened this issue 2 years ago • 3 comments

def test(a, b):
    print(a, b)

if __name__ == "__main__":
    fire.Fire(test)

python .\test.py 1 '2 3' 1 2 3

python .\test2.py 1 '""2 3""' 1 2 3

python .\test.py 1 '--2 3' ERROR: The function received no value for the required argument: b

python .\test.py 1 '\"2 3\"'

1 "2
ERROR: Could not consume arg: 3"

python .\test.py 1 '\"\"2 3\"\"' 1 ""2 3""

The correct behavior should be equivalent to print(1, "2 3"), so strings args do not need to be parsed.

hxse avatar Jul 11 '23 09:07 hxse

I guess not parsing string args will also fix the following problem, right?

I have with the following code:

def preprocess(data_path: str = "data.json", output_format: str):
...

if __name__ == "__main__":
    fire.Fire(preprocess)

If I call python preprocess.py --data_path foo.json --output_format "{response}", I get the error AttributeError: 'set' object has no attribute 'format' because type(output_format) = set and not str.

DavidFarago avatar Jul 25 '23 14:07 DavidFarago

I also encountered a similar issue when passing a string starting with "-" , but it can be passed by explicitly specifying the parameter name, for example:

import fire

def test(a, b):
    print(a, b)
    print(type(a), type(b))

if __name__ == "__main__":
    fire.Fire(test)
python ./test.py -a=1 -b='--2 3'
1 --2 3
<class 'int'> <class 'str'>

Dragon-Git avatar Sep 30 '23 07:09 Dragon-Git

I would like to pick this one up can it be assigned to me?

pioneerHitesh avatar Oct 17 '23 08:10 pioneerHitesh