pax_global_header 0000666 0000000 0000000 00000000064 14002611375 0014511 g ustar 00root root 0000000 0000000 52 comment=2b5902a15857287066108fdd204a790b3cca8887
python-fire-0.4.0/ 0000775 0000000 0000000 00000000000 14002611375 0013756 5 ustar 00root root 0000000 0000000 python-fire-0.4.0/.gitignore 0000664 0000000 0000000 00000002224 14002611375 0015746 0 ustar 00root root 0000000 0000000 # Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# PyCharm IDE
.idea/
# Type-checking
.pytype/
python-fire-0.4.0/.travis.yml 0000664 0000000 0000000 00000001707 14002611375 0016074 0 ustar 00root root 0000000 0000000 language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.7"
- "3.8"
- "3.9"
before_install:
- pip install --upgrade setuptools pip
- pip install --upgrade pylint pytest pytest-pylint pytest-runner
install:
- pip install termcolor
- pip install hypothesis python-Levenshtein
- python setup.py develop
script:
- python -m pytest # Run the tests without IPython.
- pip install ipython
- python -m pytest # Now run the tests with IPython.
- pylint fire --ignore=test_components_py3.py,parser_fuzz_test.py,console
- if [[ $TRAVIS_PYTHON_VERSION == 2.7 || $TRAVIS_PYTHON_VERSION == 3.7 ]]; then
pip install pytype;
fi
# Run type-checking, excluding files that define or use py3 features in py2.
- if [[ $TRAVIS_PYTHON_VERSION == 2.7 ]]; then
pytype -x
fire/fire_test.py
fire/inspectutils_test.py
fire/test_components_py3.py;
elif [[ $TRAVIS_PYTHON_VERSION == 3.7 ]]; then
pytype;
fi
python-fire-0.4.0/CONTRIBUTING.md 0000664 0000000 0000000 00000005071 14002611375 0016212 0 ustar 00root root 0000000 0000000 # How to contribute
We'd love to accept your patches and contributions to this project. There are
just a few small guidelines you need to follow.
First, read these guidelines.
Before you begin making changes, state your intent to do so in an Issue.
Then, fork the project. Make changes in your copy of the repository.
Then open a pull request once your changes are ready.
If this is your first contribution, sign the Contributor License Agreement.
A discussion about your change will follow, and if accepted your contribution
will be incorporated into the Python Fire codebase.
## Contributor License Agreement
Contributions to this project must be accompanied by a Contributor License
Agreement. You (or your employer) retain the copyright to your contribution,
this simply gives us permission to use and redistribute your contributions as
part of the project. Head over to to see
your current agreements on file or to sign a new one.
You generally only need to submit a CLA once, so if you've already submitted one
(even if it was for a different project), you probably don't need to do it
again.
## Code reviews
All submissions, including submissions by project members, require review.
For changes introduced by non-Googlers, we use GitHub pull requests for this
purpose. Consult [GitHub Help] for more information on using pull requests.
[GitHub Help]: https://help.github.com/articles/about-pull-requests/
## Code style
In general, Python Fire follows the guidelines in the
[Google Python Style Guide].
In addition, the project follows a convention of:
- Maximum line length: 80 characters
- Indentation: 2 spaces (4 for line continuation)
- PascalCase for function and method names.
- No type hints, as described in [PEP 484], to maintain compatibility with
Python versions < 3.5.
- Single quotes around strings, three double quotes around docstrings.
[Google Python Style Guide]: http://google.github.io/styleguide/pyguide.html
[PEP 484]: https://www.python.org/dev/peps/pep-0484
## Testing
Python Fire uses Travis CI to run tests on each pull request. You can run
these tests yourself as well. To do this, first install the test dependencies
listed in setup.py (e.g. pytest, mock, termcolor, and hypothesis).
Then run the tests by running `pytest` in the root directory of the repository.
## Linting
Please run lint on your pull requests to make accepting the requests easier.
To do this, run `pylint fire` in the root directory of the repository.
Note that even if lint is passing, additional style changes to your submission
may be made during merging.
python-fire-0.4.0/LICENSE 0000664 0000000 0000000 00000001075 14002611375 0014766 0 ustar 00root root 0000000 0000000 Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
python-fire-0.4.0/MANIFEST.in 0000664 0000000 0000000 00000000020 14002611375 0015504 0 ustar 00root root 0000000 0000000 include LICENSE
python-fire-0.4.0/README.md 0000664 0000000 0000000 00000007721 14002611375 0015244 0 ustar 00root root 0000000 0000000 # Python Fire [](https://github.com/google/python-fire)
_Python Fire is a library for automatically generating command line interfaces
(CLIs) from absolutely any Python object._
- Python Fire is a simple way to create a CLI in Python.
[[1]](docs/benefits.md#simple-cli)
- Python Fire is a helpful tool for developing and debugging Python code.
[[2]](docs/benefits.md#debugging)
- Python Fire helps with exploring existing code or turning other people's
code into a CLI. [[3]](docs/benefits.md#exploring)
- Python Fire makes transitioning between Bash and Python easier.
[[4]](docs/benefits.md#bash)
- Python Fire makes using a Python REPL easier by setting up the REPL with the
modules and variables you'll need already imported and created.
[[5]](docs/benefits.md#repl)
## Installation
To install Python Fire with pip, run: `pip install fire`
To install Python Fire with conda, run: `conda install fire -c conda-forge`
To install Python Fire from source, first clone the repository and then run:
`python setup.py install`
## Basic Usage
You can call `Fire` on any Python object:
functions, classes, modules, objects, dictionaries, lists, tuples, etc.
They all work!
Here's an example of calling Fire on a function.
```python
import fire
def hello(name="World"):
return "Hello %s!" % name
if __name__ == '__main__':
fire.Fire(hello)
```
Then, from the command line, you can run:
```bash
python hello.py # Hello World!
python hello.py --name=David # Hello David!
python hello.py --help # Shows usage information.
```
Here's an example of calling Fire on a class.
```python
import fire
class Calculator(object):
"""A simple calculator class."""
def double(self, number):
return 2 * number
if __name__ == '__main__':
fire.Fire(Calculator)
```
Then, from the command line, you can run:
```bash
python calculator.py double 10 # 20
python calculator.py double --number=15 # 30
```
To learn how Fire behaves on functions, objects, dicts, lists, etc, and to learn
about Fire's other features, see the [Using a Fire CLI page](docs/using-cli.md).
For additional examples, see [The Python Fire Guide](docs/guide.md).
## Why is it called Fire?
When you call `Fire`, it fires off (executes) your command.
## Where can I learn more?
Please see [The Python Fire Guide](docs/guide.md).
## Reference
| Setup | Command | Notes
| :------ | :------------------ | :---------
| install | `pip install fire` |
| Creating a CLI | Command | Notes
| :--------------| :--------------------- | :---------
| import | `import fire` |
| Call | `fire.Fire()` | Turns the current module into a Fire CLI.
| Call | `fire.Fire(component)` | Turns `component` into a Fire CLI.
| Using a CLI | Command | Notes
| :---------------------------------------------- | :-------------------------------------- | :----
| [Help](docs/using-cli.md#help-flag) | `command --help` or `command -- --help` |
| [REPL](docs/using-cli.md#interactive-flag) | `command -- --interactive` | Enters interactive mode.
| [Separator](docs/using-cli.md#separator-flag) | `command -- --separator=X` | Sets the separator to `X`. The default separator is `-`.
| [Completion](docs/using-cli.md#completion-flag) | `command -- --completion [shell]` | Generates a completion script for the CLI.
| [Trace](docs/using-cli.md#trace-flag) | `command -- --trace` | Gets a Fire trace for the command.
| [Verbose](docs/using-cli.md#verbose-flag) | `command -- --verbose` |
_Note that these flags are separated from the Fire command by an isolated `--`._
## License
Licensed under the
[Apache 2.0](https://github.com/google/python-fire/blob/master/LICENSE) License.
## Disclaimer
This is not an official Google product.
python-fire-0.4.0/docs/ 0000775 0000000 0000000 00000000000 14002611375 0014706 5 ustar 00root root 0000000 0000000 python-fire-0.4.0/docs/api.md 0000664 0000000 0000000 00000010350 14002611375 0016000 0 ustar 00root root 0000000 0000000 ## Python Fire Quick Reference
| Setup | Command | Notes
| :------ | :------------------ | :---------
| install | `pip install fire` |
| Creating a CLI | Command | Notes
| :--------------| :--------------------- | :---------
| import | `import fire` |
| Call | `fire.Fire()` | Turns the current module into a Fire CLI.
| Call | `fire.Fire(component)` | Turns `component` into a Fire CLI.
| Using a CLI | Command | Notes |
| :----------------------------------------- | :------------- | :------------- |
| [Help](using-cli.md#help-flag) | `command | Show the help |
: : --help` : screen. :
| [REPL](using-cli.md#interactive-flag) | `command -- | Enters |
: : --interactive` : interactive :
: : : mode. :
| [Separator](using-cli.md#separator-flag) | `command -- | This sets the |
: : --separator=X` : separator to :
: : : `X`. The :
: : : default :
: : : separator is :
: : : `-`. :
| [Completion](using-cli.md#completion-flag) | `command -- | Generate a |
: : --completion : completion :
: : [shell]` : script for the :
: : : CLI. :
| [Trace](using-cli.md#trace-flag) | `command -- | Gets a Fire |
: : --trace` : trace for the :
: : : command. :
| [Verbose](using-cli.md#verbose-flag) | `command -- | |
: : --verbose` : :
_Note that flags are separated from the Fire command by an isolated `--` arg.
Help is an exception; the isolated `--` is optional for getting help._
## Arguments for Calling fire.Fire()
| Argument | Usage | Notes |
| :-------- | :------------------------ | :----------------------------------- |
| component | `fire.Fire(component)` | If omitted, defaults to a dict of |
: : : all locals and globals. :
| command | `fire.Fire(command='hello | Either a string or a list of |
: : --name=5')` : arguments. If a string is provided, :
: : : it is split to determine the :
: : : arguments. If a list or tuple is :
: : : provided, they are the arguments. If :
: : : `command` is omitted, then :
: : : `sys.argv[1\:]` (the arguments from :
: : : the command line) are used by :
: : : default. :
| name | `fire.Fire(name='tool')` | The name of the CLI, ideally the |
: : : name users will enter to run the :
: : : CLI. This name will be used in the :
: : : CLI's help screens. If the argument :
: : : is omitted, it will be inferred :
: : : automatically. :
## Using a Fire CLI without modifying any code
You can use Python Fire on a module without modifying the code of the module.
The syntax for this is:
`python -m fire `
or
`python -m fire `
For example, `python -m fire calendar -h` will treat the built in `calendar`
module as a CLI and provide its help.
python-fire-0.4.0/docs/benefits.md 0000664 0000000 0000000 00000005214 14002611375 0017031 0 ustar 00root root 0000000 0000000 # Benefits of Python Fire
## Create CLIs in Python
It's dead simple. Simply write the functionality you want exposed at the command
line as a function / module / class, and then call Fire. With this addition of a
single-line call to Fire, your CLI is ready to go.
## Develop and debug Python code
When you're writing a Python library, you probably want to try it out as you go.
You could write a main method to check the functionality you're interested in,
but then you have to change the main method with every new experiment you're
interested in testing, and constantly updating the main method is a hassle.
You could also open an IPython REPL and import your library there and test it,
but then you have to deal with reloading your imports every time you change
something.
If you simply call Fire in your library, then you can run all of it's
functionality from the command line without having to keep making changes to
a main method. And if you use the `--interactive` flag to enter an IPython REPL
then you don't need to load the imports or create your variables; they'll
already be ready for use as soon as you start the REPL.
## Explore existing code; turn other people's code into a CLI
You can take an existing module, maybe even one that you don't have access to
the source code for, and call `Fire` on it. This lets you easily see what
functionality this code exposes, without you having to read through all the
code.
This technique can be a very simple way to create very powerful CLIs. Call
`Fire` on the difflib library and you get a powerful diffing tool. Call `Fire`
on the Python Imaging Library (PIL) module and you get a powerful image
manipulation command line tool, very similar in nature to ImageMagick.
The auto-generated help strings that Fire provides when you run a Fire CLI
allow you to see all the functionality these modules provide in a concise
manner.
## Transition between Bash and Python
Using Fire lets you call Python directly from Bash. So you can mix your Python
functions with the unix tools you know and love, like `grep`, `xargs`, `wc`,
etc.
Additionally since writing CLIs in Python requires only a single call to Fire,
it is now easy to write even one-off scripts that would previously have been in
Bash, in Python.
## Explore code in a Python REPL
When you use the `--interactive` flag to enter an IPython REPL, it starts with
variables and modules already defined for you. You don't need to waste time
importing the modules you care about or defining the variables you're going to
use, since Fire has already done so for you.
python-fire-0.4.0/docs/guide.md 0000664 0000000 0000000 00000043570 14002611375 0016336 0 ustar 00root root 0000000 0000000 ## The Python Fire Guide
### Introduction
Welcome to the Python Fire guide! Python Fire is a Python library that will turn
any Python component into a command line interface with just a single call to
`Fire`.
Let's get started!
### Installation
To install Python Fire from pypi, run:
`pip install fire`
Alternatively, to install Python Fire from source, clone the source and run:
`python setup.py install`
### Hello World
##### Version 1: `fire.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.
```python
import fire
def hello(name):
return 'Hello {name}!'.format(name=name)
if __name__ == '__main__':
fire.Fire()
```
Here's how we can run our program from the command line:
```bash
$ python example.py hello World
Hello World!
```
##### Version 2: `fire.Fire()`
Let's modify our program slightly to only expose the `hello` function to the
command line.
```python
import fire
def hello(name):
return 'Hello {name}!'.format(name=name)
if __name__ == '__main__':
fire.Fire(hello)
```
Here's how we can run this from the command line:
```bash
$ python example.py World
Hello World!
```
Notice we no longer have to specify to run the `hello` function, because we
called `fire.Fire(hello)`.
##### Version 3: Using a main
We can alternatively write this program like this:
```python
import fire
def hello(name):
return 'Hello {name}!'.format(name=name)
def main():
fire.Fire(hello)
if __name__ == '__main__':
main()
```
Or if we're using
[entry points](https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points),
then simply this:
```python
import fire
def hello(name):
return 'Hello {name}!'.format(name=name)
def main():
fire.Fire(hello)
```
##### Version 4: Fire Without Code Changes
If you have a file `example.py` that doesn't even import fire:
```python
def hello(name):
return 'Hello {name}!'.format(name=name)
```
Then you can use it with Fire like this:
```bash
$ python -m fire example hello --name=World
Hello World!
```
You can also specify the filepath of example.py rather than its module path,
like so:
```bash
$ python -m fire example.py hello --name=World
Hello World!
```
### Exposing Multiple Commands
In the previous example, we exposed a single function to the command line. Now
we'll look at ways of exposing multiple functions to the command line.
##### Version 1: `fire.Fire()`
The simplest way to expose multiple commands is to write multiple functions, and
then call Fire.
```python
import fire
def add(x, y):
return x + y
def multiply(x, y):
return x * y
if __name__ == '__main__':
fire.Fire()
```
We can use this like so:
```bash
$ python example.py add 10 20
30
$ python example.py multiply 10 20
200
```
You'll notice that Fire correctly parsed `10` and `20` as numbers, rather than
as strings. Read more about [argument parsing here](#argument-parsing).
##### Version 2: `fire.Fire()`
In version 1 we exposed all the program's functionality to the command line. By
using a dict, we can selectively expose functions to the command line.
```python
import fire
def add(x, y):
return x + y
def multiply(x, y):
return x * y
if __name__ == '__main__':
fire.Fire({
'add': add,
'multiply': multiply,
})
```
We can use this in the same way as before:
```bash
$ python example.py add 10 20
30
$ python example.py multiply 10 20
200
```
##### Version 3: `fire.Fire(