pax_global_header 0000666 0000000 0000000 00000000064 14674041172 0014520 g ustar 00root root 0000000 0000000 52 comment=90b7f824f2e760e6363b0d10c52b1940346a0fa6
python-fire-0.7.0/ 0000775 0000000 0000000 00000000000 14674041172 0013770 5 ustar 00root root 0000000 0000000 python-fire-0.7.0/.github/ 0000775 0000000 0000000 00000000000 14674041172 0015330 5 ustar 00root root 0000000 0000000 python-fire-0.7.0/.github/dependabot.yml 0000664 0000000 0000000 00000001374 14674041172 0020165 0 ustar 00root root 0000000 0000000 # Basic dependabot.yml file with minimum configuration for two package managers
version: 2
updates:
# Enable version updates for python
- package-ecosystem: "pip"
directory: ".github/scripts/"
schedule:
interval: "monthly"
labels: ["dependabot"]
pull-request-branch-name:
separator: "-"
open-pull-requests-limit: 5
reviewers:
- "dbieber"
# Enable version updates for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
groups:
gh-actions:
patterns:
- "*" # Check all dependencies
labels: ["dependabot"]
pull-request-branch-name:
separator: "-"
open-pull-requests-limit: 5
reviewers:
- "dbieber"
python-fire-0.7.0/.github/scripts/ 0000775 0000000 0000000 00000000000 14674041172 0017017 5 ustar 00root root 0000000 0000000 python-fire-0.7.0/.github/scripts/build.sh 0000775 0000000 0000000 00000002062 14674041172 0020455 0 ustar 00root root 0000000 0000000 # Copyright (C) 2018 Google Inc.
#
# 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.
#!/usr/bin/env bash
# Exit when any command fails.
set -e
PYTHON_VERSION=${PYTHON_VERSION:-3.7}
pip install -U -r .github/scripts/requirements.txt
python setup.py develop
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 [[ ${PYTHON_VERSION} == 3.7 ]]; then
# Run type-checking.
pip install pytype;
pytype -x fire/test_components_py3.py;
fi
python-fire-0.7.0/.github/scripts/requirements.txt 0000664 0000000 0000000 00000000232 14674041172 0022300 0 ustar 00root root 0000000 0000000 setuptools <=75.1.0
pip
pylint <3.2.8
pytest <=8.3.3
pytest-pylint <=1.1.2
pytest-runner <7.0.0
termcolor <2.5.0
hypothesis <6.113.0
levenshtein <=0.26.0
python-fire-0.7.0/.github/workflows/ 0000775 0000000 0000000 00000000000 14674041172 0017365 5 ustar 00root root 0000000 0000000 python-fire-0.7.0/.github/workflows/build.yml 0000664 0000000 0000000 00000001605 14674041172 0021211 0 ustar 00root root 0000000 0000000 name: Python Fire
on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
defaults:
run:
shell: bash
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: ["macos-latest", "ubuntu-latest"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13.0-rc.2"]
include:
- {os: "ubuntu-20.04", python-version: "3.7"}
steps:
# Checkout the repo.
- name: Checkout Python Fire repository
uses: actions/checkout@v4
# Set up Python environment.
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
# Build Python Fire using the build.sh script.
- name: Run build script
run: ./.github/scripts/build.sh
env:
PYTHON_VERSION: ${{ matrix.python-version }}
python-fire-0.7.0/.gitignore 0000664 0000000 0000000 00000002224 14674041172 0015760 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.7.0/CONTRIBUTING.md 0000664 0000000 0000000 00000004731 14674041172 0016226 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.
- Single quotes around strings, three double quotes around docstrings.
[Google Python Style Guide]: http://google.github.io/styleguide/pyguide.html
## Testing
Python Fire uses [GitHub Actions](https://github.com/google/python-fire/actions) 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.7.0/LICENSE 0000664 0000000 0000000 00000001075 14674041172 0015000 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.7.0/MANIFEST.in 0000664 0000000 0000000 00000000020 14674041172 0015516 0 ustar 00root root 0000000 0000000 include LICENSE
python-fire-0.7.0/README.md 0000664 0000000 0000000 00000007721 14674041172 0015256 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.7.0/docs/ 0000775 0000000 0000000 00000000000 14674041172 0014720 5 ustar 00root root 0000000 0000000 python-fire-0.7.0/docs/api.md 0000664 0000000 0000000 00000005735 14674041172 0016025 0 ustar 00root root 0000000 0000000 ## Python Fire Quick Reference
| Setup | Command | Notes
| ------- | ------------------- | ----------
| install | `pip install fire` | Installs fire from pypi
| 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 --help` | Show the help screen. |
| [REPL](using-cli.md#interactive-flag) | `command -- --interactive` | Enters interactive mode. |
| [Separator](using-cli.md#separator-flag) | `command -- --separator=X` | This sets the separator to `X`. The default separator is `-`. |
| [Completion](using-cli.md#completion-flag) | `command -- --completion [shell]` | Generate a completion script for the CLI. |
| [Trace](using-cli.md#trace-flag) | `command -- --trace` | Gets a Fire 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 --name=5')` | Either a string or a list of 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.|
| serialize | `fire.Fire(serialize=custom_serializer)` | If omitted, simple types are serialized via their builtin str method, and any objects that define a custom `__str__` method are serialized with that. If specified, all objects are serialized to text via the provided method. |
## 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.7.0/docs/benefits.md 0000664 0000000 0000000 00000005214 14674041172 0017043 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.7.0/docs/guide.md 0000664 0000000 0000000 00000046627 14674041172 0016356 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 f'Hello {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 f'Hello {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 f'Hello {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 f'Hello {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 f'Hello {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(