diff --git a/README.md b/README.md index 1482d56d..1d42d306 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,31 @@ Please see [The Python Fire Guide](docs/guide.md). _Note that these flags are separated from the Fire command by an isolated `--`._ +## Xonsh shell support + +```xsh +xpip install fire +xontrib load fire + +obj = {'var': 'val'} +fire obj var +# val + +type(@.env) +# xonsh.environ.Env +fire @.env get USER - upper +# PC + +type(@.imp) +# xonsh.built_ins.InlineImporter +fire @.imp.json dumps --help +# @.imp.json dumps OBJ +fire @.imp.json dumps '{"a":1}' --indent 4 +# { +# "a": 1 +# } +``` + ## License Licensed under the diff --git a/pyproject.toml b/pyproject.toml index 912c08aa..9cf579b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,7 +54,7 @@ test = [ ] [tool.setuptools.packages.find] -include = ["fire*"] +include = ["fire*", "xontrib*"] [tool.setuptools.package-data] fire = ["console/*"] diff --git a/xontrib/fire.py b/xontrib/fire.py new file mode 100644 index 00000000..8138ea59 --- /dev/null +++ b/xontrib/fire.py @@ -0,0 +1,21 @@ +"""Turns any object into a CLI tool using Fire in the xonsh shell.""" + + +@aliases.register +def _fire(args, stdout): + """Turns any object into a CLI tool using Fire in the xonsh shell.""" + if len(args) == 0: + print('Usage: fire [args|--help]', file=stderr) + print('Example: fire @ --help', file=stderr) + return 1 + def fixed_fire(): + """Fix https://github.com/google/python-fire/issues/188""" + import fire + fire.core.Display = lambda lines, out: stdout.write("\n".join(lines) + "\n") + return fire + try: + obj = eval(args[0]) + except: + obj = evalx(args[0]) + with __xonsh__.env.swap(UPDATE_OS_ENVIRON=True, PAGER='-'): + fixed_fire().Fire(obj, command=args[1:], name=args[0])