-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: tell fire to stop parsing argument and pass the remaining unparsed args to function #403
Comments
I think part of the problem here is that A hacky workaround I have came up with in the past was to disable Fire's custom flags altogether and force #!/usr/bin/env python3
import fire
def _patch_fire():
# Disable Fire's custom flags.
def _SeparateFlagArgs(args):
return args, []
fire.core.parser.SeparateFlagArgs = _SeparateFlagArgs
# Make -- behave like POSIX, i.e., separate positional args from options.
orig__ParseKeywordArgs = fire.core._ParseKeywordArgs
def _ParseKeywordArgs(*args, **kwargs):
kwargs, remaining_kwargs, remaining_args = orig__ParseKeywordArgs(*args, **kwargs)
if remaining_kwargs:
try:
ddash_index = remaining_kwargs.index('--')
remaining_args.extend(remaining_kwargs[ddash_index + 1:])
remaining_kwargs = remaining_kwargs[:ddash_index]
except ValueError:
pass
return kwargs, remaining_kwargs, remaining_args
fire.core._ParseKeywordArgs = _ParseKeywordArgs
_patch_fire()
class Conda:
def __init__(self, verbose=False):
...
def run(self, *argv):
print(argv)
if __name__ == '__main__':
fire.Fire(Conda) Now you can get the output you expect: $ python3 fake-conda.py --verbose - run -- which -a python3
('which', '-a', 'python3') And you can pass options too as long as you remember to end the options with class Conda:
def __init__(self, verbose=False):
...
def run(self, *argv, foo=None, bar=None):
print(argv, foo, bar) You can still do things like: $ python3 fake-conda.py --verbose - run --foo --bar=10 -- which -a pytthon3
('which', '-a', 'python3') True 10 Of course this hack is too intrusive and will likely brick the moment Fire changes anything this code touches.. but it might be enough for your needs until a proper feature for this lands in Fire. |
Hi @melsabagh-kw Thank you for the sample code. I think it's a good idea to use def run(self, *argv, foo=None, bar=None): # That would be too complicated to have fire handle partial of args
pass
@fire.decorators.SkipParse
def run(self, *argv): # that's the simple form I want to solve, by just stop paring anything
pass I have already make a PR for it and it works well in my use case to delegate arguments to the downstream commands. And this is the reason why I want this feature to create a tool to turn this java -jar jenkins-cli.jar -s http://localhost:9090/ -webSocket list-jobs into this jenkins-fire-cli run list-jobs by wrapping the original command line to reduce some noise for the end user. |
Feature Description
I am working on a tool to wrap some existed command line tools to make them easier to use (like auto install, inject argument automatically, etc). And I hope that I can have a way to tell Fire to stop parsing after encount some specific command.
Example
Here are some real world examples like
poetry run python3 ./my-scripts.py
orconda run -n my-env python3 ./my-scripts.py
Without this feature I can only put everything else as string, just like what
-c
option ofbash
does, e.g.bash -c "echo 'hello world'"
, which could still work, but the user have to type extra string quote and dealing with escaping in some cases.I am not sure if it is possbile to provide some decorator to archive this, for example
And when running with
python fake-conda.py --verbose run which -a python3
, the output should be('which', '-a', 'python3')
The text was updated successfully, but these errors were encountered: