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

arg parsing truncates at hash

Open mgdelmonte opened this issue 4 years ago • 4 comments

Fire's arg parser truncates strings with hashes (#) in them. Simple test:

test.py:

import fire
def test(a): print(a, type(a))
fire.Fire(test)
> python test.py hi#there
hi <class 'str'>
> python test.py "hi#there"
hi <class 'str'>

If this is by design, it doesn't appear to be documented anywhere.

mgdelmonte avatar May 04 '21 18:05 mgdelmonte

This is an unintended side-effect of our reliance on the python ast when parsing inputs: https://github.com/google/python-fire/blob/c1266d0dbb2114514fcf8be62044344b5a51c733/fire/parser.py#L77

As a workaround for now, you can add an extra layer of quotes. python test.py '"hi#there"'

I agree we should improve this behavior.

dbieber avatar Jun 07 '21 14:06 dbieber

@dbieber hey can we json.dumps the value in _LiteralEval, then we don't have to give parameter in this manner, '"hi#there"'

sidshrivastav avatar Jun 23 '22 22:06 sidshrivastav

I don't think that will work, unfortunately. If the input is "1", json.dumps will produce '"1"' which will get parsed as a string, whereas an int is desired.

dbieber avatar Jun 23 '22 23:06 dbieber

Ok, let me check how we can use AST with preserving comments.

sidshrivastav avatar Jun 24 '22 07:06 sidshrivastav