Full Code of google/python-fire for AI

master 716bbc23d7ec cached
79 files
434.1 KB
108.0k tokens
782 symbols
1 requests
Download .txt
Showing preview only (457K chars total). Download the full file or copy to clipboard to get everything.
Repository: google/python-fire
Branch: master
Commit: 716bbc23d7ec
Files: 79
Total size: 434.1 KB

Directory structure:
gitextract_5446x807/

├── .github/
│   ├── dependabot.yml
│   ├── scripts/
│   │   └── build.sh
│   └── workflows/
│       └── build.yml
├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── docs/
│   ├── api.md
│   ├── benefits.md
│   ├── guide.md
│   ├── index.md
│   ├── installation.md
│   ├── troubleshooting.md
│   └── using-cli.md
├── examples/
│   ├── __init__.py
│   ├── cipher/
│   │   ├── __init__.py
│   │   ├── cipher.py
│   │   └── cipher_test.py
│   ├── diff/
│   │   ├── __init__.py
│   │   ├── diff.py
│   │   ├── diff_test.py
│   │   └── difffull.py
│   ├── identity/
│   │   ├── __init__.py
│   │   └── identity.py
│   └── widget/
│       ├── __init__.py
│       ├── collector.py
│       ├── collector_test.py
│       ├── widget.py
│       └── widget_test.py
├── fire/
│   ├── __init__.py
│   ├── __main__.py
│   ├── completion.py
│   ├── completion_test.py
│   ├── console/
│   │   ├── README.md
│   │   ├── __init__.py
│   │   ├── console_attr.py
│   │   ├── console_attr_os.py
│   │   ├── console_io.py
│   │   ├── console_pager.py
│   │   ├── encoding.py
│   │   ├── files.py
│   │   ├── platforms.py
│   │   └── text.py
│   ├── core.py
│   ├── core_test.py
│   ├── custom_descriptions.py
│   ├── custom_descriptions_test.py
│   ├── decorators.py
│   ├── decorators_test.py
│   ├── docstrings.py
│   ├── docstrings_fuzz_test.py
│   ├── docstrings_test.py
│   ├── fire_import_test.py
│   ├── fire_test.py
│   ├── formatting.py
│   ├── formatting_test.py
│   ├── formatting_windows.py
│   ├── helptext.py
│   ├── helptext_test.py
│   ├── inspectutils.py
│   ├── inspectutils_test.py
│   ├── interact.py
│   ├── interact_test.py
│   ├── main_test.py
│   ├── parser.py
│   ├── parser_fuzz_test.py
│   ├── parser_test.py
│   ├── test_components.py
│   ├── test_components_bin.py
│   ├── test_components_py3.py
│   ├── test_components_test.py
│   ├── testutils.py
│   ├── testutils_test.py
│   ├── trace.py
│   ├── trace_test.py
│   └── value_types.py
├── mkdocs.yml
├── pylintrc
└── pyproject.toml

================================================
FILE CONTENTS
================================================

================================================
FILE: .github/dependabot.yml
================================================
# Basic dependabot.yml file with minimum configuration for two package managers

version: 2
updates:
  # Enable version updates for python
  - package-ecosystem: "pip"
    directory: "/"
    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"


================================================
FILE: .github/scripts/build.sh
================================================
# 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 -e .[test]
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.12 ]]; then
  # Run type-checking
  pip install ty
  python -m ty check --python $(which python) --exclude fire/test_components_py3.py --exclude fire/console/ --exclude fire/formatting_windows.py
fi


================================================
FILE: .github/workflows/build.yml
================================================
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", "3.14.0-rc.2"]
        include:
          - {os: "ubuntu-22.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 }}


================================================
FILE: .gitignore
================================================
# 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/


================================================
FILE: CONTRIBUTING.md
================================================
# 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 <https://cla.developers.google.com/> 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.


================================================
FILE: LICENSE
================================================
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.


================================================
FILE: README.md
================================================
# Python Fire [![PyPI](https://img.shields.io/pypi/pyversions/fire.svg?style=plastic)](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:<br>
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.


================================================
FILE: docs/api.md
================================================
## 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 <module> <arguments>`

or

`python -m fire <filepath> <arguments>`

For example, `python -m fire calendar -h` will treat the built in `calendar`
module as a CLI and provide its help.


================================================
FILE: docs/benefits.md
================================================
# Benefits of Python Fire

<a name="simple-cli"></a>
## 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.

<a name="debugging"></a>
## 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.

<a name="exploring"></a>
## 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.

<a name="bash"></a>
## 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.

<a name="repl"></a>
## 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.


================================================
FILE: docs/guide.md
================================================
## 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(<fn>)`

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(<dict>)`

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(<object>)`

Fire also works on objects, as in this variant. This is a good way to expose
multiple commands.

```python
import fire

class Calculator(object):

  def add(self, x, y):
    return x + y

  def multiply(self, x, y):
    return x * y

if __name__ == '__main__':
  calculator = Calculator()
  fire.Fire(calculator)
```

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 4: `fire.Fire(<class>)`

Fire also works on classes. This is another good way to expose multiple
commands.

```python
import fire

class Calculator(object):

  def add(self, x, y):
    return x + y

  def multiply(self, x, y):
    return x * y

if __name__ == '__main__':
  fire.Fire(Calculator)
```

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
```

Why might you prefer a class over an object? One reason is that you can pass
arguments for constructing the class too, as in this broken calculator example.

```python
import fire

class BrokenCalculator(object):

  def __init__(self, offset=1):
      self._offset = offset

  def add(self, x, y):
    return x + y + self._offset

  def multiply(self, x, y):
    return x * y + self._offset

if __name__ == '__main__':
  fire.Fire(BrokenCalculator)
```

When you use a broken calculator, you get wrong answers:

```bash
$ python example.py add 10 20
31
$ python example.py multiply 10 20
201
```

But you can always fix it:

```bash
$ python example.py add 10 20 --offset=0
30
$ python example.py multiply 10 20 --offset=0
200
```

Unlike calling ordinary functions, which can be done both with positional
arguments and named arguments (--flag syntax), arguments to \_\_init\_\_
functions must be passed with the --flag syntax. See the section on
[calling functions](#calling-functions) for more.

### Grouping Commands

Here's an example of how you might make a command line interface with grouped
commands.

```python
class IngestionStage(object):

  def run(self):
    return 'Ingesting! Nom nom nom...'

class DigestionStage(object):

  def run(self, volume=1):
    return ' '.join(['Burp!'] * volume)

  def status(self):
    return 'Satiated.'

class Pipeline(object):

  def __init__(self):
    self.ingestion = IngestionStage()
    self.digestion = DigestionStage()

  def run(self):
    ingestion_output = self.ingestion.run()
    digestion_output = self.digestion.run()
    return [ingestion_output, digestion_output]

if __name__ == '__main__':
  fire.Fire(Pipeline)
```

Here's how this looks at the command line:

```bash
$ python example.py run
Ingesting! Nom nom nom...
Burp!
$ python example.py ingestion run
Ingesting! Nom nom nom...
$ python example.py digestion run
Burp!
$ python example.py digestion status
Satiated.
```

You can nest your commands in arbitrarily complex ways, if you're feeling grumpy
or adventurous.


### Accessing Properties

In the examples we've looked at so far, our invocations of `python example.py`
have all run some function from the example program. In this example, we simply
access a property.

```python
from airports import airports

import fire

class Airport(object):

  def __init__(self, code):
    self.code = code
    self.name = dict(airports).get(self.code)
    self.city = self.name.split(',')[0] if self.name else None

if __name__ == '__main__':
  fire.Fire(Airport)
```

Now we can use this program to learn about airport codes!

```bash
$ python example.py --code=JFK code
JFK
$ python example.py --code=SJC name
San Jose-Sunnyvale-Santa Clara, CA - Norman Y. Mineta San Jose International (SJC)
$ python example.py --code=ALB city
Albany-Schenectady-Troy
```

By the way, you can find this
[airports module here](https://github.com/trendct-data/airports.py).

### Chaining Function Calls

When you run a Fire CLI, you can take all the same actions on the _result_ of
the call to Fire that you can take on the original object passed in.

For example, we can use our Airport CLI from the previous example like this:

```bash
$ python example.py --code=ALB city upper
ALBANY-SCHENECTADY-TROY
```

This works since `upper` is a method on all strings.

So, if you want to set up your functions to chain nicely, all you have to do is
have a class whose methods return self. Here's an example.

```python
import fire

class BinaryCanvas(object):
  """A canvas with which to make binary art, one bit at a time."""

  def __init__(self, size=10):
    self.pixels = [[0] * size for _ in range(size)]
    self._size = size
    self._row = 0  # The row of the cursor.
    self._col = 0  # The column of the cursor.

  def __str__(self):
    return '\n'.join(' '.join(str(pixel) for pixel in row) for row in self.pixels)

  def show(self):
    print(self)
    return self

  def move(self, row, col):
    self._row = row % self._size
    self._col = col % self._size
    return self

  def on(self):
    return self.set(1)

  def off(self):
    return self.set(0)

  def set(self, value):
    self.pixels[self._row][self._col] = value
    return self

if __name__ == '__main__':
  fire.Fire(BinaryCanvas)
```

Now we can draw stuff :).

```bash
$ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on move 7 5 on
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
```

It's supposed to be a smiley face.

### Custom Serialization

You'll notice in the BinaryCanvas example, the canvas with the smiley face was
printed to the screen. You can determine how a component will be serialized by
defining its `__str__` method.

If a custom `__str__` method is present on the final component, the object is
serialized and printed. If there's no custom `__str__` method, then the help
screen for the object is shown instead.

### Can we make an even simpler example than Hello World?

Yes, this program is even simpler than our original Hello World example.

```python
import fire
english = 'Hello World'
spanish = 'Hola Mundo'
fire.Fire()
```

You can use it like this:

```bash
$ python example.py english
Hello World
$ python example.py spanish
Hola Mundo
```

### Calling Functions

Arguments to a constructor are passed by name using flag syntax `--name=value`.

For example, consider this simple class:

```python
import fire

class Building(object):

  def __init__(self, name, stories=1):
    self.name = name
    self.stories = stories

  def climb_stairs(self, stairs_per_story=10):
    for story in range(self.stories):
      for stair in range(1, stairs_per_story):
        yield stair
      yield 'Phew!'
    yield 'Done!'

if __name__ == '__main__':
  fire.Fire(Building)
```

We can instantiate it as follows: `python example.py --name="Sherrerd Hall"`

Arguments to other functions may be passed positionally or by name using flag
syntax.

To instantiate a `Building` and then run the `climb_stairs` function, the
following commands are all valid:

```bash
$ python example.py --name="Sherrerd Hall" --stories=3 climb_stairs 10
$ python example.py --name="Sherrerd Hall" climb_stairs --stairs_per_story=10
$ python example.py --name="Sherrerd Hall" climb_stairs --stairs-per-story 10
$ python example.py climb-stairs --stairs-per-story 10 --name="Sherrerd Hall"
```

You'll notice that hyphens and underscores (`-` and `_`) are interchangeable in
member names and flag names.

You'll also notice that the constructor's arguments can come after the
function's arguments or before the function.

You'll also notice that the equal sign between the flag name and its value is
optional.

##### Functions with `*varargs` and `**kwargs`

Fire supports functions that take \*varargs or \*\*kwargs. Here's an example:

```python
import fire

def order_by_length(*items):
  """Orders items by length, breaking ties alphabetically."""
  sorted_items = sorted(items, key=lambda item: (len(str(item)), str(item)))
  return ' '.join(sorted_items)

if __name__ == '__main__':
  fire.Fire(order_by_length)
```

To use it, we run:

```bash
$ python example.py dog cat elephant
cat dog elephant
```

You can use a separator to indicate that you're done providing arguments to a
function. All arguments after the separator will be used to process the result
of the function, rather than being passed to the function itself. The default
separator is the hyphen `-`.

Here's an example where we use a separator.

```bash
$ python example.py dog cat elephant - upper
CAT DOG ELEPHANT
```

Without the separator, upper would have been treated as another argument.

```bash
$ python example.py dog cat elephant upper
cat dog upper elephant
```

You can change the separator with the `--separator` flag. Flags are always
separated from your Fire command by an isolated `--`. Here's an example where we
change the separator.

```bash
$ python example.py dog cat elephant X upper -- --separator=X
CAT DOG ELEPHANT
```

Separators can be useful when a function accepts \*varargs, \*\*kwargs, or
default values that you don't want to specify. It is also important to remember
to change the separator if you want to pass `-` as an argument.


##### Async Functions

Fire supports calling async functions too. Here's a simple example.

```python
import asyncio

async def count_to_ten():
  for i in range(1, 11):
    await asyncio.sleep(1)
    print(i)

if __name__ == '__main__':
  fire.Fire(count_to_ten)
```

Whenever fire encounters a coroutine function, it runs it, blocking until it completes.


### Argument Parsing

The types of the arguments are determined by their values, rather than by the
function signature where they're used. You can pass any Python literal from the
command line: numbers, strings, tuples, lists, dictionaries, (sets are only
supported in some versions of Python). You can also nest the collections
arbitrarily as long as they only contain literals.

To demonstrate this, we'll make a small example program that tells us the type
of any argument we give it:

```python
import fire
fire.Fire(lambda obj: type(obj).__name__)
```

And we'll use it like so:

```bash
$ python example.py 10
int
$ python example.py 10.0
float
$ python example.py hello
str
$ python example.py '(1,2)'
tuple
$ python example.py [1,2]
list
$ python example.py True
bool
$ python example.py {name:David}
dict
```

You'll notice in that last example that bare-words are automatically replaced
with strings.

Be careful with your quotes! If you want to pass the string `"10"`, rather than
the int `10`, you'll need to either escape or quote your quotes. Otherwise Bash
will eat your quotes and pass an unquoted `10` to your Python program, where
Fire will interpret it as a number.


```bash
$ python example.py 10
int
$ python example.py "10"
int
$ python example.py '"10"'
str
$ python example.py "'10'"
str
$ python example.py \"10\"
str
```

Be careful with your quotes! Remember that Bash processes your arguments first,
and then Fire parses the result of that.
If you wanted to pass the dict `{"name": "David Bieber"}` to your program, you
might try this:

```bash
$ python example.py '{"name": "David Bieber"}'  # Good! Do this.
dict
$ python example.py {"name":'"David Bieber"'}  # Okay.
dict
$ python example.py {"name":"David Bieber"}  # Wrong. This is parsed as a string.
str
$ python example.py {"name": "David Bieber"}  # Wrong. This isn't even treated as a single argument.
<error>
$ python example.py '{"name": "Justin Bieber"}'  # Wrong. This is not the Bieber you're looking for. (The syntax is fine though :))
dict
```

##### Boolean Arguments

The tokens `True` and `False` are parsed as boolean values.

You may also specify booleans via flag syntax `--name` and `--noname`, which set
`name` to `True` and `False` respectively.

Continuing the previous example, we could run any of the following:

```bash
$ python example.py --obj=True
bool
$ python example.py --obj=False
bool
$ python example.py --obj
bool
$ python example.py --noobj
bool
```

Be careful with boolean flags! If a token other than another flag immediately
follows a flag that's supposed to be a boolean, the flag will take on the value
of the token rather than the boolean value. You can resolve this: by putting a
separator after your last flag, by explicitly stating the value of the boolean
flag (as in `--obj=True`), or by making sure there's another flag after any
boolean flag argument.


### Using Fire Flags

Fire CLIs all come with a number of flags. These flags should be separated from
the Fire command by an isolated `--`. If there is at least one isolated `--`
argument, then arguments after the final isolated `--` are treated as flags,
whereas all arguments before the final isolated `--` are considered part of the
Fire command.

One useful flag is the `--interactive` flag. Use the `--interactive` flag on any
CLI to enter a Python REPL with all the modules and variables used in the
context where `Fire` was called already available to you for use. Other useful
variables, such as the result of the Fire command will also be available. Use
this feature like this: `python example.py -- --interactive`.

You can add the help flag to any command to see help and usage information. Fire
incorporates your docstrings into the help and usage information that it
generates. Fire will try to provide help even if you omit the isolated `--`
separating the flags from the Fire command, but may not always be able to, since
`help` is a valid argument name. Use this feature like this: `python
example.py -- --help` or `python example.py --help` (or even `python example.py
-h`).

The complete set of flags available is shown below, in the reference section.


### Reference

| Setup   | Command             | Notes
| :------ | :------------------ | :---------
| install | `pip install fire`  |

##### Creating a CLI

| 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.

##### Flags

| Using a CLI    | Command                    | Notes
| :------------- | :------------------------- | :---------
| [Help](using-cli.md#help-flag) | `command -- --help` | Show help and usage information for the command.
| [REPL](using-cli.md#interactive-flag) | `command -- --interactive` | Enter 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` | Include private members in the output.

_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. |


### Disclaimer

Python Fire is not an official Google product.


================================================
FILE: docs/index.md
================================================
# Python Fire [![PyPI](https://img.shields.io/pypi/pyversions/fire.svg?style=plastic)](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]](benefits.md#simple-cli)
-   Python Fire is a helpful tool for developing and debugging Python code.
    [[2]](benefits.md#debugging)
-   Python Fire helps with exploring existing code or turning other people's
    code into a CLI. [[3]](benefits.md#exploring)
-   Python Fire makes transitioning between Bash and Python easier.
    [[4]](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]](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:<br>
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](using-cli.md).

For additional examples, see [The Python Fire Guide](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](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](using-cli.md#help-flag)             | `command --help` or `command -- --help` |
| [REPL](using-cli.md#interactive-flag)      | `command -- --interactive`              | Enters interactive mode.
| [Separator](using-cli.md#separator-flag)   | `command -- --separator=X`              | Sets the separator to `X`. The default separator is `-`.
| [Completion](using-cli.md#completion-flag) | `command -- --completion [shell]`       | Generates 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._

## 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.


================================================
FILE: docs/installation.md
================================================
# 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`. To install from source for development, instead run `python setup.py develop`.


================================================
FILE: docs/troubleshooting.md
================================================
# Troubleshooting

This page describes known issues that users of Python Fire have run into. If you
have an issue not resolved here, consider opening a
[GitHub Issue](https://github.com/google/python-fire/issues).

### Issue [#19](https://github.com/google/python-fire/issues/19): Don't name your module "cmd"

If you have a module name that conflicts with the name of a builtin module, then
when Fire goes to import the builtin module, it will import your module instead.
This will result in an error, possibly an `AttributeError`. Specifically, do not
name your module any of the following:
sys, linecache, cmd, bdb, repr, os, re, pprint, traceback


================================================
FILE: docs/using-cli.md
================================================
# Using a Fire CLI

## Basic usage

Every Fire command corresponds to a Python component.

The simplest Fire command consists of running your program with no additional
arguments. This command corresponds to the Python component you called the
`Fire` function on. If you did not supply an object in the call to `Fire`, then
the context in which `Fire` was called will be used as the Python component.

You can append `--help` or `-h` to a command to see what Python component it
corresponds to, as well as the various ways in which you can extend the command.

Flags to Fire should be separated from the Fire command by an isolated `--` in
order to distinguish between flags and named arguments. So, for example, to
enter interactive mode append `-- -i` or `-- --interactive` to any command. To
use Fire in verbose mode, append `-- --verbose`.

Given a Fire command that corresponds to a Python object, you can extend that
command to access a member of that object, call it with arguments if it is a
function, instantiate it if it is a class, or index into it if it is a list.

Read on to learn about how you can write a Fire command corresponding to
whatever Python component you're looking for.


### Accessing members of an object

If your command corresponds to an object, you can extend your command by adding
the name of a member of that object as a new argument to the command. The
resulting command will correspond to that member.

For example, if the object your command corresponds to has a method defined on
it named 'whack', then you can add the argument 'whack' to your command, and the
resulting new command corresponds to the whack method.

As another example, if the object your command corresponds to has a property
named high_score, then you can add the argument 'high-score' to your command,
and the resulting new command corresponds to the value of the high_score
property.


### Accessing members of a dict

If your command corresponds to a dict, you can extend your command by adding
the name of one of the dict's keys as an argument.

For example, `widget function-that-returns-dict key` will correspond to the
value of the item with key `key` in the dict returned by
`function_that_returns_dict`.


### Accessing members of a list or tuple

If your command corresponds to a list or tuple, you can extend your command by
adding the index of an element of the component to your command as an argument.

For example, `widget function-that-returns-list 2` will correspond to item 2 of
the result of `function_that_returns_list`.


### Calling a function

If your command corresponds to a function, you can extend your command by adding
the arguments of this function. Arguments can be specified positionally, or by
name. To specify an argument by name, use flag syntax.

For example, suppose your `command` corresponds to the function `double`:

```python
def double(value=0):
  return 2 * value
```

Then you can extend your command using named arguments as `command --value 5`,
or using positional arguments as `command 5`. In both cases, the new command
corresponds to the result of the function, in this case the number 10.

You can force a function that takes a variable number of arguments to be
evaluated by adding a separator (the default separator is the hyphen, "-"). This
will prevent arguments to the right of the separator from being consumed for
calling the function. This is useful if the function has arguments with default
values, or if the function accepts \*varargs, or if the function accepts
\*\*kwargs.

See also the section on [Changing the Separator](#separator-flag).


### Instantiating a class

If your command corresponds to a class, you can extend your command by adding
the arguments of the class's `__init__` function. Arguments must be specified
by name, using the flags syntax. See the section on
[calling a function](#calling-a-function) for more details.

Similarly, when passing arguments to a callable object (an object with a custom
`__call__` function), those arguments must be passed using flags syntax.

## Using Flags with Fire CLIs <a name="using-flags"></a>

Command line arguments to a Fire CLI are normally consumed by Fire, as described
in the [Basic Usage](#basic-usage) section. In order to set Flags, put the flags
after the final standalone `--` argument. (If there is no `--` argument, then no
arguments are used for flags.)

For example, to set the alsologtostderr flag, you could run the command:
`widget bang --noise=boom -- --alsologtostderr`. The `--noise` argument is
consumed by Fire, but the `--alsologtostderr` argument is treated as a normal
Flag.

All CLIs built with Python Fire share some flags, as described in the next
sections.


## Python Fire's Flags

As described in the [Using Flags](#using-flags) section, you must add an
isolated `--` argument in order to have arguments treated as Flags rather than
be consumed by Python Fire. All arguments to a Fire CLI after the final
standalone `--` argument are treated as Flags.

The following flags are accepted by all Fire CLIs:
[`--interactive`/`-i`](#interactive-flag),
[`--help`/`-h`](#help-flag),
[`--separator`](#separator-flag),
[`--completion`](#completion-flag),
[`--trace`](#trace-flag),
and [`--verbose`/`-v`](#verbose-flag),
as described in the following sections.

### `--interactive`: Interactive mode <a name="interactive-flag"></a>

Call `widget -- --interactive` or `widget -- -i` to enter interactive mode. This
will put you in an IPython REPL, with the variable `widget` already defined.

You can then explore the Python object that `widget` corresponds to
interactively using Python.

Note: if you want fire to start the IPython REPL instead of the regular Python one,
the `ipython` package needs to be installed in your environment.


### `--completion`: Generating a completion script <a name="completion-flag"></a>

Call `widget -- --completion` to generate a completion script for the Fire CLI
`widget`. To save the completion script to your home directory, you could e.g.
run `widget -- --completion > ~/.widget-completion`. You should then source this
file; to get permanent completion, source this file from your `.bashrc` file.

Call `widget -- --completion fish` to generate a completion script for the Fish
shell. Source this file from your fish.config.

If the commands available in the Fire CLI change, you'll have to regenerate the
completion script and source it again.


### `--help`: Getting help <a name="help-flag"></a>

Let say you have a command line tool named `widget` that was made with Fire. How
do you use this Fire CLI?

The simplest way to get started is to run `widget -- --help`. This will give you
usage information for your CLI. You can always append `-- --help` to any Fire
command in order to get usage information for that command and any subcommands.

Additionally, help will be displayed if you hit an error using Fire. For
example, if you try to pass too many or too few arguments to a function, then
help will be displayed. Similarly, if you try to access a member that does not
exist, or if you index into a list with too high an index, then help will be
displayed.

The displayed help shows information about which Python component your command
corresponds to, as well as usage information for how to extend that command.


### `--trace`: Getting a Fire trace <a name="trace-flag"></a>

In order to understand what is happening when you call Python Fire, it can be
useful to request a trace. This is done via the `--trace` flag, e.g.
`widget whack 5 -- --trace`.

A trace provides step by step information about how the Fire command was
executed. In includes which actions were taken, starting with the initial
component, leading to the final component represented by the command.

A trace is also shown alongside the help if your Fire command reaches an error.


### `--separator`: Changing the separator <a name="separator-flag"></a>

As described in [Calling a Function](#calling-a-function), you can use a
separator argument when writing a command that corresponds to calling a
function. The separator will cause the function to be evaluated or the class to
be instantiated using only the arguments left of the separator. Arguments right
of the separator will then be applied to the result of the function call or to
the instantiated object.

The default separator is `-`.

If you want to supply the string "-" as an argument, then you will have to
change the separator. You can choose a new separator by supplying the
`--separator` flag to Fire.

Here's an example to demonstrate separator usage. Let's say you have a function
that takes a variable number of args, and you want to call that function, and
then upper case the result. Here's how to do it:

```python
# Here's the Python function
def display(arg1, arg2='!'):
  return arg1 + arg2
```

```bash
# Here's what you can do from Bash (Note: the default separator is the hyphen -)
display hello                         # hello!
display hello upper                   # helloupper
display hello - upper                 # HELLO!
display - SEP upper -- --separator SEP    # -!
```
Notice how in the third and fourth lines, the separator caused the display
function to be called with the default value for arg2. In the fourth example,
we change the separator to the string "SEP" so that we can pass '-' as an
argument.

### `--verbose`: Verbose usage <a name="verbose-flag"></a>

Adding the `-v` or `--verbose` flag turns on verbose mode. This will eg
reveal private members in the usage string. Often these members will not
actually be usable from the command line tool. As such, verbose mode should be
considered a debugging tool, but not fully supported yet.


================================================
FILE: examples/__init__.py
================================================


================================================
FILE: examples/cipher/__init__.py
================================================


================================================
FILE: examples/cipher/cipher.py
================================================
# 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.

"""The Caesar Shift Cipher example Fire CLI.

This module demonstrates the use of Fire without specifying a target component.
Notice how the call to Fire() in the main method doesn't indicate a component.
So, all local and global variables (including all functions defined in the
module) are made available as part of the Fire CLI.

Example usage:
cipher rot13 'Hello world!'  # Uryyb jbeyq!
cipher rot13 'Uryyb jbeyq!'  # Hello world!
cipher caesar-encode 1 'Hello world!'  # Ifmmp xpsme!
cipher caesar-decode 1 'Ifmmp xpsme!'  # Hello world!
"""

import fire


def caesar_encode(n=0, text=''):
  return ''.join(
      _caesar_shift_char(n, char)
      for char in text
  )


def caesar_decode(n=0, text=''):
  return caesar_encode(-n, text)


def rot13(text):
  return caesar_encode(13, text)


def _caesar_shift_char(n=0, char=' '):
  if not char.isalpha():
    return char
  if char.isupper():
    return chr((ord(char) - ord('A') + n) % 26 + ord('A'))
  return chr((ord(char) - ord('a') + n) % 26 + ord('a'))


def main():
  fire.Fire(name='cipher')

if __name__ == '__main__':
  main()


================================================
FILE: examples/cipher/cipher_test.py
================================================
# 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.

"""Tests for the cipher module."""

from fire import testutils

from examples.cipher import cipher


class CipherTest(testutils.BaseTestCase):

  def testCipher(self):
    self.assertEqual(cipher.rot13('Hello world!'), 'Uryyb jbeyq!')
    self.assertEqual(cipher.caesar_encode(13, 'Hello world!'), 'Uryyb jbeyq!')
    self.assertEqual(cipher.caesar_decode(13, 'Uryyb jbeyq!'), 'Hello world!')

    self.assertEqual(cipher.caesar_encode(1, 'Hello world!'), 'Ifmmp xpsme!')
    self.assertEqual(cipher.caesar_decode(1, 'Ifmmp xpsme!'), 'Hello world!')


if __name__ == '__main__':
  testutils.main()


================================================
FILE: examples/diff/__init__.py
================================================


================================================
FILE: examples/diff/diff.py
================================================
# 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.

r"""A command line tool for diffing files.

The Python 2.7 documentation demonstrates how to make a command line interface
for the difflib library using optparse:
https://docs.python.org/2/library/difflib.html#a-command-line-interface-to-difflib

This file demonstrates how to create a command line interface providing the same
functionality using Python Fire.

Usage:

diff FROMFILE TOFILE COMMAND [LINES]

Arguments can be passed positionally or via the Flag syntax.
Using positional arguments, the usage is:

diff FROMFILE TOFILE
diff FROMFILE TOFILE context-diff [LINES]
diff FROMFILE TOFILE unified-diff [LINES]
diff FROMFILE TOFILE ndiff
diff FROMFILE TOFILE make-file [CONTEXT] [LINES]

Using the Flag syntax, the usage is:

diff --fromfile=FROMFILE --tofile=TOFILE
diff --fromfile=FROMFILE --tofile=TOFILE context-diff [--lines=LINES]
diff --fromfile=FROMFILE --tofile=TOFILE unified-diff [--lines=LINES]
diff --fromfile=FROMFILE --tofile=TOFILE ndiff
diff --fromfile=FROMFILE --tofile=TOFILE make-file \
    [--context=CONTEXT] [--lines LINES]

As with any Fire CLI, you can append '--' followed by any Flags to any command.

The Flags available for all Fire CLIs are:
  --help
  --interactive
  --trace
  --separator=SEPARATOR
  --completion
  --verbose
"""

import difflib
import os
import time

import fire


class DiffLibWrapper(object):
  """Provides a simple interface to the difflib module.

  The purpose of this simple interface is to offer a limited subset of the
  difflib functionality as a command line interface.
  """

  def __init__(self, fromfile, tofile):
    self._fromfile = fromfile
    self._tofile = tofile

    self.fromdate = time.ctime(os.stat(fromfile).st_mtime)
    self.todate = time.ctime(os.stat(tofile).st_mtime)
    with open(fromfile) as f:
      self.fromlines = f.readlines()
    with open(tofile) as f:
      self.tolines = f.readlines()

  def unified_diff(self, lines=3):
    return difflib.unified_diff(
        self.fromlines, self.tolines, self._fromfile,
        self._tofile, self.fromdate, self.todate, n=lines)

  def ndiff(self):
    return difflib.ndiff(self.fromlines, self.tolines)

  def make_file(self, context=False, lines=3):
    return difflib.HtmlDiff().make_file(
        self.fromlines, self.tolines, self._fromfile, self._tofile,
        context=context, numlines=lines)

  def context_diff(self, lines=3):
    return difflib.context_diff(
        self.fromlines, self.tolines, self._fromfile,
        self._tofile, self.fromdate, self.todate, n=lines)


def main():
  fire.Fire(DiffLibWrapper, name='diff')

if __name__ == '__main__':
  main()


================================================
FILE: examples/diff/diff_test.py
================================================
# 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.

"""Tests for the diff and difffull modules."""

import tempfile

from fire import testutils

from examples.diff import diff
from examples.diff import difffull


class DiffTest(testutils.BaseTestCase):
  """The purpose of these tests is to ensure the difflib wrappers works.

  It is not the goal of these tests to exhaustively test difflib functionality.
  """

  def setUp(self):
    self.file1 = file1 = tempfile.NamedTemporaryFile()
    self.file2 = file2 = tempfile.NamedTemporaryFile()

    file1.write(b'test\ntest1\n')
    file2.write(b'test\ntest2\nextraline\n')

    file1.flush()
    file2.flush()

    self.diff = diff.DiffLibWrapper(file1.name, file2.name)

  def testSetUp(self):
    self.assertEqual(self.diff.fromlines, ['test\n', 'test1\n'])
    self.assertEqual(self.diff.tolines, ['test\n', 'test2\n', 'extraline\n'])

  def testUnifiedDiff(self):
    results = list(self.diff.unified_diff())
    self.assertTrue(results[0].startswith('--- ' + self.file1.name))
    self.assertTrue(results[1].startswith('+++ ' + self.file2.name))
    self.assertEqual(
        results[2:],
        [
            '@@ -1,2 +1,3 @@\n',
            ' test\n',
            '-test1\n',
            '+test2\n',
            '+extraline\n',
        ]
    )

  def testContextDiff(self):
    expected_lines = [
        '***************\n',
        '*** 1,2 ****\n',
        '  test\n',
        '! test1\n',
        '--- 1,3 ----\n',
        '  test\n',
        '! test2\n',
        '! extraline\n']
    results = list(self.diff.context_diff())
    self.assertEqual(results[2:], expected_lines)

  def testNDiff(self):
    expected_lines = [
        '  test\n',
        '- test1\n',
        '?     ^\n',
        '+ test2\n',
        '?     ^\n',
        '+ extraline\n']
    results = list(self.diff.ndiff())
    self.assertEqual(results, expected_lines)

  def testMakeDiff(self):
    self.assertTrue(''.join(self.diff.make_file()).startswith('\n<!DOC'))

  def testDiffFull(self):
    self.assertIsNotNone(difffull)
    self.assertIsNotNone(difffull.difflib)


if __name__ == '__main__':
  testutils.main()


================================================
FILE: examples/diff/difffull.py
================================================
# 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.

"""A command line tool for diffing files.

This demonstrates the simplest possible way to turn a module into a command line
interface with Python Fire. It exemplifies the power and shortcomings of relying
on Python Fire's simplicity.

See //fire/examples/diff/diff.py for another way of turning
difflib into a CLI that requires more code, but gives the developer more control
over the CLI's API.

Use the help flag to see usage for all the things this CLI can do. For example:

difffull -- -h
difffull HtmlDiff -- -h  # Help for the HtmlDiff class
difffull HtmlDiff - -- -h  # Help for an HtmlDiff object, not the HtmlDiff class

Here are some of the diff commands available:

difffull ndiff A B [LINEJUNK] [CHARJUNK]
difffull context-diff A B [FROMFILE] [TOFILE] [FROMFILEDATE] [TOFILEDATE] [N]
difffull unified-diff A B [FROMFILE] [TOFILE] [FROMFILEDATE] [TOFILEDATE] [N]
difffull HtmlDiff - make-file FROMLINES TOLINES [FROMDESC] [TODESC] [CONTEXT]

For more useful versions of those last four commands using Python Fire, see
//fire/examples/diff:diff.par
"""

import difflib

import fire


def main():
  fire.Fire(difflib, name='difffull')

if __name__ == '__main__':
  main()


================================================
FILE: examples/identity/__init__.py
================================================


================================================
FILE: examples/identity/identity.py
================================================
# 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.

"""A simple command line tool for testing purposes."""

import fire


def identity(arg=None):
  return arg, type(arg)


def main(_=None):
  fire.Fire(identity, name='identity')

if __name__ == '__main__':
  main()


================================================
FILE: examples/widget/__init__.py
================================================


================================================
FILE: examples/widget/collector.py
================================================
# 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.

"""As a Python Fire demo, a Collector collects widgets, and nobody knows why."""

import fire

from examples.widget import widget


class Collector(object):
  """A Collector has one Widget, but wants more."""

  def __init__(self):
    self.widget = widget.Widget()
    self.desired_widget_count = 10

  def collect_widgets(self):
    """Returns all the widgets the Collector wants."""
    return [widget.Widget() for _ in range(self.desired_widget_count)]


def main():
  fire.Fire(Collector(), name='collector')

if __name__ == '__main__':
  main()


================================================
FILE: examples/widget/collector_test.py
================================================
# 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.

"""Tests for the collector module."""

from fire import testutils

from examples.widget import collector
from examples.widget import widget


class CollectorTest(testutils.BaseTestCase):

  def testCollectorHasWidget(self):
    col = collector.Collector()
    self.assertIsInstance(col.widget, widget.Widget)

  def testCollectorWantsMoreWidgets(self):
    col = collector.Collector()
    self.assertEqual(col.desired_widget_count, 10)

  def testCollectorGetsWantedWidgets(self):
    col = collector.Collector()
    self.assertEqual(len(col.collect_widgets()), 10)


if __name__ == '__main__':
  testutils.main()


================================================
FILE: examples/widget/widget.py
================================================
# 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.

"""As a simple example of Python Fire, a Widget serves no clear purpose."""

import fire


class Widget(object):

  def whack(self, n=1):
    """Prints "whack!" n times."""
    return ' '.join('whack!' for _ in range(n))

  def bang(self, noise='bang'):
    """Makes a loud noise."""
    return f'{noise} bang!'


def main():
  fire.Fire(Widget(), name='widget')

if __name__ == '__main__':
  main()


================================================
FILE: examples/widget/widget_test.py
================================================
# 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.

"""Tests for the widget module."""

from fire import testutils

from examples.widget import widget


class WidgetTest(testutils.BaseTestCase):

  def testWidgetWhack(self):
    toy = widget.Widget()
    self.assertEqual(toy.whack(), 'whack!')
    self.assertEqual(toy.whack(3), 'whack! whack! whack!')

  def testWidgetBang(self):
    toy = widget.Widget()
    self.assertEqual(toy.bang(), 'bang bang!')
    self.assertEqual(toy.bang('boom'), 'boom bang!')


if __name__ == '__main__':
  testutils.main()


================================================
FILE: fire/__init__.py
================================================
# 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.

"""The Python Fire module."""

from fire.core import Fire

__all__ = ['Fire']
__version__ = '0.7.1'


================================================
FILE: fire/__main__.py
================================================
# 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.

# pylint: disable=invalid-name
"""Enables use of Python Fire as a "main" function (i.e. "python -m fire").

This allows using Fire with third-party libraries without modifying their code.
"""

import importlib
from importlib import util
import os
import sys

import fire

cli_string = """usage: python -m fire [module] [arg] ..."

Python Fire is a library for creating CLIs from absolutely any Python
object or program. To run Python Fire from the command line on an
existing Python file, it can be invoked with "python -m fire [module]"
and passed a Python module using module notation:

"python -m fire packageA.packageB.module"

or with a file path:

"python -m fire packageA/packageB/module.py" """


def import_from_file_path(path):
  """Performs a module import given the filename.

  Args:
    path (str): the path to the file to be imported.

  Raises:
    IOError: if the given file does not exist or importlib fails to load it.

  Returns:
    Tuple[ModuleType, str]: returns the imported module and the module name,
      usually extracted from the path itself.
  """

  if not os.path.exists(path):
    raise OSError('Given file path does not exist.')

  module_name = os.path.basename(path)

  spec = util.spec_from_file_location(module_name, path)

  if spec is None or spec.loader is None:
    raise OSError('Unable to load module from specified path.')

  module = util.module_from_spec(spec)  # pylint: disable=no-member
  spec.loader.exec_module(module)

  return module, module_name


def import_from_module_name(module_name):
  """Imports a module and returns it and its name."""
  module = importlib.import_module(module_name)
  return module, module_name


def import_module(module_or_filename):
  """Imports a given module or filename.

  If the module_or_filename exists in the file system and ends with .py, we
  attempt to import it. If that import fails, try to import it as a module.

  Args:
    module_or_filename (str): string name of path or module.

  Raises:
    ValueError: if the given file is invalid.
    IOError: if the file or module can not be found or imported.

  Returns:
    Tuple[ModuleType, str]: returns the imported module and the module name,
      usually extracted from the path itself.
  """

  if os.path.exists(module_or_filename):
    # importlib.util.spec_from_file_location requires .py
    if not module_or_filename.endswith('.py'):
      try:  # try as module instead
        return import_from_module_name(module_or_filename)
      except ImportError:
        raise ValueError('Fire can only be called on .py files.')

    return import_from_file_path(module_or_filename)

  if os.path.sep in module_or_filename:  # Use / to detect if it was a filename.
    raise OSError('Fire was passed a filename which could not be found.')

  return import_from_module_name(module_or_filename)  # Assume it's a module.


def main(args):
  """Entrypoint for fire when invoked as a module with python -m fire."""

  if len(args) < 2:
    print(cli_string)
    sys.exit(1)

  module_or_filename = args[1]
  module, module_name = import_module(module_or_filename)

  fire.Fire(module, name=module_name, command=args[2:])


if __name__ == '__main__':
  main(sys.argv)


================================================
FILE: fire/completion.py
================================================
# 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.

"""Provides tab completion functionality for CLIs built with Fire."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import copy
import inspect

from fire import inspectutils


def Script(name, component, default_options=None, shell='bash'):
  if shell == 'fish':
    return _FishScript(name, _Commands(component), default_options)
  return _BashScript(name, _Commands(component), default_options)


def _BashScript(name, commands, default_options=None):
  """Returns a Bash script registering a completion function for the commands.

  Args:
    name: The first token in the commands, also the name of the command.
    commands: A list of all possible commands that tab completion can complete
        to. Each command is a list or tuple of the string tokens that make up
        that command.
    default_options: A dict of options that can be used with any command. Use
        this if there are flags that can always be appended to a command.
  Returns:
    A string which is the Bash script. Source the bash script to enable tab
    completion in Bash.
  """
  default_options = default_options or set()
  global_options, options_map, subcommands_map = _GetMaps(
      name, commands, default_options
  )

  bash_completion_template = """# bash completion support for {name}
# DO NOT EDIT.
# This script is autogenerated by fire/completion.py.

_complete-{identifier}()
{{
  local cur prev opts lastcommand
  COMPREPLY=()
  prev="${{COMP_WORDS[COMP_CWORD-1]}}"
  cur="${{COMP_WORDS[COMP_CWORD]}}"
  lastcommand=$(get_lastcommand)

  opts="{default_options}"
  GLOBAL_OPTIONS="{global_options}"

{checks}

  COMPREPLY=( $(compgen -W "${{opts}}" -- ${{cur}}) )
  return 0
}}

get_lastcommand()
{{
  local lastcommand i

  lastcommand=
  for ((i=0; i < ${{#COMP_WORDS[@]}}; ++i)); do
    if [[ ${{COMP_WORDS[i]}} != -* ]] && [[ -n ${{COMP_WORDS[i]}} ]] && [[
      ${{COMP_WORDS[i]}} != $cur ]]; then
      lastcommand=${{COMP_WORDS[i]}}
    fi
  done

  echo $lastcommand
}}

filter_options()
{{
  local opts
  opts=""
  for opt in "$@"
  do
    if ! option_already_entered $opt; then
      opts="$opts $opt"
    fi
  done

  echo $opts
}}

option_already_entered()
{{
  local opt
  for opt in ${{COMP_WORDS[@]:0:$COMP_CWORD}}
  do
    if [ $1 == $opt ]; then
      return 0
    fi
  done
  return 1
}}

is_prev_global()
{{
  local opt
  for opt in $GLOBAL_OPTIONS
  do
    if [ $opt == $prev ]; then
      return 0
    fi
  done
  return 1
}}

complete -F _complete-{identifier} {command}
"""

  check_wrapper = """
  case "${{lastcommand}}" in
  {lastcommand_checks}
  esac"""

  lastcommand_check_template = """
    {command})
      {opts_assignment}
      opts=$(filter_options $opts)
    ;;"""

  opts_assignment_subcommand_template = """
      if is_prev_global; then
        opts="${{GLOBAL_OPTIONS}}"
      else
        opts="{options} ${{GLOBAL_OPTIONS}}"
      fi"""

  opts_assignment_main_command_template = """
      opts="{options} ${{GLOBAL_OPTIONS}}" """

  def _GetOptsAssignmentTemplate(command):
    if command == name:
      return opts_assignment_main_command_template
    else:
      return opts_assignment_subcommand_template

  lines = []
  commands_set = set()
  commands_set.add(name)
  commands_set = commands_set.union(set(subcommands_map.keys()))
  commands_set = commands_set.union(set(options_map.keys()))
  for command in commands_set:
    opts_assignment = _GetOptsAssignmentTemplate(command).format(
        options=' '.join(
            sorted(options_map[command].union(subcommands_map[command]))
        ),
    )
    lines.append(
        lastcommand_check_template.format(
            command=command,
            opts_assignment=opts_assignment)
    )
  lastcommand_checks = '\n'.join(lines)

  checks = check_wrapper.format(
      lastcommand_checks=lastcommand_checks,
  )

  return (
      bash_completion_template.format(
          name=name,
          command=name,
          checks=checks,
          default_options=' '.join(default_options),
          identifier=name.replace('/', '').replace('.', '').replace(',', ''),
          global_options=' '.join(global_options),
      )
  )


def _FishScript(name, commands, default_options=None):
  """Returns a Fish script registering a completion function for the commands.

  Args:
    name: The first token in the commands, also the name of the command.
    commands: A list of all possible commands that tab completion can complete
        to. Each command is a list or tuple of the string tokens that make up
        that command.
    default_options: A dict of options that can be used with any command. Use
        this if there are flags that can always be appended to a command.
  Returns:
    A string which is the Fish script. Source the fish script to enable tab
    completion in Fish.
  """
  default_options = default_options or set()
  global_options, options_map, subcommands_map = _GetMaps(
      name, commands, default_options
  )

  fish_source = """function __fish_using_command
    set cmd (commandline -opc)
    for i in (seq (count $cmd) 1)
        switch $cmd[$i]
        case "-*"
        case "*"
            if [ $cmd[$i] = $argv[1] ]
                return 0
            else
                return 1
            end
        end
    end
    return 1
end

function __option_entered_check
    set cmd (commandline -opc)
    for i in (seq (count $cmd))
        switch $cmd[$i]
        case "-*"
            if [ $cmd[$i] = $argv[1] ]
                return 1
            end
        end
    end
    return 0
end

function __is_prev_global
    set cmd (commandline -opc)
    set global_options {global_options}
    set prev (count $cmd)

    for opt in $global_options
        if [ "--$opt" = $cmd[$prev] ]
            echo $prev
            return 0
        end
    end
    return 1
end

"""

  subcommand_template = ("complete -c {name} -n '__fish_using_command "
                         "{command}' -f -a {subcommand}\n")
  flag_template = ("complete -c {name} -n "
                   "'__fish_using_command {command};{prev_global_check} and "
                   "__option_entered_check --{option}' -l {option}\n")

  prev_global_check = ' and __is_prev_global;'
  for command in set(subcommands_map.keys()).union(set(options_map.keys())):
    for subcommand in subcommands_map[command]:
      fish_source += subcommand_template.format(
          name=name,
          command=command,
          subcommand=subcommand,
      )

    for option in options_map[command].union(global_options):
      check_needed = command != name
      fish_source += flag_template.format(
          name=name,
          command=command,
          prev_global_check=prev_global_check if check_needed else '',
          option=option.lstrip('--'),
      )

  return fish_source.format(
      global_options=' '.join(f'"{option}"' for option in global_options)
  )


def MemberVisible(component, name, member, class_attrs=None, verbose=False):
  """Returns whether a member should be included in auto-completion or help.

  Determines whether a member of an object with the specified name should be
  included in auto-completion or help text(both usage and detailed help).

  If the member name starts with '__', it will always be excluded. If it
  starts with only one '_', it will be included for all non-string types. If
  verbose is True, the members, including the private members, are included.

  When not in verbose mode, some modules and functions are excluded as well.

  Args:
    component: The component containing the member.
    name: The name of the member.
    member: The member itself.
    class_attrs: (optional) If component is a class, provide this as:
      inspectutils.GetClassAttrsDict(component). If not provided, it will be
      computed.
    verbose: Whether to include private members.
  Returns
    A boolean value indicating whether the member should be included.
  """
  if isinstance(name, str) and name.startswith('__'):
    return False
  if verbose:
    return True
  if (member is absolute_import
      or member is division
      or member is print_function):
    return False
  if isinstance(member, type(absolute_import)):
    return False
  # TODO(dbieber): Determine more generally which modules to hide.
  modules_to_hide = []
  if inspect.ismodule(member) and member in modules_to_hide:
    return False
  if inspect.isclass(component):
    # If class_attrs has not been provided, compute it.
    if class_attrs is None:
      class_attrs = inspectutils.GetClassAttrsDict(component) or {}
    class_attr = class_attrs.get(name)
    if class_attr:
      # Methods and properties should only be accessible on instantiated
      # objects, not on uninstantiated classes.
      if class_attr.kind in ('method', 'property'):
        return False
      # Backward compatibility notes: Before Python 3.8, namedtuple attributes
      # were properties. In Python 3.8, they have type tuplegetter.
      tuplegetter = getattr(collections, '_tuplegetter', type(None))
      if isinstance(class_attr.object, tuplegetter):
        return False
  if isinstance(name, str):
    return not name.startswith('_')
  return True  # Default to including the member


def VisibleMembers(component, class_attrs=None, verbose=False):
  """Returns a list of the members of the given component.

  If verbose is True, then members starting with _ (normally ignored) are
  included.

  Args:
    component: The component whose members to list.
    class_attrs: (optional) If component is a class, you may provide this as:
      inspectutils.GetClassAttrsDict(component). If not provided, it will be
      computed. If provided, this determines how class members will be treated
      for visibility. In particular, methods are generally hidden for
      non-instantiated classes, but if you wish them to be shown (e.g. for
      completion scripts) then pass in a different class_attr for them.
    verbose: Whether to include private members.
  Returns:
    A list of tuples (member_name, member) of all members of the component.
  """
  if isinstance(component, dict):
    members = component.items()
  else:
    members = inspect.getmembers(component)

  # If class_attrs has not been provided, compute it.
  if class_attrs is None:
    class_attrs = inspectutils.GetClassAttrsDict(component)
  return [
      (member_name, member) for member_name, member in members
      if MemberVisible(component, member_name, member, class_attrs=class_attrs,
                       verbose=verbose)
  ]


def _CompletionsFromArgs(fn_args):
  """Takes a list of fn args and returns a list of the fn's completion strings.

  Args:
    fn_args: A list of the args accepted by a function.
  Returns:
    A list of possible completion strings for that function.
  """
  completions = []
  for arg in fn_args:
    arg = arg.replace('_', '-')
    completions.append(f'--{arg}')
  return completions


def Completions(component, verbose=False):
  """Gives possible Fire command completions for the component.

  A completion is a string that can be appended to a command to continue that
  command. These are used for TAB-completions in Bash for Fire CLIs.

  Args:
    component: The component whose completions to list.
    verbose: Whether to include all completions, even private members.
  Returns:
    A list of completions for a command that would so far return the component.
  """
  if inspect.isroutine(component) or inspect.isclass(component):
    spec = inspectutils.GetFullArgSpec(component)
    return _CompletionsFromArgs(spec.args + spec.kwonlyargs)

  if isinstance(component, (tuple, list)):
    return [str(index) for index in range(len(component))]

  if inspect.isgenerator(component):
    # TODO(dbieber): There are currently no commands available for generators.
    return []

  return [
      _FormatForCommand(member_name)
      for member_name, _ in VisibleMembers(component, verbose=verbose)
  ]


def _FormatForCommand(token):
  """Replaces underscores with hyphens, unless the token starts with a token.

  This is because we typically prefer hyphens to underscores at the command
  line, but we reserve hyphens at the start of a token for flags. This becomes
  relevant when --verbose is activated, so that things like __str__ don't get
  transformed into --str--, which would get confused for a flag.

  Args:
    token: The token to transform.
  Returns:
    The transformed token.
  """
  if not isinstance(token, str):
    token = str(token)

  if token.startswith('_'):
    return token

  return token.replace('_', '-')


def _Commands(component, depth=3):
  """Yields tuples representing commands.

  To use the command from Python, insert '.' between each element of the tuple.
  To use the command from the command line, insert ' ' between each element of
  the tuple.

  Args:
    component: The component considered to be the root of the yielded commands.
    depth: The maximum depth with which to traverse the member DAG for commands.
  Yields:
    Tuples, each tuple representing one possible command for this CLI.
    Only traverses the member DAG up to a depth of depth.
  """
  if inspect.isroutine(component) or inspect.isclass(component):
    for completion in Completions(component, verbose=False):
      yield (completion,)
  if inspect.isroutine(component):
    return  # Don't descend into routines.

  if depth < 1:
    return

  # By setting class_attrs={} we don't hide methods in completion.
  for member_name, member in VisibleMembers(component, class_attrs={},
                                            verbose=False):
    # TODO(dbieber): Also skip components we've already seen.
    member_name = _FormatForCommand(member_name)

    yield (member_name,)

    for command in _Commands(member, depth - 1):
      yield (member_name,) + command


def _IsOption(arg):
  return arg.startswith('-')


def _GetMaps(name, commands, default_options):
  """Returns sets of subcommands and options for each command.

  Args:
    name: The first token in the commands, also the name of the command.
    commands: A list of all possible commands that tab completion can complete
        to. Each command is a list or tuple of the string tokens that make up
        that command.
    default_options: A dict of options that can be used with any command. Use
        this if there are flags that can always be appended to a command.
  Returns:
    global_options: A set of all options of the first token of the command.
    subcommands_map: A dict storing set of subcommands for each
        command/subcommand.
    options_map: A dict storing set of options for each subcommand.
  """
  global_options = copy.copy(default_options)
  options_map = collections.defaultdict(lambda: copy.copy(default_options))
  subcommands_map = collections.defaultdict(set)

  for command in commands:
    if len(command) == 1:
      if _IsOption(command[0]):
        global_options.add(command[0])
      else:
        subcommands_map[name].add(command[0])

    elif command:
      subcommand = command[-2]
      arg = _FormatForCommand(command[-1])

      if _IsOption(arg):
        args_map = options_map
      else:
        args_map = subcommands_map

      args_map[subcommand].add(arg)
      args_map[subcommand.replace('_', '-')].add(arg)

  return global_options, options_map, subcommands_map


================================================
FILE: fire/completion_test.py
================================================
# 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.

"""Tests for the completion module."""

from fire import completion
from fire import test_components as tc
from fire import testutils


class TabCompletionTest(testutils.BaseTestCase):

  def testCompletionBashScript(self):
    # A sanity check test to make sure the bash completion script satisfies
    # some basic assumptions.
    commands = [
        ['run'],
        ['halt'],
        ['halt', '--now'],
    ]
    script = completion._BashScript(name='command', commands=commands)  # pylint: disable=protected-access
    self.assertIn('command', script)
    self.assertIn('halt', script)

    for last_command in ['command', 'halt']:
      self.assertIn(f'{last_command})', script)

  def testCompletionFishScript(self):
    # A sanity check test to make sure the fish completion script satisfies
    # some basic assumptions.
    commands = [
        ['run'],
        ['halt'],
        ['halt', '--now'],
    ]
    script = completion._FishScript(name='command', commands=commands)  # pylint: disable=protected-access
    self.assertIn('command', script)
    self.assertIn('halt', script)
    self.assertIn('-l now', script)

  def testFnCompletions(self):
    def example(one, two, three):
      return one, two, three

    completions = completion.Completions(example)
    self.assertIn('--one', completions)
    self.assertIn('--two', completions)
    self.assertIn('--three', completions)

  def testListCompletions(self):
    completions = completion.Completions(['red', 'green', 'blue'])
    self.assertIn('0', completions)
    self.assertIn('1', completions)
    self.assertIn('2', completions)
    self.assertNotIn('3', completions)

  def testDictCompletions(self):
    colors = {
        'red': 'green',
        'blue': 'yellow',
        '_rainbow': True,
    }
    completions = completion.Completions(colors)
    self.assertIn('red', completions)
    self.assertIn('blue', completions)
    self.assertNotIn('green', completions)
    self.assertNotIn('yellow', completions)
    self.assertNotIn('_rainbow', completions)
    self.assertNotIn('True', completions)
    self.assertNotIn(True, completions)

  def testDictCompletionsVerbose(self):
    colors = {
        'red': 'green',
        'blue': 'yellow',
        '_rainbow': True,
    }
    completions = completion.Completions(colors, verbose=True)
    self.assertIn('red', completions)
    self.assertIn('blue', completions)
    self.assertNotIn('green', completions)
    self.assertNotIn('yellow', completions)
    self.assertIn('_rainbow', completions)
    self.assertNotIn('True', completions)
    self.assertNotIn(True, completions)

  def testDeepDictCompletions(self):
    deepdict = {'level1': {'level2': {'level3': {'level4': {}}}}}
    completions = completion.Completions(deepdict)
    self.assertIn('level1', completions)
    self.assertNotIn('level2', completions)

  def testDeepDictScript(self):
    deepdict = {'level1': {'level2': {'level3': {'level4': {}}}}}
    script = completion.Script('deepdict', deepdict)
    self.assertIn('level1', script)
    self.assertIn('level2', script)
    self.assertIn('level3', script)
    self.assertNotIn('level4', script)  # The default depth is 3.

  def testFnScript(self):
    script = completion.Script('identity', tc.identity)
    self.assertIn('--arg1', script)
    self.assertIn('--arg2', script)
    self.assertIn('--arg3', script)
    self.assertIn('--arg4', script)

  def testClassScript(self):
    script = completion.Script('', tc.MixedDefaults)
    self.assertIn('ten', script)
    self.assertIn('sum', script)
    self.assertIn('identity', script)
    self.assertIn('--alpha', script)
    self.assertIn('--beta', script)

  def testDeepDictFishScript(self):
    deepdict = {'level1': {'level2': {'level3': {'level4': {}}}}}
    script = completion.Script('deepdict', deepdict, shell='fish')
    self.assertIn('level1', script)
    self.assertIn('level2', script)
    self.assertIn('level3', script)
    self.assertNotIn('level4', script)  # The default depth is 3.

  def testFnFishScript(self):
    script = completion.Script('identity', tc.identity, shell='fish')
    self.assertIn('arg1', script)
    self.assertIn('arg2', script)
    self.assertIn('arg3', script)
    self.assertIn('arg4', script)

  def testClassFishScript(self):
    script = completion.Script('', tc.MixedDefaults, shell='fish')
    self.assertIn('ten', script)
    self.assertIn('sum', script)
    self.assertIn('identity', script)
    self.assertIn('alpha', script)
    self.assertIn('beta', script)

  def testNonStringDictCompletions(self):
    completions = completion.Completions({
        10: 'green',
        3.14: 'yellow',
        ('t1', 't2'): 'pink',
    })
    self.assertIn('10', completions)
    self.assertIn('3.14', completions)
    self.assertIn("('t1', 't2')", completions)
    self.assertNotIn('green', completions)
    self.assertNotIn('yellow', completions)
    self.assertNotIn('pink', completions)

  def testGeneratorCompletions(self):
    def generator():
      x = 0
      while True:
        yield x
        x += 1
    completions = completion.Completions(generator())
    self.assertEqual(completions, [])

  def testClassCompletions(self):
    completions = completion.Completions(tc.NoDefaults)
    self.assertEqual(completions, [])

  def testObjectCompletions(self):
    completions = completion.Completions(tc.NoDefaults())
    self.assertIn('double', completions)
    self.assertIn('triple', completions)

  def testMethodCompletions(self):
    completions = completion.Completions(tc.NoDefaults().double)
    self.assertNotIn('--self', completions)
    self.assertIn('--count', completions)


if __name__ == '__main__':
  testutils.main()


================================================
FILE: fire/console/README.md
================================================
This is the console package from googlecloudsdk, as used by Python Fire.
Python Fire does not accept pull requests modifying the console package; rather,
changes to console should go through the upstream project googlecloudsdk.


================================================
FILE: fire/console/__init__.py
================================================


================================================
FILE: fire/console/console_attr.py
================================================
# -*- coding: utf-8 -*- #

# Copyright 2015 Google LLC. 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.

r"""A module for console attributes, special characters and functions.

The target architectures {linux, macos, windows} support inline encoding for
all attributes except color. Windows requires win32 calls to manipulate the
console color state.

Usage:

  # Get the console attribute state.
  out = log.out
  con = console_attr.GetConsoleAttr(out=out)

  # Get the ISO 8879:1986//ENTITIES Box and Line Drawing characters.
  box = con.GetBoxLineCharacters()
  # Print an X inside a box.
  out.write(box.dr)
  out.write(box.h)
  out.write(box.dl)
  out.write('\n')
  out.write(box.v)
  out.write('X')
  out.write(box.v)
  out.write('\n')
  out.write(box.ur)
  out.write(box.h)
  out.write(box.ul)
  out.write('\n')

  # Print the bullet characters.
  for c in con.GetBullets():
    out.write(c)
  out.write('\n')

  # Print FAIL in red.
  out.write('Epic ')
  con.Colorize('FAIL', 'red')
  out.write(', my first.')

  # Print italic and bold text.
  bold = con.GetFontCode(bold=True)
  italic = con.GetFontCode(italic=True)
  normal = con.GetFontCode()
  out.write('This is {bold}bold{normal}, this is {italic}italic{normal},'
            ' and this is normal.\n'.format(bold=bold, italic=italic,
                                            normal=normal))

  # Read one character from stdin with echo disabled.
  c = con.GetRawKey()
  if c is None:
    print 'EOF\n'

  # Return the display width of a string that may contain FontCode() chars.
  display_width = con.DisplayWidth(string)

  # Reset the memoized state.
  con = console_attr.ResetConsoleAttr()

  # Print the console width and height in characters.
  width, height = con.GetTermSize()
  print 'width={width}, height={height}'.format(width=width, height=height)

  # Colorize table data cells.
  fail = console_attr.Colorizer('FAIL', 'red')
  pass = console_attr.Colorizer('PASS', 'green')
  cells = ['label', fail, 'more text', pass, 'end']
  for cell in cells;
    if isinstance(cell, console_attr.Colorizer):
      cell.Render()
    else:
      out.write(cell)
"""


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import os
import sys
import unicodedata

# from fire.console import properties
from fire.console import console_attr_os
from fire.console import encoding as encoding_util
from fire.console import text


# TODO: Unify this logic with console.style.mappings
class BoxLineCharacters(object):
  """Box/line drawing characters.

  The element names are from ISO 8879:1986//ENTITIES Box and Line Drawing//EN:
    http://www.w3.org/2003/entities/iso8879doc/isobox.html
  """


class BoxLineCharactersUnicode(BoxLineCharacters):
  """unicode Box/line drawing characters (cp437 compatible unicode)."""
  dl = '┐'
  dr = '┌'
  h = '─'
  hd = '┬'
  hu = '┴'
  ul = '┘'
  ur = '└'
  v = '│'
  vh = '┼'
  vl = '┤'
  vr = '├'
  d_dl = '╗'
  d_dr = '╔'
  d_h = '═'
  d_hd = '╦'
  d_hu = '╩'
  d_ul = '╝'
  d_ur = '╚'
  d_v = '║'
  d_vh = '╬'
  d_vl = '╣'
  d_vr = '╠'


class BoxLineCharactersAscii(BoxLineCharacters):
  """ASCII Box/line drawing characters."""
  dl = '+'
  dr = '+'
  h = '-'
  hd = '+'
  hu = '+'
  ul = '+'
  ur = '+'
  v = '|'
  vh = '+'
  vl = '+'
  vr = '+'
  d_dl = '#'
  d_dr = '#'
  d_h = '='
  d_hd = '#'
  d_hu = '#'
  d_ul = '#'
  d_ur = '#'
  d_v = '#'
  d_vh = '#'
  d_vl = '#'
  d_vr = '#'


class BoxLineCharactersScreenReader(BoxLineCharactersAscii):
  dl = ' '
  dr = ' '
  hd = ' '
  hu = ' '
  ul = ' '
  ur = ' '
  vh = ' '
  vl = ' '
  vr = ' '


class ProgressTrackerSymbols(object):
  """Characters used by progress trackers."""


class ProgressTrackerSymbolsUnicode(ProgressTrackerSymbols):
  """Characters used by progress trackers."""

  @property
  def spin_marks(self):
    return ['⠏', '⠛', '⠹', '⠼', '⠶', '⠧']

  success = text.TypedText(['✓'], text_type=text.TextTypes.PT_SUCCESS)
  failed = text.TypedText(['X'], text_type=text.TextTypes.PT_FAILURE)
  interrupted = '-'
  not_started = '.'
  prefix_length = 2


class ProgressTrackerSymbolsAscii(ProgressTrackerSymbols):
  """Characters used by progress trackers."""

  @property
  def spin_marks(self):
    return ['|', '/', '-', '\\',]

  success = 'OK'
  failed = 'X'
  interrupted = '-'
  not_started = '.'
  prefix_length = 3


class ConsoleAttr(object):
  """Console attribute and special drawing characters and functions accessor.

  Use GetConsoleAttr() to get a global ConsoleAttr object shared by all callers.
  Use ConsoleAttr() for abstracting multiple consoles.

  If _out is not associated with a console, or if the console properties cannot
  be determined, the default behavior is ASCII art with no attributes.

  Attributes:
    _ANSI_COLOR: The ANSI color control sequence dict.
    _ANSI_COLOR_RESET: The ANSI color reset control sequence string.
    _csi: The ANSI Control Sequence indicator string, '' if not supported.
    _encoding: The character encoding.
        ascii: ASCII art. This is the default.
        utf8: UTF-8 unicode.
        win: Windows code page 437.
    _font_bold: The ANSI bold font embellishment code string.
    _font_italic: The ANSI italic font embellishment code string.
    _get_raw_key: A function that reads one keypress from stdin with no echo.
    _out: The console output file stream.
    _term: TERM environment variable value.
    _term_size: The terminal (x, y) dimensions in characters.
  """

  _CONSOLE_ATTR_STATE = None

  _ANSI_COLOR = {
      'red': '31;1m',
      'yellow': '33;1m',
      'green': '32m',
      'blue': '34;1m'
      }
  _ANSI_COLOR_RESET = '39;0m'

  _BULLETS_UNICODE = ('▪', '◆', '▸', '▫', '◇', '▹')
  _BULLETS_WINDOWS = ('■', '≡', '∞', 'Φ', '·')  # cp437 compatible unicode
  _BULLETS_ASCII = ('o', '*', '+', '-')

  def __init__(self, encoding=None, suppress_output=False):
    """Constructor.

    Args:
      encoding: Encoding override.
        ascii -- ASCII art. This is the default.
        utf8 -- UTF-8 unicode.
        win -- Windows code page 437.
      suppress_output: True to create a ConsoleAttr that doesn't want to output
        anything.
    """
    # Normalize the encoding name.
    if not encoding:
      encoding = self._GetConsoleEncoding()
    elif encoding == 'win':
      encoding = 'cp437'
    self._encoding = encoding or 'ascii'
    self._term = '' if suppress_output else os.getenv('TERM', '').lower()

    # ANSI "standard" attributes.
    if self.SupportsAnsi():
      # Select Graphic Rendition parameters from
      # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
      # Italic '3' would be nice here but its not widely supported.
      self._csi = '\x1b['
      self._font_bold = '1'
      self._font_italic = '4'
    else:
      self._csi = None
      self._font_bold = ''
      self._font_italic = ''

    # Encoded character attributes.
    is_screen_reader = False
    if self._encoding == 'utf8' and not is_screen_reader:
      self._box_line_characters = BoxLineCharactersUnicode()
      self._bullets = self._BULLETS_UNICODE
      self._progress_tracker_symbols = ProgressTrackerSymbolsUnicode()
    elif self._encoding == 'cp437' and not is_screen_reader:
      self._box_line_characters = BoxLineCharactersUnicode()
      self._bullets = self._BULLETS_WINDOWS
      # Windows does not support the unicode characters used for the spinner.
      self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()
    else:
      self._box_line_characters = BoxLineCharactersAscii()
      if is_screen_reader:
        self._box_line_characters = BoxLineCharactersScreenReader()
      self._bullets = self._BULLETS_ASCII
      self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()

    # OS specific attributes.
    self._get_raw_key = [console_attr_os.GetRawKeyFunction()]
    self._term_size = (
        (0, 0) if suppress_output else console_attr_os.GetTermSize())

    self._display_width_cache = {}

  def _GetConsoleEncoding(self):
    """Gets the encoding as declared by the stdout stream.

    Returns:
      str, The encoding name or None if it could not be determined.
    """
    console_encoding = getattr(sys.stdout, 'encoding', None)
    if not console_encoding:
      return None
    console_encoding = console_encoding.lower()
    if 'utf-8' in console_encoding:
      return 'utf8'
    elif 'cp437' in console_encoding:
      return 'cp437'
    return None

  def Colorize(self, string, color, justify=None):
    """Generates a colorized string, optionally justified.

    Args:
      string: The string to write.
      color: The color name -- must be in _ANSI_COLOR.
      justify: The justification function, no justification if None. For
        example, justify=lambda s: s.center(10)

    Returns:
      str, The colorized string that can be printed to the console.
    """
    if justify:
      string = justify(string)
    if self._csi and color in self._ANSI_COLOR:
      return '{csi}{color_code}{string}{csi}{reset_code}'.format(
          csi=self._csi,
          color_code=self._ANSI_COLOR[color],
          reset_code=self._ANSI_COLOR_RESET,
          string=string)
    # TODO: Add elif self._encoding == 'cp437': code here.
    return string

  def ConvertOutputToUnicode(self, buf):
    """Converts a console output string buf to unicode.

    Mainly used for testing. Allows test comparisons in unicode while ensuring
    that unicode => encoding => unicode works.

    Args:
      buf: The console output string to convert.

    Returns:
      The console output string buf converted to unicode.
    """
    if isinstance(buf, str):
      buf = buf.encode(self._encoding)
    return str(buf, self._encoding, 'replace')

  def GetBoxLineCharacters(self):
    """Returns the box/line drawing characters object.

    The element names are from ISO 8879:1986//ENTITIES Box and Line Drawing//EN:
      http://www.w3.org/2003/entities/iso8879doc/isobox.html

    Returns:
      A BoxLineCharacters object for the console output device.
    """
    return self._box_line_characters

  def GetBullets(self):
    """Returns the bullet characters list.

    Use the list elements in order for best appearance in nested bullet lists,
    wrapping back to the first element for deep nesting. The list size depends
    on the console implementation.

    Returns:
      A tuple of bullet characters.
    """
    return self._bullets

  def GetProgressTrackerSymbols(self):
    """Returns the progress tracker characters object.

    Returns:
      A ProgressTrackerSymbols object for the console output device.
    """
    return self._progress_tracker_symbols

  def GetControlSequenceIndicator(self):
    """Returns the control sequence indicator string.

    Returns:
      The control sequence indicator string or None if control sequences are not
      supported.
    """
    return self._csi

  def GetControlSequenceLen(self, buf):
    """Returns the control sequence length at the beginning of buf.

    Used in display width computations. Control sequences have display width 0.

    Args:
      buf: The string to check for a control sequence.

    Returns:
      The control sequence length at the beginning of buf or 0 if buf does not
      start with a control sequence.
    """
    if not self._csi or not buf.startswith(self._csi):
      return 0
    n = 0
    for c in buf:
      n += 1
      if c.isalpha():
        break
    return n

  def GetEncoding(self):
    """Returns the current encoding."""
    return self._encoding

  def GetFontCode(self, bold=False, italic=False):
    """Returns a font code string for 0 or more embellishments.

    GetFontCode() with no args returns the default font code string.

    Args:
      bold: True for bold embellishment.
      italic: True for italic embellishment.

    Returns:
      The font code string for the requested embellishments. Write this string
        to the console output to control the font settings.
    """
    if not self._csi:
      return ''
    codes = []
    if bold:
      codes.append(self._font_bold)
    if italic:
      codes.append(self._font_italic)
    return '{csi}{codes}m'.format(csi=self._csi, codes=';'.join(codes))

  def GetRawKey(self):
    """Reads one key press from stdin with no echo.

    Returns:
      The key name, None for EOF, <KEY-*> for function keys, otherwise a
      character.
    """
    return self._get_raw_key[0]()

  def GetTermIdentifier(self):
    """Returns the TERM environment variable for the console.

    Returns:
      str: A str that describes the console's text capabilities
    """
    return self._term

  def GetTermSize(self):
    """Returns the terminal (x, y) dimensions in characters.

    Returns:
      (x, y): A tuple of the terminal x and y dimensions.
    """
    return self._term_size

  def DisplayWidth(self, buf):
    """Returns the display width of buf, handling unicode and ANSI controls.

    Args:
      buf: The string to count from.

    Returns:
      The display width of buf, handling unicode and ANSI controls.
    """
    if not isinstance(buf, str):
      # Handle non-string objects like Colorizer().
      return len(buf)

    cached = self._display_width_cache.get(buf, None)
    if cached is not None:
      return cached

    width = 0
    max_width = 0
    i = 0
    while i < len(buf):
      if self._csi and buf[i:].startswith(self._csi):
        i += self.GetControlSequenceLen(buf[i:])
      elif buf[i] == '\n':
        # A newline incidates the start of a new line.
        # Newline characters have 0 width.
        max_width = max(width, max_width)
        width = 0
        i += 1
      else:
        width += GetCharacterDisplayWidth(buf[i])
        i += 1
    max_width = max(width, max_width)

    self._display_width_cache[buf] = max_width
    return max_width

  def SplitIntoNormalAndControl(self, buf):
    """Returns a list of (normal_string, control_sequence) tuples from buf.

    Args:
      buf: The input string containing one or more control sequences
        interspersed with normal strings.

    Returns:
      A list of (normal_string, control_sequence) tuples.
    """
    if not self._csi or not buf:
      return [(buf, '')]
    seq = []
    i = 0
    while i < len(buf):
      c = buf.find(self._csi, i)
      if c < 0:
        seq.append((buf[i:], ''))
        break
      normal = buf[i:c]
      i = c + self.GetControlSequenceLen(buf[c:])
      seq.append((normal, buf[c:i]))
    return seq

  def SplitLine(self, line, width):
    """Splits line into width length chunks.

    Args:
      line: The line to split.
      width: The width of each chunk except the last which could be smaller than
        width.

    Returns:
      A list of chunks, all but the last with display width == width.
    """
    lines = []
    chunk = ''
    w = 0
    keep = False
    for normal, control in self.SplitIntoNormalAndControl(line):
      keep = True
      while True:
        n = width - w
        w += len(normal)
        if w <= width:
          break
        lines.append(chunk + normal[:n])
        chunk = ''
        keep = False
        w = 0
        normal = normal[n:]
      chunk += normal + control
    if chunk or keep:
      lines.append(chunk)
    return lines

  def SupportsAnsi(self):
    return (self._encoding != 'ascii' and
            ('screen' in self._term or 'xterm' in self._term))


class Colorizer(object):
  """Resource string colorizer.

  Attributes:
    _con: ConsoleAttr object.
    _color: Color name.
    _string: The string to colorize.
    _justify: The justification function, no justification if None. For example,
      justify=lambda s: s.center(10)
  """

  def __init__(self, string, color, justify=None):
    """Constructor.

    Args:
      string: The string to colorize.
      color: Color name used to index ConsoleAttr._ANSI_COLOR.
      justify: The justification function, no justification if None. For
        example, justify=lambda s: s.center(10)
    """
    self._con = GetConsoleAttr()
    self._color = color
    self._string = string
    self._justify = justify

  def __eq__(self, other):
    return self._string == str(other)

  def __ne__(self, other):
    return not self == other

  def __gt__(self, other):
    return self._string > str(other)

  def __lt__(self, other):
    return self._string < str(other)

  def __ge__(self, other):
    return not self < other

  def __le__(self, other):
    return not self > other

  def __len__(self):
    return self._con.DisplayWidth(self._string)

  def __str__(self):
    return self._string

  def Render(self, stream, justify=None):
    """Renders the string as self._color on the console.

    Args:
      stream: The stream to render the string to. The stream given here *must*
        have the same encoding as sys.stdout for this to work properly.
      justify: The justification function, self._justify if None.
    """
    stream.write(
        self._con.Colorize(self._string, self._color, justify or self._justify))


def GetConsoleAttr(encoding=None, reset=False):
  """Gets the console attribute state.

  If this is the first call or reset is True or encoding is not None and does
  not match the current encoding or out is not None and does not match the
  current out then the state is (re)initialized. Otherwise the current state
  is returned.

  This call associates the out file stream with the console. All console related
  output should go to the same stream.

  Args:
    encoding: Encoding override.
      ascii -- ASCII. This is the default.
      utf8 -- UTF-8 unicode.
      win -- Windows code page 437.
    reset: Force re-initialization if True.

  Returns:
    The global ConsoleAttr state object.
  """
  attr = ConsoleAttr._CONSOLE_ATTR_STATE  # pylint: disable=protected-access
  if not reset:
    if not attr:
      reset = True
    elif encoding and encoding != attr.GetEncoding():
      reset = True
  if reset:
    attr = ConsoleAttr(encoding=encoding)
    ConsoleAttr._CONSOLE_ATTR_STATE = attr  # pylint: disable=protected-access
  return attr


def ResetConsoleAttr(encoding=None):
  """Resets the console attribute state to the console default.

  Args:
    encoding: Reset to this encoding instead of the default.
      ascii -- ASCII. This is the default.
      utf8 -- UTF-8 unicode.
      win -- Windows code page 437.

  Returns:
    The global ConsoleAttr state object.
  """
  return GetConsoleAttr(encoding=encoding, reset=True)


def GetCharacterDisplayWidth(char):
  """Returns the monospaced terminal display width of char.

  Assumptions:
    - monospaced display
    - ambiguous or unknown chars default to width 1
    - ASCII control char width is 1 => don't use this for control chars

  Args:
    char: The character to determine the display width of.

  Returns:
    The monospaced terminal display width of char: either 0, 1, or 2.
  """
  if not isinstance(char, str):
    # Non-unicode chars have width 1. Don't use this function on control chars.
    return 1

  # Normalize to avoid special cases.
  char = unicodedata.normalize('NFC', char)

  if unicodedata.combining(char) != 0:
    # Modifies the previous character and does not move the cursor.
    return 0
  elif unicodedata.category(char) == 'Cf':
    # Unprintable formatting char.
    return 0
  elif unicodedata.east_asian_width(char) in 'FW':
    # Fullwidth or Wide chars take 2 character positions.
    return 2
  else:
    # Don't use this function on control chars.
    return 1


def SafeText(data, encoding=None, escape=True):
  br"""Converts the data to a text string compatible with the given encoding.

  This works the same way as Decode() below except it guarantees that any
  characters in the resulting text string can be re-encoded using the given
  encoding (or GetConsoleAttr().GetEncoding() if None is given). This means
  that the string will be safe to print to sys.stdout (for example) without
  getting codec exceptions if the user's terminal doesn't support the encoding
  used by the source of the text.

  Args:
    data: Any bytes, string, or object that has str() or unicode() methods.
    encoding: The encoding name to ensure compatibility with. Defaults to
      GetConsoleAttr().GetEncoding().
    escape: Replace unencodable characters with a \uXXXX or \xXX equivalent if
      True. Otherwise replace unencodable characters with an appropriate unknown
      character, '?' for ASCII, and the unicode unknown replacement character
      \uFFFE for unicode.

  Returns:
    A text string representation of the data, but modified to remove any
    characters that would result in an encoding exception with the target
    encoding. In the worst case, with escape=False, it will contain only ?
    characters.
  """
  if data is None:
    return 'None'
  encoding = encoding or GetConsoleAttr().GetEncoding()
  string = encoding_util.Decode(data, encoding=encoding)

  try:
    # No change needed if the string encodes to the output encoding.
    string.encode(encoding)
    return string
  except UnicodeError:
    # The string does not encode to the output encoding. Encode it with error
    # handling then convert it back into a text string (which will be
    # guaranteed to only contain characters that can be encoded later.
    return (string
            .encode(encoding, 'backslashreplace' if escape else 'replace')
            .decode(encoding))


def EncodeToBytes(data):
  r"""Encode data to bytes.

  The primary use case is for base64/mime style 7-bit ascii encoding where the
  encoder input must be bytes. "safe" means that the conversion always returns
  bytes and will not raise codec exceptions.

  If data is text then an 8-bit ascii encoding is attempted, then the console
  encoding, and finally utf-8.

  Args:
    data: Any bytes, string, or object that has str() or unicode() methods.

  Returns:
    A bytes string representation of the data.
  """
  if data is None:
    return b''
  if isinstance(data, bytes):
    # Already bytes - our work is done.
    return data

  # Coerce to text that will be converted to bytes.
  s = str(data)

  try:
    # Assume the text can be directly converted to bytes (8-bit ascii).
    return s.encode('iso-8859-1')
  except UnicodeEncodeError:
    pass

  try:
    # Try the output encoding.
    return s.encode(GetConsoleAttr().GetEncoding())
  except UnicodeEncodeError:
    pass

  # Punt to utf-8.
  return s.encode('utf-8')


def Decode(data, encoding=None):
  """Converts the given string, bytes, or object to a text string.

  Args:
    data: Any bytes, string, or object that has str() or unicode() methods.
    encoding: A suggesting encoding used to decode. If this encoding doesn't
      work, other defaults are tried. Defaults to
      GetConsoleAttr().GetEncoding().

  Returns:
    A text string representation of the data.
  """
  encoding = encoding or GetConsoleAttr().GetEncoding()
  return encoding_util.Decode(data, encoding=encoding)


================================================
FILE: fire/console/console_attr_os.py
================================================
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. 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.

"""OS specific console_attr helper functions."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import os
import sys

from fire.console import encoding


def GetTermSize():
  """Gets the terminal x and y dimensions in characters.

  _GetTermSize*() helper functions taken from:
    http://stackoverflow.com/questions/263890/

  Returns:
    (columns, lines): A tuple containing the terminal x and y dimensions.
  """
  xy = None
  # Believe the first helper that doesn't bail.
  for get_terminal_size in (_GetTermSizePosix,
                            _GetTermSizeWindows,
                            _GetTermSizeEnvironment,
                            _GetTermSizeTput):
    try:
      xy = get_terminal_size()
      if xy:
        break
    except:  # pylint: disable=bare-except
      pass
  return xy or (80, 24)


def _GetTermSizePosix():
  """Returns the Posix terminal x and y dimensions."""
  # pylint: disable=g-import-not-at-top
  import fcntl
  # pylint: disable=g-import-not-at-top
  import struct
  # pylint: disable=g-import-not-at-top
  import termios

  def _GetXY(fd):
    """Returns the terminal (x,y) size for fd.

    Args:
      fd: The terminal file descriptor.

    Returns:
      The terminal (x,y) size for fd or None on error.
    """
    try:
      # This magic incantation converts a struct from ioctl(2) containing two
      # binary shorts to a (rows, columns) int tuple.
      rc = struct.unpack(b'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, b'junk'))
      return (rc[1], rc[0]) if rc else None
    except:  # pylint: disable=bare-except
      return None

  xy = _GetXY(0) or _GetXY(1) or _GetXY(2)
  if not xy:
    fd = None
    try:
      fd = os.open(os.ctermid(), os.O_RDONLY)
      xy = _GetXY(fd)
    except:  # pylint: disable=bare-except
      xy = None
    finally:
      if fd is not None:
        os.close(fd)
  return xy


def _GetTermSizeWindows():
  """Returns the Windows terminal x and y dimensions."""
  # pylint:disable=g-import-not-at-top
  import struct
  # pylint: disable=g-import-not-at-top
  from ctypes import create_string_buffer
  # pylint:disable=g-import-not-at-top
  from ctypes import windll

  # stdin handle is -10
  # stdout handle is -11
  # stderr handle is -12

  h = windll.kernel32.GetStdHandle(-12)
  csbi = create_string_buffer(22)
  if not windll.kernel32.GetConsoleScreenBufferInfo(h, csbi):
    return None
  (unused_bufx, unused_bufy, unused_curx, unused_cury, unused_wattr,
   left, top, right, bottom,
   unused_maxx, unused_maxy) = struct.unpack(b'hhhhHhhhhhh', csbi.raw)
  x = right - left + 1
  y = bottom - top + 1
  return (x, y)


def _GetTermSizeEnvironment():
  """Returns the terminal x and y dimensions from the environment."""
  return (int(os.environ['COLUMNS']), int(os.environ['LINES']))


def _GetTermSizeTput():
  """Returns the terminal x and y dimensions from tput(1)."""
  import subprocess  # pylint: disable=g-import-not-at-top
  output = encoding.Decode(subprocess.check_output(['tput', 'cols'],
                                                   stderr=subprocess.STDOUT))
  cols = int(output)
  output = encoding.Decode(subprocess.check_output(['tput', 'lines'],
                                                   stderr=subprocess.STDOUT))
  rows = int(output)
  return (cols, rows)


_ANSI_CSI = '\x1b'  # ANSI control sequence indicator (ESC)
_CONTROL_D = '\x04'  # unix EOF (^D)
_CONTROL_Z = '\x1a'  # Windows EOF (^Z)
_WINDOWS_CSI_1 = '\x00'  # Windows control sequence indicator #1
_WINDOWS_CSI_2 = '\xe0'  # Windows control sequence indicator #2


def GetRawKeyFunction():
  """Returns a function that reads one keypress from stdin with no echo.

  Returns:
    A function that reads one keypress from stdin with no echo or a function
    that always returns None if stdin does not support it.
  """
  # Believe the first helper that doesn't bail.
  for get_raw_key_function in (_GetRawKeyFunctionPosix,
                               _GetRawKeyFunctionWindows):
    try:
      return get_raw_key_function()
    except:  # pylint: disable=bare-except
      pass
  return lambda: None


def _GetRawKeyFunctionPosix():
  """_GetRawKeyFunction helper using Posix APIs."""
  # pylint: disable=g-import-not-at-top
  import tty
  # pylint: disable=g-import-not-at-top
  import termios

  def _GetRawKeyPosix():
    """Reads and returns one keypress from stdin, no echo, using Posix APIs.

    Returns:
      The key name, None for EOF, <*> for function keys, otherwise a
      character.
    """
    ansi_to_key = {
        'A': '<UP-ARROW>',
        'B': '<DOWN-ARROW>',
        'D': '<LEFT-ARROW>',
        'C': '<RIGHT-ARROW>',
        '5': '<PAGE-UP>',
        '6': '<PAGE-DOWN>',
        'H': '<HOME>',
        'F': '<END>',
        'M': '<DOWN-ARROW>',
        'S': '<PAGE-UP>',
        'T': '<PAGE-DOWN>',
    }

    # Flush pending output. sys.stdin.read() would do this, but it's explicitly
    # bypassed in _GetKeyChar().
    sys.stdout.flush()

    fd = sys.stdin.fileno()

    def _GetKeyChar():
      return encoding.Decode(os.read(fd, 1))

    old_settings = termios.tcgetattr(fd)
    try:
      tty.setraw(fd)
      c = _GetKeyChar()
      if c == _ANSI_CSI:
        c = _GetKeyChar()
        while True:
          if c == _ANSI_CSI:
            return c
          if c.isalpha():
            break
          prev_c = c
          c = _GetKeyChar()
          if c == '~':
            c = prev_c
            break
        return ansi_to_key.get(c, '')
    except:  # pylint:disable=bare-except
      c = None
    finally:
      termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return None if c in (_CONTROL_D, _CONTROL_Z) else c

  return _GetRawKeyPosix


def _GetRawKeyFunctionWindows():
  """_GetRawKeyFunction helper using Windows APIs."""
  # pylint: disable=g-import-not-at-top
  import msvcrt

  def _GetRawKeyWindows():
    """Reads and returns one keypress from stdin, no echo, using Windows APIs.

    Returns:
      The key name, None for EOF, <*> for function keys, otherwise a
      character.
    """
    windows_to_key = {
        'H': '<UP-ARROW>',
        'P': '<DOWN-ARROW>',
        'K': '<LEFT-ARROW>',
        'M': '<RIGHT-ARROW>',
        'I': '<PAGE-UP>',
        'Q': '<PAGE-DOWN>',
        'G': '<HOME>',
        'O': '<END>',
    }

    # Flush pending output. sys.stdin.read() would do this it's explicitly
    # bypassed in _GetKeyChar().
    sys.stdout.flush()

    def _GetKeyChar():
      return encoding.Decode(msvcrt.getch())

    c = _GetKeyChar()
    # Special function key is a two character sequence; return the second char.
    if c in (_WINDOWS_CSI_1, _WINDOWS_CSI_2):
      return windows_to_key.get(_GetKeyChar(), '')
    return None if c in (_CONTROL_D, _CONTROL_Z) else c

  return _GetRawKeyWindows


================================================
FILE: fire/console/console_io.py
================================================
# -*- coding: utf-8 -*- #
# Copyright 2013 Google LLC. 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.

"""General console printing utilities used by the Cloud SDK."""

import os
import signal
import subprocess
import sys

from fire.console import console_attr
from fire.console import console_pager
from fire.console import encoding
from fire.console import files


def IsInteractive(output=False, error=False, heuristic=False):
  """Determines if the current terminal session is interactive.

  sys.stdin must be a terminal input stream.

  Args:
    output: If True then sys.stdout must also be a terminal output stream.
    error: If True then sys.stderr must also be a terminal output stream.
    heuristic: If True then we also do some additional heuristics to check if
               we are in an interactive context. Checking home path for example.

  Returns:
    True if the current terminal session is interactive.
  """
  if not sys.stdin.isatty():
    return False
  if output and not sys.stdout.isatty():
    return False
  if error and not sys.stderr.isatty():
    return False

  if heuristic:
    # Check the home path. Most startup scripts for example are executed by
    # users that don't have a home path set. Home is OS dependent though, so
    # check everything.
    # *NIX OS usually sets the HOME env variable. It is usually '/home/user',
    # but can also be '/root'. If it's just '/' we are most likely in an init
    # script.
    # Windows usually sets HOMEDRIVE and HOMEPATH. If they don't exist we are
    # probably being run from a task scheduler context. HOMEPATH can be '\'
    # when a user has a network mapped home directory.
    # Cygwin has it all! Both Windows and Linux. Checking both is perfect.
    home = os.getenv('HOME')
    homepath = os.getenv('HOMEPATH')
    if not homepath and (not home or home == '/'):
      return False
  return True


def More(contents, out, prompt=None, check_pager=True):
  """Run a user specified pager or fall back to the internal pager.

  Args:
    contents: The entire contents of the text lines to page.
    out: The output stream.
    prompt: The page break prompt.
    check_pager: Checks the PAGER env var and uses it if True.
  """
  if not IsInteractive(output=True):
    out.write(contents)
    return
  if check_pager:
    pager = encoding.GetEncodedValue(os.environ, 'PAGER', None)
    if pager == '-':
      # Use the fallback Pager.
      pager = None
    elif not pager:
      # Search for a pager that handles ANSI escapes.
      for command in ('less', 'pager'):
        if files.FindExecutableOnPath(command):
          pager = command
          break
    if pager:
      # If the pager is less(1) then instruct it to display raw ANSI escape
      # sequences to enable colors and font embellishments.
      less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None)
      less = '-R' + (less_orig or '')
      encoding.SetEncodedValue(os.environ, 'LESS', less)
      # Ignore SIGINT while the pager is running.
      # We don't want to terminate the parent while the child is still alive.
      signal.signal(signal.SIGINT, signal.SIG_IGN)
      p = subprocess.Popen(pager, stdin=subprocess.PIPE, shell=True)
      enc = console_attr.GetConsoleAttr().GetEncoding()
      p.communicate(input=contents.encode(enc))
      p.wait()
      # Start using default signal handling for SIGINT again.
      signal.signal(signal.SIGINT, signal.SIG_DFL)
      if less_orig is None:
        encoding.SetEncodedValue(os.environ, 'LESS', None)
      return
  # Fall back to the internal pager.
  console_pager.Pager(contents, out, prompt).Run()


================================================
FILE: fire/console/console_pager.py
================================================
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. 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.

"""Simple console pager."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import re
import sys

from fire.console import console_attr


class Pager(object):
  """A simple console text pager.

  This pager requires the entire contents to be available. The contents are
  written one page of lines at a time. The prompt is written after each page of
  lines. A one character response is expected. See HELP_TEXT below for more
  info.

  The contents are written as is. For example, ANSI control codes will be in
  effect. This is different from pagers like more(1) which is ANSI control code
  agnostic and miscalculates line lengths, and less(1) which displays control
  character names by default.

  Attributes:
    _attr: The current ConsoleAttr handle.
    _clear: A string that clears the prompt when written to _out.
    _contents: The entire contents of the text lines to page.
    _height: The terminal height in characters.
    _out: The output stream, log.out (effectively) if None.
    _prompt: The page break prompt.
    _search_direction: The search direction command, n:forward, N:reverse.
    _search_pattern: The current forward/reverse search compiled RE.
    _width: The termonal width in characters.
  """

  HELP_TEXT = """
  Simple pager commands:

    b, ^B, <PAGE-UP>, <LEFT-ARROW>
      Back one page.
    f, ^F, <SPACE>, <PAGE-DOWN>, <RIGHT-ARROW>
      Forward one page. Does not quit if there are no more lines.
    g, <HOME>
      Back to the first page.
    <number>g
      Go to <number> lines from the top.
    G, <END>
      Forward to the last page.
    <number>G
      Go to <number> lines from the bottom.
    h
      Print pager command help.
    j, +, <DOWN-ARROW>
      Forward one line.
    k, -, <UP-ARROW>
      Back one line.
    /pattern
      Forward search for pattern.
    ?pattern
      Backward search for pattern.
    n
      Repeat current search.
    N
      Repeat current search in the opposite direction.
    q, Q, ^C, ^D, ^Z
      Quit return to the caller.
    any other character
      Prompt again.

  Hit any key to continue:"""

  PREV_POS_NXT_REPRINT = -1, -1

  def __init__(self, contents, out=None, prompt=None):
    """Constructor.

    Args:
      contents: The entire contents of the text lines to page.
      out: The output stream, log.out (effectively) if None.
      prompt: The page break prompt, a default prompt is used if None..
    """
    self._contents = contents
    self._out = out or sys.stdout
    self._search_pattern = None
    self._search_direction = None

    # prev_pos, prev_next values to force reprint
    self.prev_pos, self.prev_nxt = self.PREV_POS_NXT_REPRINT
    # Initialize the console attributes.
    self._attr = console_attr.GetConsoleAttr()
    self._width, self._height = self._attr.GetTermSize()

    # Initialize the prompt and the prompt clear string.
    if not prompt:
      prompt = '{bold}--({{percent}}%)--{normal}'.format(
          bold=self._attr.GetFontCode(bold=True),
          normal=self._attr.GetFontCode())
    self._clear = '\r{0}\r'.format(' ' * (self._attr.DisplayWidth(prompt) - 6))
    self._prompt = prompt

    # Initialize a list of lines with long lines split into separate display
    # lines.
    self._lines = []
    for line in contents.splitlines():
      self._lines += self._attr.SplitLine(line, self._width)

  def _Write(self, s):
    """Mockable helper that writes s to self._out."""
    self._out.write(s)

  def _GetSearchCommand(self, c):
    """Consumes a search command and returns the equivalent pager command.

    The search pattern is an RE that is pre-compiled and cached for subsequent
    /<newline>, ?<newline>, n, or N commands.

    Args:
      c: The search command char.

    Returns:
      The pager command char.
    """
    self._Write(c)
    buf = ''
    while True:
      p = self._attr.GetRawKey()
      if p in (None, '\n', '\r') or len(p) != 1:
        break
      self._Write(p)
      buf += p
    self._Write('\r' + ' ' * len(buf) + '\r')
    if buf:
      try:
        self._search_pattern = re.compile(buf)
      except re.error:
        # Silently ignore pattern errors.
        self._search_pattern = None
        return ''
    self._search_direction = 'n' if c == '/' else 'N'
    return 'n'

  def _Help(self):
    """Print command help and wait for any character to continue."""
    clear = self._height - (len(self.HELP_TEXT) -
                            len(self.HELP_TEXT.replace('\n', '')))
    if clear > 0:
      self._Write('\n' * clear)
    self._Write(self.HELP_TEXT)
    self._attr.GetRawKey()
    self._Write('\n')

  def Run(self):
    """Run the pager."""
    # No paging if the contents are small enough.
    if len(self._lines) <= self._height:
      self._Write(self._contents)
      return

    # We will not always reset previous values.
    reset_prev_values = True
    # Save room for the prompt at the bottom of the page.
    self._height -= 1

    # Loop over all the pages.
    pos = 0
    while pos < len(self._lines):
      # Write a page of lines.
      nxt = pos + self._height
      if nxt > len(self._lines):
        nxt = len(self._lines)
        pos = nxt - self._height
      # Checks if the starting position is in between the current printed lines
      # so we don't need to reprint all the lines.
      if self.prev_pos < pos < self.prev_nxt:
        # we start where the previous page ended.
        self._Write('\n'.join(self._lines[self.prev_nxt:nxt]) + '\n')
      elif pos != self.prev_pos and nxt != self.prev_nxt:
        self._Write('\n'.join(self._lines[pos:nxt]) + '\n')

      # Handle the prompt response.
      percent = self._prompt.format(percent=100 * nxt // len(self._lines))
      digits = ''
      while True:
        # We want to reset prev values if we just exited out of the while loop
        if reset_prev_values:
          self.prev_pos, self.prev_nxt = pos, nxt
          reset_prev_values = False
        self._Write(percent)
        c = self._attr.GetRawKey()
        self._Write(self._clear)

        # Parse the command.
        if c in (None,    # EOF.
                 'q',     # Quit.
                 'Q',     # Quit.
                 '\x03',  # ^C  (unix & windows terminal interrupt)
                 '\x1b',  # ESC.
                ):
          # Quit.
          return
        elif c in ('/', '?'):
          c = self._GetSearchCommand(c)
        elif c.isdigit():
          # Collect digits for operation count.
          digits += c
          continue

        # Set the optional command count.
        if digits:
          count = int(digits)
          digits = ''
        else:
          count = 0

        # Finally commit to command c.
        if c in ('<PAGE-UP>', '<LEFT-ARROW>', 'b', '\x02'):
          # Previous page.
          nxt = pos - self._height
          if nxt < 0:
            nxt = 0
        elif c in ('<PAGE-DOWN>', '<RIGHT-ARROW>', 'f', '\x06', ' '):
          # Next page.
          if nxt >= len(self._lines):
            continue
          nxt = pos + self._height
          if nxt >= len(self._lines):
            nxt = pos
        elif c in ('<HOME>', 'g'):
          # First page.
          nxt = count - 1
          if nxt > len(self._lines) - self._height:
            nxt = len(self._lines) - self._height
          if nxt < 0:
            nxt = 0
        elif c in ('<END>', 'G'):
          # Last page.
          nxt = len(self._lines) - count
          if nxt > len(self._lines) - self._height:
            nxt = len(self._lines) - self._height
          if nxt < 0:
            nxt = 0
        elif c == 'h':
          self._Help()
          # Special case when we want to reprint the previous display.
          self.prev_pos, self.prev_nxt = self.PREV_POS_NXT_REPRINT
          nxt = pos
          break
        elif c in ('<DOWN-ARROW>', 'j', '+', '\n', '\r'):
          # Next line.
          if nxt >= len(self._lines):
            continue
          nxt = pos + 1
          if nxt >= len(self._lines):
            nxt = pos
        elif c in ('<UP-ARROW>', 'k', '-'):
          # Previous line.
          nxt = pos - 1
          if nxt < 0:
            nxt = 0
        elif c in ('n', 'N'):
          # Next pattern match search.
          if not self._search_pattern:
            continue
          nxt = pos
          i = pos
          direction = 1 if c == self._search_direction else -1
          while True:
            i += direction
            if i < 0 or i >= len(self._lines):
              break
            if self._search_pattern.search(self._lines[i]):
              nxt = i
              break
        else:
          # Silently ignore everything else.
          continue
        if nxt != pos:
          # We will exit the while loop because position changed so we can reset
          # prev values.
          reset_prev_values = True
          break
      pos = nxt


================================================
FILE: fire/console/encoding.py
================================================
# -*- coding: utf-8 -*- #

# Copyright 2015 Google LLC. 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.

"""A module for dealing with unknown string and environment encodings."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import sys


def Encode(string, encoding=None):
  """Encode the text string to a byte string.

  Args:
    string: str, The text string to encode.
    encoding: The suggested encoding if known.

  Returns:
    str, The binary string.
  """
  del encoding  # Unused.
  return string


def Decode(data, encoding=None):
  """Returns string with non-ascii characters decoded to UNICODE.

  UTF-8, the suggested encoding, and the usual suspects will be attempted in
  order.

  Args:
    data: A string or object that has str() and unicode() methods that may
      contain an encoding incompatible with the standard output encoding.
    encoding: The suggested encoding if known.

  Returns:
    A text string representing the decoded byte string.
  """
  if data is None:
    return None

  # First we are going to get the data object to be a text string.
  if isinstance(data, str) or isinstance(data, bytes):
    string = data
  else:
    # Some non-string type of object.
    string = str(data)

  if isinstance(string, str):
    # Our work is done here.
    return string

  try:
    # Just return the string if its pure ASCII.
    return string.decode('ascii')
  except UnicodeError:
    # The string is not ASCII encoded.
    pass

  # Try the suggested encoding if specified.
  if encoding:
    try:
      return string.decode(encoding)
    except UnicodeError:
      # Bad suggestion.
      pass

  # Try UTF-8 because the other encodings could be extended ASCII. It would
  # be exceptional if a valid extended ascii encoding with extended chars
  # were also a valid UITF-8 encoding.
  try:
    return string.decode('utf8')
  except UnicodeError:
    # Not a UTF-8 encoding.
    pass

  # Try the filesystem encoding.
  try:
    return string.decode(sys.getfilesystemencoding())
  except UnicodeError:
    # string is not encoded for filesystem paths.
    pass

  # Try the system default encoding.
  try:
    return string.decode(sys.getdefaultencoding())
  except UnicodeError:
    # string is not encoded using the default encoding.
    pass

  # We don't know the string encoding.
  # This works around a Python str.encode() "feature" that throws
  # an ASCII *decode* exception on str strings that contain 8th bit set
  # bytes. For example, this sequence throws an exception:
  #   string = '\xdc'  # iso-8859-1 'Ü'
  #   string = string.encode('ascii', 'backslashreplace')
  # even though 'backslashreplace' is documented to handle encoding
  # errors. We work around the problem by first decoding the str string
  # from an 8-bit encoding to unicode, selecting any 8-bit encoding that
  # uses all 256 bytes (such as ISO-8559-1):
  #   string = string.decode('iso-8859-1')
  # Using this produces a sequence that works:
  #   string = '\xdc'
  #   string = string.decode('iso-8859-1')
  #   string = string.encode('ascii', 'backslashreplace')
  return string.decode('iso-8859-1')


def GetEncodedValue(env, name, default=None):
  """Returns the decoded value of the env var name.

  Args:
    env: {str: str}, The env dict.
    name: str, The env var name.
    default: The value to return if name is not in env.

  Returns:
    The decoded value of the env var name.
  """
  name = Encode(name)
  value = env.get(name)
  if value is None:
    return default
  # In Python 3, the environment sets and gets accept and return text strings
  # only, and it handles the encoding itself so this is not necessary.
  return Decode(value)


def SetEncodedValue(env, name, value, encoding=None):
  """Sets the value of name in env to an encoded value.

  Args:
    env: {str: str}, The env dict.
    name: str, The env var name.
    value: str or unicode, The value for name. If None then name is removed from
      env.
    encoding: str, The encoding to use or None to try to infer it.
  """
  # Python 2 *and* 3 unicode support falls apart at filesystem/argv/environment
  # boundaries. The encoding used for filesystem paths and environment variable
  # names/values is under user control on most systems. With one of those values
  # in hand there is no way to tell exactly how the value was encoded. We get
  # some reasonable hints from sys.getfilesystemencoding() or
  # sys.getdefaultencoding() and use them to encode values that the receiving
  # process will have a chance at decoding. Leaving the values as unicode
  # strings will cause os module Unicode exceptions. What good is a language
  # unicode model when the module support could care less?
  name = Encode(name, encoding=encoding)
  if value is None:
    env.pop(name, None)
    return
  env[name] = Encode(value, encoding=encoding)


def EncodeEnv(env, encoding=None):
  """Encodes all the key value pairs in env in preparation for subprocess.

  Args:
    env: {str: str}, The environment you are going to pass to subprocess.
    encoding: str, The encoding to use or None to use the default.

  Returns:
    {bytes: bytes}, The environment to pass to subprocess.
  """
  encoding = encoding or _GetEncoding()
  return {
      Encode(k, encoding=encoding): Encode(v, encoding=encoding)
      for k, v in env.items()
  }


def _GetEncoding():
  """Gets the default encoding to use."""
  return sys.getfilesystemencoding() or sys.getdefaultencoding()


================================================
FILE: fire/console/files.py
================================================
# -*- coding: utf-8 -*- #
# Copyright 2013 Google LLC. 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.

"""Some general file utilities used that can be used by the Cloud SDK."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import os

from fire.console import encoding as encoding_util
from fire.console import platforms


def _GetSystemPath():
  """Returns properly encoded system PATH variable string."""
  return encoding_util.GetEncodedValue(os.environ, 'PATH')


def _FindExecutableOnPath(executable, path, pathext):
  """Internal function to a find an executable.

  Args:
    executable: The name of the executable to find.
    path: A list of directories to search separated by 'os.pathsep'.
    pathext: An iterable of file name extensions to use.

  Returns:
    str, the path to a file on `path` with name `executable` + `p` for
      `p` in `pathext`.

  Raises:
    ValueError: invalid input.
  """

  if isinstance(pathext, str):
    raise ValueError('_FindExecutableOnPath(..., pathext=\'{0}\') failed '
                     'because pathext must be an iterable of strings, but got '
                     'a string.'.format(pathext))

  # Prioritize preferred extension over earlier in path.
  for ext in pathext:
    for directory in path.split(os.pathsep):
      # Windows can have paths quoted.
      directory = directory.strip('"')
      full = os.path.normpath(os.path.join(directory, executable) + ext)
      # On Windows os.access(full, os.X_OK) is always True.
      if os.path.isfile(full) and os.access(full, os.X_OK):
        return full
  return None


def _PlatformExecutableExtensions(platform):
  if platform == platforms.OperatingSystem.WINDOWS:
    return ('.exe', '.cmd', '.bat', '.com', '.ps1')
  else:
    return ('', '.sh')


def FindExecutableOnPath(executable, path=None, pathext=None,
                         allow_extensions=False):
  """Searches for `executable` in the directories listed in `path` or $PATH.

  Executable must not contain a directory or an extension.

  Args:
    executable: The name of the executable to find.
    path: A list of directories to search separated by 'os.pathsep'.  If None
      then the system PATH is used.
    pathext: An iterable of file name extensions to use.  If None then
      platform specific extensions are used.
    allow_extensions: A boolean flag indicating whether extensions in the
      executable are allowed.

  Returns:
    The path of 'executable' (possibly with a platform-specific extension) if
    found and executable, None if not found.

  Raises:
    ValueError: if executable has a path or an extension, and extensions are
      not allowed, or if there's an internal error.
  """

  if not allow_extensions and os.path.splitext(executable)[1]:
    raise ValueError('FindExecutableOnPath({0},...) failed because first '
                     'argument must not have an extension.'.format(executable))

  if os.path.dirname(executable):
    raise ValueError('FindExecutableOnPath({0},...) failed because first '
                     'argument must not have a path.'.format(executable))

  if path is None:
    effective_path = _GetSystemPath()
  else:
    effective_path = path
  effective_pathext = (pathext if pathext is not None
                       else _PlatformExecutableExtensions(
                           platforms.OperatingSystem.Current()))

  return _FindExecutableOnPath(executable, effective_path,
                               effective_pathext)


================================================
FILE: fire/console/platforms.py
================================================
# -*- coding: utf-8 -*- #
# Copyright 2013 Google LLC. 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.

"""Utilities for determining the current platform and architecture."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import os
import platform
import subprocess
import sys


class Error(Exception):
  """Base class for exceptions in the platforms module."""
  pass


class InvalidEnumValue(Error):  # pylint: disable=g-bad-exception-name
  """Exception for when a string could not be parsed to a valid enum value."""

  def __init__(self, given, enum_type, options):
    """Constructs a new exception.

    Args:
      given: str, The given string that could not be parsed.
      enum_type: str, The human readable name of the enum you were trying to
        parse.
      options: list(str), The valid values for this enum.
    """
    super(InvalidEnumValue, self).__init__(
        'Could not parse [{0}] into a valid {1}.  Valid values are [{2}]'
        .format(given, enum_type, ', '.join(options)))


class OperatingSystem(object):
  """An enum representing the operating system you are running on."""

  class _OS(object):
    """A single operating system."""

    # pylint: disable=redefined-builtin
    def __init__(self, id, name, file_name):
      self.id = id
      self.name = name
      self.file_name = file_name

    def __str__(self):
      return self.id

    def __eq__(self, other):
      return (isinstance(other, type(self)) and
              self.id == other.id and
              self.name == other.name and
              self.file_name == other.file_name)

    def __hash__(self):
      return hash(self.id) + hash(self.name) + hash(self.file_name)

    def __ne__(self, other):
      return not self == other

    @classmethod
    def _CmpHelper(cls, x, y):
      """Just a helper equivalent to the cmp() function in Python 2."""
      return (x > y) - (x < y)

    def __lt__(self, other):
      return self._CmpHelper(
          (self.id, self.name, self.file_name),
          (other.id, other.name, other.file_name)) < 0

    def __gt__(self, other):
      return self._CmpHelper(
          (self.id, self.name, self.file_name),
          (other.id, other.name, other.file_name)) > 0

    def __le__(self, other):
      return not self.__gt__(other)

    def __ge__(self, other):
      return not self.__lt__(other)

  WINDOWS = _OS('WINDOWS', 'Windows', 'windows')
  MACOSX = _OS('MACOSX', 'Mac OS X', 'darwin')
  LINUX = _OS('LINUX', 'Linux', 'linux')
  CYGWIN = _OS('CYGWIN', 'Cygwin', 'cygwin')
  MSYS = _OS('MSYS', 'Msys', 'msys')
  _ALL = [WINDOWS, MACOSX, LINUX, CYGWIN, MSYS]

  @staticmethod
  def AllValues():
    """Gets all possible enum values.

    Returns:
      list, All the enum values.
    """
    return list(OperatingSystem._ALL)

  @staticmethod
  def FromId(os_id, error_on_unknown=True):
    """Gets the enum corresponding to the given operating system id.

    Args:
      os_id: str, The operating system id to parse
      error_on_unknown: bool, True to raise an exception if the id is unknown,
        False to just return None.

    Raises:
      InvalidEnumValue: If the given value cannot be parsed.

    Returns:
      OperatingSystemTuple, One of the OperatingSystem constants or None if the
      input is None.
    """
    if not os_id:
      return None
    for operating_system in OperatingSystem._ALL:
      if operating_system.id == os_id:
        return operating_system
    if error_on_unknown:
      raise InvalidEnumValue(os_id, 'Operating System',
                             [value.id for value in OperatingSystem._ALL])
    return None

  @staticmethod
  def Current():
    """Determines the current operating system.

    Returns:
      OperatingSystemTuple, One of the OperatingSystem constants or None if it
      cannot be determined.
    """
    if os.name == 'nt':
      return OperatingSystem.WINDOWS
    elif 'linux' in sys.platform:
      return OperatingSystem.LINUX
    elif 'darwin' in sys.platform:
      return OperatingSystem.MACOSX
    elif 'cygwin' in sys.platform:
      return OperatingSystem.CYGWIN
    elif 'msys' in sys.platform:
      return OperatingSystem.MSYS
    return None

  @staticmethod
  def IsWindows():
    """Returns True if the current operating system is Windows."""
    return OperatingSystem.Current() is OperatingSystem.WINDOWS


class Architecture(object):
  """An enum representing the system architecture you are running on."""

  class _ARCH(object):
    """A single architecture."""

    # pylint: disable=redefined-builtin
    def __init__(self, id, name, file_name):
      self.id = id
      self.name = name
      self.file_name = file_name

    def __str__(self):
      return self.id

    def __eq__(self, other):
      return (isinstance(other, type(self)) and
              self.id == other.id and
              self.name == other.name and
              self.file_name == other.file_name)

    def __hash__(self):
      return hash(self.id) + hash(self.name) + hash(self.file_name)

    def __ne__(self, other):
      return not self == other

    @classmethod
    def _CmpHelper(cls, x, y):
      """Just a helper equivalent to the cmp() function in Python 2."""
      return (x > y) - (x < y)

    def __lt__(self, other):
      return self._CmpHelper(
          (self.id, self.name, self.file_name),
          (other.id, other.name, other.file_name)) < 0

    def __gt__(self, other):
      return self._CmpHelper(
          (self.id, self.name, self.file_name),
          (other.id, other.name, other.file_name)) > 0

    def __le__(self, other):
      return not self.__gt__(other)

    def __ge__(self, other):
      return not self.__lt__(other)

  x86 = _ARCH('x86', 'x86', 'x86')
  x86_64 = _ARCH('x86_64', 'x86_64', 'x86_64')
  ppc = _ARCH('PPC', 'PPC', 'ppc')
  arm = _ARCH('arm', 'arm', 'arm')
  _ALL = [x86, x86_64, ppc, arm]

  # Possible values for `uname -m` and what arch they map to.
  # Examples of possible values: https://en.wikipedia.org/wiki/Uname
  _MACHINE_TO_ARCHITECTURE = {
      'amd64': x86_64, 'x86_64': x86_64, 'i686-64': x86_64,
      'i386': x86, 'i686': x86, 'x86': x86,
      'ia64': x86,  # Itanium is different x64 arch, treat it as the common x86.
      'powerpc': ppc, 'power macintosh': ppc, 'ppc64': ppc,
      'armv6': arm, 'armv6l': arm, 'arm64': arm, 'armv7': arm, 'armv7l': arm}

  @staticmethod
  def AllValues():
    """Gets all possible enum values.

    Returns:
      list, All the enum values.
    """
    return list(Architecture._ALL)

  @staticmethod
  def FromId(architecture_id, error_on_unknown=True):
    """Gets the enum corresponding to the given architecture id.

    Args:
      architecture_id: str, The architecture id to parse
      error_on_unknown: bool, True to raise an exception if the id is unknown,
        False to just return None.

    Raises:
      InvalidEnumValue: If the given value cannot be parsed.

    Returns:
      ArchitectureTuple, One of the Architecture constants or None if the input
      is None.
    """
    if not architecture_id:
      return None
    for arch in Architecture._ALL:
      if arch.id == architecture_id:
        return arch
    if error_on_unknown:
      raise InvalidEnumValue(architecture_id, 'Architecture',
                             [value.id for value in Architecture._ALL])
    return None

  @staticmethod
  def Current():
    """Determines the current system architecture.

    Returns:
      ArchitectureTuple, One of the Architecture constants or None if it cannot
      be determined.
    """
    return Architecture._MACHINE_TO_ARCHITECTURE.get(platform.machine().lower())


class Platform(object):
  """Holds an operating system and architecture."""

  def __init__(self, operating_system, architecture):
    """Constructs a new platform.

    Args:
      operating_system: OperatingSystem, The OS
      architecture: Architecture, The machine architecture.
    """
    self.operating_system = operating_system
    self.architecture = architecture

  def __str__(self):
    return '{}-{}'.format(self.operating_system, self.architecture)

  @staticmethod
  def Current(os_override=None, arch_override=None):
    """Determines the current platform you are running on.

    Args:
      os_override: OperatingSystem, A value to use instead of the current.
      arch_override: Architecture, A value to use instead of the current.

    Returns:
      Platform, The platform tuple of operating system and architecture.  Either
      can be None if it could not be determined.
    """
    return Platform(
        os_override if os_override else OperatingSystem.Current(),
        arch_override if arch_override else Architecture.Current())

  def UserAgentFragment(self):
    """Generates the fragment of the User-Agent that represents the OS.

    Examples:
      (Linux 3.2.5-gg1236)
      (Windows NT 6.1.7601)
      (Macintosh; PPC Mac OS X 12.4.0)
      (Macintosh; Intel Mac OS X 12.4.0)

    Returns:
      str, The fragment of the User-Agent string.
    """
    # Below, there are examples of the value of platform.uname() per platform.
    # platform.release() is uname[2], platform.version() is uname[3].
    if self.operating_system == OperatingSystem.LINUX:
      # ('Linux', '<hostname goes here>', '3.2.5-gg1236',
      # '#1 SMP Tue May 21 02:35:06 PDT 2013', 'x86_64', 'x86_64')
      return '({name} {version})'.format(
          name=self.operating_system.name, version=platform.release())
    elif self.operating_system == OperatingSystem.WINDOWS:
      # ('Windows', '<hostname goes here>', '7', '6.1.7601', 'AMD64',
      # 'Intel64 Family 6 Model 45 Stepping 7, GenuineIntel')
      return '({name} NT {version})'.format(
          name=self.operating_system.name, version=platform.version())
    elif self.operating_system == OperatingSystem.MACOSX:
      # ('Darwin', '<hostname goes here>', '12.4.0',
      # 'Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013;
      # root:xnu-2050.24.15~1/RELEASE_X86_64', 'x86_64', 'i386')
      format_string = '(Macintosh; {name} Mac OS X {version})'
      arch_string = (self.architecture.name
                     if self.architecture == Architecture.ppc else 'Intel')
      return format_string.format(
          name=arch_string, version=platform.release())
    else:
      return '()'

  def AsyncPopenArgs(self):
    """Returns the args for spawning an async process using Popen on this OS.

    Make sure the main process does not wait for the new process. On windows
    this means setting the 0x8 creation flag to detach the process.

    Killing a group leader kills the whole group. Setting creation flag 0x200 on
    Windows or running setsid on *nix makes sure the new process is in a new
    session with the new process the group leader. This means it can't be killed
    if the parent is killed.

    Finally, all file descriptors (FD) need to be closed so that waiting for the
    output of the main process does not inadvertently wait for the output of the
    new process, which means waiting for the termination of the new process.
    If the new process wants to write to a file, it can open new FDs.

    Returns:
      {str:}, The args for spawning an async process using Popen on this OS.
    """
    args = {}
    if self.operating_system == OperatingSystem.WINDOWS:
      args['close_fds'] = True  # This is enough to close _all_ FDs on windows.
      detached_process = 0x00000008
      create_new_process_group = 0x00000200
      # 0x008 | 0x200 == 0x208
      args['creationflags'] = detached_process | create_new_process_group
    else:
      # Killing a group leader kills the whole group.
      # Create a new session with the new process the group leader.
      args['preexec_fn'] = os.setsid
      args['close_fds'] = True  # This closes all FDs _except_ 0, 1, 2 on *nix.
      args['stdin'] = subprocess.PIPE
      args['stdout'] = subprocess.PIPE
      args['stderr'] = subprocess.PIPE
    return args


class PythonVersion(object):
  """Class to validate the Python version we are using.

  The Cloud SDK officially supports Python 2.7.

  However, many commands do work with Python 2.6, so we don't error out when
  users are using this (we consider it sometimes "compatible" but not
  "supported").
  """

  # See class docstring for descriptions of what these mean
  MIN_REQUIRED_PY2_VERSION = (2, 6)
  MIN_SUPPORTED_PY2_VERSION = (2, 7)
  MIN_SUPPORTED_PY3_VERSION = (3, 4)
  ENV_VAR_MESSAGE = """\

If you have a compatible Python interpreter installed, you can use it by setting
the CLOUDSDK_PYTHON environment variable to point to it.

"""

  def __init__(self, version=None):
    if version:
      self.version = version
    elif hasattr(sys, 'version_info'):
      self.version = sys.version_info[:2]
    else:
      self.version = None

  def SupportedVersionMessage(self, allow_py3):
    if allow_py3:
      return 'Please use Python version {0}.{1}.x or {2}.{3} and up.'.format(
          PythonVersion.MIN_SUPPORTED_PY2_VERSION[0],
          PythonVersion.MIN_SUPPORTED_PY2_VERSION[1],
          PythonVersion.MIN_SUPPORTED_PY3_VERSION[0],
          PythonVersion.MIN_SUPPORTED_PY3_VERSION[1])
    else:
      return 'Please use Python version {0}.{1}.x.'.format(
          PythonVersion.MIN_SUPPORTED_PY2_VERSION[0],
          PythonVersion.MIN_SUPPORTED_PY2_VERSION[1])

  def IsCompatible(self, allow_py3=False, raise_exception=False):
    """Ensure that the Python version we are using is compatible.

    This will print an error message if not compatible.

    Compatible versions are 2.6 and 2.7 and > 3.4 if allow_py3 is True.
    We don't guarantee support for 2.6 so we want to warn about it.

    Args:
      allow_py3: bool, True if we should allow a Python 3 interpreter to run
        gcloud. If False, this returns an error for Python 3.
      raise_exception: bool, True to raise an exception rather than printing
        the error and exiting.

    Raises:
      Error: If not compatible and raise_exception is True.

    Returns:
      bool, True if the version is valid, False otherwise.
    """
    error = None
    if not self.version:
      # We don't know the version, not a good sign.
      error = ('ERROR: Your current version of Python is not compatible with '
               'the Google Cloud SDK. {0}\n'
               .format(self.SupportedVersionMessage(allow_py3)))
    else:
      if self.version[0] < 3:
        # Python 2 Mode
        if self.version < PythonVersion.MIN_REQUIRED_PY2_VERSION:
          error = ('ERROR: Python {0}.{1} is not compatible with the Google '
                   'Cloud SDK. {2}\n'
                   .format(self.version[0], self.version[1],
                           self.SupportedVersionMessage(allow_py3)))
      else:
        # Python 3 Mode
        if not allow_py3:
          error = ('ERROR: Python 3 and later is not compatible with the '
                   'Google Cloud SDK. {0}\n'
                   .format(self.SupportedVersionMessage(allow_py3)))
        elif self.version < PythonVersion.MIN_SUPPORTED_PY3_VERSION:
          error = ('ERROR: Python {0}.{1} is not compatible with the Google '
                   'Cloud SDK. {2}\n'
                   .format(self.version[0], self.version[1],
                           self.SupportedVersionMessage(allow_py3)))

    if error:
      if raise_exception:
        raise Error(error)
      sys.stderr.write(error)
      sys.stderr.write(PythonVersion.ENV_VAR_MESSAGE)
      return False

    # Warn that 2.6 might not work.
    if (self.version >= self.MIN_REQUIRED_PY2_VERSION and
        self.version < self.MIN_SUPPORTED_PY2_VERSION):
      sys.stderr.write("""\
WARNING:  Python 2.6.x is no longer officially supported by the Google Cloud SDK
and may not function correctly.  {0}
{1}""".format(self.SupportedVersionMessage(allow_py3),
              PythonVersion.ENV_VAR_MESSAGE))

    return True


================================================
FILE: fire/console/text.py
================================================
# -*- coding: utf-8 -*- #
# Copyright 2018 Google LLC. 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.
"""Semantic text objects that are used for styled outputting."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import enum


class TextAttributes(object):
  """Attributes to use to style text with."""

  def __init__(self, format_str=None, color=None, attrs=None):
    """Defines a set of attributes for a piece of text.

    Args:
      format_str: (str), string that will be used to format the text
        with. For example '[{}]', to enclose text in brackets.
      color: (Colors), the color the text should be formatted with.
      attrs: (Attrs), the attributes to apply to text.
    """
    self._format_str = format_str
    self._color = color
    self._attrs = attrs or []

  @property
  def format_str(self):
    return self._format_str

  @property
  def color(self):
    return self._color

  @property
  def attrs(self):
    return self._attrs


class TypedText(object):
  """Text with a semantic type that will be used for styling."""

  def __init__(self, texts, text_type=None):
    """String of text and a corresponding type to use to style that text.

    Args:
     texts: (list[str]), list of strs or TypedText objects
       that should be styled using text_type.
     text_type: (TextTypes), the semantic type of the text that
       will be used to style text.
    """
    self.texts = texts
    self.text_type = text_type

  def __len__(self):
    length = 0
    for text in self.texts:
      length += len(text)
    return length

  def __add__(self, other):
    texts = [self, other]
    return TypedText(texts)

  def __radd__(self, other):
    texts = [other, self]
    return TypedText(texts)


class _TextTypes(enum.Enum):
  """Text types base class that defines base functionality."""

  def __call__(self, *args):
    """Returns a TypedText object using this style."""
    return TypedText(list(args), self)


# TODO: Add more types.
class TextTypes(_TextTypes):
  """Defines text types that can be used for styling text."""
  RESOURCE_NAME = 1
  URL = 2
  USER_INPUT = 3
  COMMAND = 4
  INFO = 5
  URI = 6
  OUTPUT = 7
  PT_SUCCESS = 8
  PT_FAILURE = 9



================================================
FILE: fire/core.py
================================================
# 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.

"""Python Fire is a library for creating CLIs from absolutely any Python object.

You can call Fire on any Python object:
functions, classes, modules, objects, dictionaries, lists, tuples, etc.
They all work!

Python Fire turns any Python object into a command line interface.
Simply call the Fire function as your main method to create a CLI.

When using Fire to build a CLI, your main method includes a call to Fire. Eg:

def main(argv):
  fire.Fire(Component)

A Fire CLI command is run by consuming the arguments in the command in order to
access a member of current component, call the current component (if it's a
function), or instantiate the current component (if it's a class). The target
component begins as Component, and at each operation the component becomes the
result of the preceding operation.

For example "command fn arg1 arg2" might access the "fn" property of the initial
target component, and then call that function with arguments 'arg1' and 'arg2'.
Additional examples are available in the examples directory.

Fire Flags, common to all Fire CLIs, must go after a separating "--". For
example, to get help for a command you might run: `command -- --help`.

The available flags for all Fire CLIs are:
  -v --verbose: Include private members in help and usage information.
  -h --help: Provide help and usage information for the command.
  -i --interactive: Drop into a Python REPL after running the command.
  --completion: Write the Bash completion script for the tool to stdout.
  --completion fish: Write the Fish completion script for the tool to stdout.
  --separator SEPARATOR: Use SEPARATOR in place of the default separator, '-'.
  --trace: Get the Fire Trace for the command.
"""

import asyncio
import inspect
import json
import os
import re
import shlex
import sys
import types

from fire import completion
from fire import decorators
from fire import formatting
from fire import helptext
from fire import inspectutils
from fire import interact
from fire import parser
from fire import trace
from fire import value_types
from fire.console import console_io


def Fire(component=None, command=None, name=None, serialize=None):
  """This function, Fire, is the main entrypoint for Python Fire.

  Executes a command either from the `command` argument or from sys.argv by
  recursively traversing the target object `component`'s members consuming
  arguments, evaluating functions, and instantiating classes as it goes.

  When building a CLI with Fire, your main method should call this function.

  Args:
    component: The initial target component.
    command: Optional. If supplied, this is the command executed. If not
        supplied, then the command is taken from sys.argv instead. This can be
        a string or a list of strings; a list of strings is preferred.
    name: Optional. The name of the command as entered at the command line.
        Used in interactive mode and for generating the completion script.
    serialize: Optional. If supplied, all objects are serialized to text via
        the provided callable.
  Returns:
    The result of executing the Fire command. Execution begins with the initial
    target component. The component is updated by using the command arguments
    to either access a member of the current component, call the current
    component (if it's a function), or instantiate the current component (if
    it's a class). When all arguments are consumed and there's no function left
    to call or class left to instantiate, the resulting current component is
    the final result.
  Raises:
    ValueError: If the command argument is supplied, but not a string or a
        sequence of arguments.
    FireExit: When Fire encounters a FireError, Fire will raise a FireExit with
        code 2. When used with the help or trace flags, Fire will raise a
        FireExit with code 0 if successful.
  """
  name = name or os.path.basename(sys.argv[0])

  # Get args as a list.
  if isinstance(command, str):
    args = shlex.split(command)
  elif isinstance(command, (list, tuple)):
    args = command
  elif command is None:
    # Use the command line args by default if no command is specified.
    args = sys.argv[1:]
  else:
    raise ValueError('The command argument must be a string or a sequence of '
                     'arguments.')

  args, flag_args = parser.SeparateFlagArgs(args)

  argparser = parser.CreateParser()
  parsed_flag_args, unused_args = argparser.parse_known_args(flag_args)

  context = {}
  if parsed_flag_args.interactive or component is None:
    # Determine the calling context.
    caller = inspect.stack()[1]
    caller_frame = caller[0]
    caller_globals = caller_frame.f_globals
    caller_locals = caller_frame.f_locals
    context.update(caller_globals)
    context.update(caller_locals)

  component_trace = _Fire(component, args, parsed_flag_args, context, name)

  if component_trace.HasError():
    _DisplayError(component_trace)
    raise FireExit(2, component_trace)
  if component_trace.show_trace and component_trace.show_help:
    output = [f'Fire trace:\n{component_trace}\n']
    result = component_trace.GetResult()
    help_text = helptext.HelpText(
        result, trace=component_trace, verbose=component_trace.verbose)
    output.append(help_text)
    Display(output, out=sys.stderr)
    raise FireExit(0, component_trace)
  if component_trace.show_trace:
    output = [f'Fire trace:\n{component_trace}']
    Display(output, out=sys.stderr)
    raise FireExit(0, component_trace)
  if component_trace.show_help:
    result = component_trace.GetResult()
    help_text = helptext.HelpText(
        result, trace=component_trace, verbose=component_trace.verbose)
    output = [help_text]
    Display(output, out=sys.stderr)
    raise FireExit(0, component_trace)

  # The command succeeded normally; print the result.
  _PrintResult(
      component_trace, verbose=component_trace.verbose, serialize=serialize)
  result = component_trace.GetResult()
  return result


def Display(lines, out):
  text = '\n'.join(lines) + '\n'
  console_io.More(text, out=out)


def CompletionScript(name, component, shell):
  """Returns the text of the completion script for a Fire CLI."""
  return completion.Script(name, component, shell=shell)


class FireError(Exception):
  """Exception used by Fire when a Fire command cannot be executed.

  These exceptions are not raised by the Fire function, but rather are caught
  and added to the FireTrace.
  """


class FireExit(SystemExit):  # pylint: disable=g-bad-exception-name
  """An exception raised by Fire to the client in the case of a FireError.

  The trace of the Fire program is available on the `trace` property.

  This exception inherits from SystemExit, so clients may explicitly catch it
  with `except SystemExit` or `except FireExit`. If not caught, this exception
  will cause the client program to exit without a stacktrace.
  """

  def __init__(self, code, component_trace):
    """Constructs a FireExit exception.

    Args:
      code: (int) Exit code for the Fire CLI.
      component_trace: (FireTrace) The trace for the Fire command.
    """
    super().__init__(code)
    self.trace = component_trace


def _IsHelpShortcut(component_trace, remaining_args):
  """Determines if the user is trying to access help without '--' separator.

  For example, mycmd.py --help instead of mycmd.py -- --help.

  Args:
    component_trace: (FireTrace) The trace for the Fire command.
    remaining_args: List of remaining args that haven't been consumed yet.
  Returns:
    True if help is requested, False otherwise.
  """
  show_help = False
  if remaining_args:
    target = remaining_args[0]
    if target in ('-h', '--help'):
      # Check if --help would be consumed as a keyword argument, or is a member.
      component = component_trace.GetResult()
      if inspect.isclass(component) or inspect.isroutine(component):
        fn_spec = inspectutils.GetFullArgSpec(component)
        _, remaining_kwargs, _ = _ParseKeywordArgs(remaining_args, fn_spec)
        show_help = target in remaining_kwargs
      else:
        members = dict(inspect.getmembers(component))
        show_help = target not in members

  if show_help:
    component_trace.show_help = True
    command = f'{component_trace.GetCommand()} -- --help'
    print(f'INFO: Showing help with the command {shlex.quote(command)}.\n',
          file=sys.stderr)
  return show_help


def _PrintResult(component_trace, verbose=False, serialize=None):
  """Prints the result of the Fire call to stdout in a human readable way."""
  # TODO(dbieber): Design human readable deserializable serialization method
  # and move serialization to its own module.
  result = component_trace.GetResult()

  # Allow users to modify the return value of the component and provide
  # custom formatting.
  if serialize:
    if not callable(serialize):
      raise FireError(
          'The argument `serialize` must be empty or callable:', serialize)
    result = serialize(result)

  if value_types.HasCustomStr(result):
    # If the object has a custom __str__ method, rather than one inherited from
    # object, then we use that to serialize the object.
    print(str(result))
    return

  if isinstance(result, (list, set, frozenset, types.GeneratorType)):
    for i in result:
      print(_OneLineResult(i))
  elif inspect.isgeneratorfunction(result):
    raise NotImplementedError
  elif isinstance(result, dict) and value_types.IsSimpleGroup(result):
    print(_DictAsString(result, verbose))
  elif isinstance(result, tuple):
    print(_OneLineResult(result))
  elif isinstance(result, value_types.VALUE_TYPES):
    if result is not None:
      print(result)
  else:
    help_text = helptext.HelpText(
        result, trace=component_trace, verbose=verbose)
    output = [help_text]
    Display(output, out=sys.stdout)


def _DisplayError(component_trace):
  """Prints the Fire trace and the error to stdout."""
  result = component_trace.GetResult()

  output = []
  show_help = False
  for help_flag in ('-h', '--help'):
    if help_flag in component_trace.elements[-1].args:
      show_help = True

  if show_help:
    command = f'{component_trace.GetCommand()} -- --help'
    print(f'INFO: Showing help with the command {shlex.quote(command)}.\n',
          file=sys.stderr)
    help_text = helptext.HelpText(result, trace=component_trace,
                                  verbose=component_trace.verbose)
    output.append(help_text)
    Display(output, out=sys.stderr)
  else:
    print(formatting.Error('ERROR: ')
          + component_trace.elements[-1].ErrorAsStr(),
          file=sys.stderr)
    error_text = helptext.UsageText(result, trace=component_trace,
                                    verbose=component_trace.verbose)
    print(error_text, file=sys.stderr)


def _DictAsString(result, verbose=False):
  """Returns a dict as a string.

  Args:
    result: The dict to convert to a string
    verbose: Whether to include 'hidden' members, those keys starting with _.
  Returns:
    A string representing the dict
  """

  # We need to do 2 iterations over the items in the result dict
  # 1) Getting visible items and the longest key for output formatting
  # 2) Actually construct the output lines
  class_attrs = inspectutils.GetClassAttrsDict(result)
  result_visible = {
      key: value for key, value in result.items()
      if completion.MemberVisible(result, key, value,
                                  class_attrs=class_attrs, verbose=verbose)
  }

  if not result_visible:
    return '{}'

  longest_key = max(len(str(key)) for key in result_visible.keys())
  format_string = f'{{key:{longest_key + 1}s}} {{value}}'

  lines = []
  for key, value in result.items():
    if completion.MemberVisible(result, key, value, class_attrs=class_attrs,
                                verbose=verbose):
      line = format_string.format(key=f'{key}:', value=_OneLineResult(value))
      lines.append(line)
  return '\n'.join(lines)


def _OneLineResult(result):
  """Returns result serialized to a single line string."""
  # TODO(dbieber): Ensure line is fewer than eg 120 characters.
  if isinstance(result, str):
    return str(result).replace('\n', ' ')

  # TODO(dbieber): Show a small amount of usage information about the function
  # or module if it fits cleanly on the line.
  if inspect.isfunction(result):
    return f'<function {result.__name__}>'

  if inspect.ismodule(result):
    return f'<module {result.__name__}>'

  try:
    # Don't force conversion to ascii.
    return json.dumps(result, ensure_ascii=False)
  except (TypeError, ValueError):
    return str(result).replace('\n', ' ')


def _Fire(component, args, parsed_flag_args, context, name=None):
  """Execute a Fire command on a target component using the args supplied.

  Arguments that come after a final isolated '--' are treated as Flags, eg for
  interactive mode or completion script generation.

  Other arguments are consumed by the execution of the Fire command, eg in the
  traversal of the members of the component, or in calling a function or
  instantiating a class found during the traversal.

  The steps performed by this method are:

  1. Parse any Flag args (the args after the final --)

  2. Start with component as the current component.
  2a. If the current component is a class, instantiate it using args from args.
  2b. If the component is a routine, call it using args from args.
  2c. If the component is a sequence, index into it using an arg from
      args.
  2d. If possible, access a member from the component using an arg from args.
  2e. If the component is a callable object, call it using args from args.
  2f. Repeat 2a-2e until no args remain.
  Note: Only the first applicable rule from 2a-2e is applied in each iteration.
  After each iteration of step 2a-2e, the current component is updated to be the
  result of the applied rule.

  3a. Embed into ipython REPL if interactive mode is selected.
  3b. Generate a completion script if that flag is provided.

  In step 2, arguments will only ever be consumed up to a separator; a single
  step will never consume arguments from both sides of a separator.
  The separator defaults to a hyphen (-), and can be overwritten with the
  --separator Fire argument.

  Args:
    component: The target component for Fire.
    args: A list of args to consume in Firing on the component, usually from
        the command line.
    parsed_flag_args: The values of the flag args (e.g. --verbose, --separator)
        that are part of every Fire CLI.
    context: A dict with the local and global variables available at the call
        to Fire.
    name: Optional. The name of the command. Used in interactive mode and in
        the tab completion script.
  Returns:
    FireTrace of components starting with component, tracing Fire's execution
        path as it consumes args.
  Raises:
    ValueError: If there are arguments that cannot be consumed.
    ValueError: If --completion is specified but no name available.
  """
  verbose = parsed_flag_args.verbose
  interactive = parsed_flag_args.interactive
  separator = parsed_flag_args.separator
  show_completion = parsed_flag_args.completion
  show_help = parsed_flag_args.help
  show_trace = parsed_flag_args.trace

  # component can be a module, class, routine, object, etc.
  if component is None:
    component = context

  initial_component = component
  component_trace = trace.FireTrace(
      initial_component=initial_component, name=name, separator=separator,
      verbose=verbose, show_help=show_help, show_trace=show_trace)

  instance = None
  remaining_args = args
  while True:
    last_component = component
    initial_args = remaining_args

    if not remaining_args and (show_help or interactive or show_trace
                               or show_completion is not None):
      # Don't initialize the final class or call the final function unless
      # there's a separator after it, and instead process the current component.
      break

    if _IsHelpShortcut(component_trace, remaining_args):
      remaining_args = []
      break

    saved_args = []
    used_separator = False
    if separator in remaining_args:
      # For the current component, only use arguments up to the separator.
      separator_index = remaining_args.index(separator)
      saved_args = remaining_args[separator_index + 1:]
      remaining_args = remaining_args[:separator_index]
      used_separator = True
    assert separator not in remaining_args

    handled = False
    candidate_errors = []

    is_callable = inspect.isclass(component) or inspect.isroutine(component)
    is_callable_object = callable(component) and not is_callable
    is_sequence = isinstance(component, (list, tuple))
    is_map = isinstance(component, dict) or inspectutils.IsNamedTuple(component)

    if not handled and is_callable:
      # The component is a class or a routine; we'll try to initialize it or
      # call it.
      is_class = inspect.isclass(component)

      try:
        component, remaining_args = _CallAndUpdateTrace(
            component,
            remaining_args,
            component_trace,
            treatment='class' if is_class else 'routine',
            target=component.__name__)
        handled = True
      except FireError as error:
        candidate_errors.append((error, initial_args))

      if handled and last_component is initial_component:
        # If the initial component is a class, keep an instance for use with -i.
        instance = component

    if not handled and is_sequence and remaining_args:
      # The component is a tuple or list; we'll try to access a member.
      arg = remaining_args[0]
      try:
        index = int(arg)
        component = component[index]
        handled = True
      except (ValueError, IndexError):
        error = FireError(
            'Unable to index into component with argument:', arg)
        candidate_errors.append((error, initial_args))

      if handled:
        remaining_args = remaining_args[1:]
        filename = None
        lineno = None
        component_trace.AddAccessedProperty(
            component, index, [arg], filename, lineno)

    if not handled and is_map and remaining_args:
      # The component is a dict or other key-value map; try to access a member.
      target = remaining_args[0]

      # Treat namedtuples as dicts when handling them as a map.
      if inspectutils.IsNamedTuple(component):
        component_dict = component._asdict()
      else:
        component_dict = component

      if target in component_dict:
        component = component_dict[target]
        handled = True
      elif target.replace('-', '_') in component_dict:
        component = component_dict[target.replace('-', '_')]
        handled = True
      else:
        # The target isn't present in the dict as a string key, but maybe it is
        # a key as another type.
        # TODO(dbieber): Consider alternatives for accessing non-string keys.
        for key, value in (
            component_dict.items()):
          if target == str(key):
            component = value
            handled = True
            break

      if handled:
        remaining_args = remaining_args[1:]
        filename = None
        lineno = None
        component_trace.AddAccessedProperty(
            component, target, [target], filename, lineno)
      else:
        error = FireError('Cannot find key:', target)
        candidate_errors.append((error, initial_args))

    if not handled and remaining_args:
      # Object handler. We'll try to access a member of the component.
      try:
        target = remaining_args[0]

        component, consumed_args, remaining_args = _GetMember(
            component, remaining_args)
        handled = True

        filename, lineno = inspectutils.GetFileAndLine(component)

        component_trace.AddAccessedProperty(
            component, target, consumed_args, filename, lineno)

      except FireError as error:
        # Couldn't access member.
        candidate_errors.append((error, initial_args))

    if not handled and is_callable_object:
      # The component is a callable object; we'll try to call it.
      try:
        component, remaining_args = _CallAndUpdateTrace(
            component,
            remaining_args,
            component_trace,
            treatment='callable')
        handled = True
      except FireError as error:
        candidate_errors.append((error, initial_args))

    if not handled and candidate_errors:
      error, initial_args = candidate_errors[0]
      component_trace.AddError(error, initial_args)
      return component_trace

    if used_separator:
      # Add back in the arguments from after the separator.
      if remaining_args:
        remaining_args = remaining_args + [separator] + saved_args
      elif (inspect.isclass(last_component)
            or inspect.isroutine(last_component)):
        remaining_args = saved_args
        component_trace.AddSeparator()
      elif component is not last_component:
        remaining_args = [separator] + saved_args
      else:
        # It was an unnecessary separator.
        remaining_args = saved_args

    if component is last_component and remaining_args == initial_args:
      # We're making no progress.
      break

  if remaining_args:
    component_trace.AddError(
        FireError('Could not consume arguments:', remaining_args),
        initial_args)
    return component_trace

  if show_completion is not None:
    if name is None:
      raise ValueError('Cannot make completion script without command name')
    script = CompletionScript(name, initial_component, shell=show_completion)
    component_trace.AddCompletionScript(script)

  if interactive:
    variables = context.copy()

    if name is not None:
      variables[name] = initial_component
    variables['component'] = initial_component
    variables['result'] = component
    variables['trace'] = component_trace

    if instance is not None:
      variables['self'] = instance

    interact.Embed(variables, verbose)

    component_trace.AddInteractiveMode()

  return component_trace


def _GetMember(component, args):
  """Returns a subcomponent of component by consuming an arg from args.

  Given a starting component and args, this function gets a member from that
  component, consuming one arg in the process.

  Args:
    component: The component from which to get a member.
    args: Args from which to consume in the search for the next component.
  Returns:
    component: The component that was found by consuming an arg.
    consumed_args: The args that were consumed by getting this member.
    remaining_args: The remaining args that haven't been consumed yet.
  Raises:
    FireError: If we cannot consume an argument to get a member.
  """
  members = dir(component)
  arg = args[0]
  arg_names = [
      arg,
      arg.replace('-', '_'),  # treat '-' as '_'.
  ]

  for arg_name in arg_names:
    if arg_name in members:
      return getattr(component, arg_name), [arg], args[1:]

  raise FireError('Could not consume arg:', arg)


def _CallAndUpdateTrace(component, args, component_trace, treatment='class',
                        target=None):
  """Call the component by consuming args from args, and update the FireTrace.

  The component could be a class, a routine, or a callable object. This function
  calls the component and adds the appropriate action to component_trace.

  Args:
    component: The component to call
    args: Args for calling the component
    component_trace: FireTrace object that contains action trace
    treatment: Type of treatment used. Indicating whether we treat the component
        as a class, a routine, or a callable.
    target: Target in FireTrace element, default is None. If the value is None,
        the component itself will be used as target.
  Returns:
    component: The object that is the result of the callable call.
    remaining_args: The remaining args that haven't been consumed yet.
  """
  if not target:
    target = component
  filename, lineno = inspectutils.GetFileAndLine(component)
  metadata = decorators.GetMetadata(component)
  fn = component.__call__ if treatment == 'callable' else component
  parse = _MakeParseFn(fn, metadata)
  (varargs, kwargs), consumed_args, remaining_args, capacity = parse(args)

  # Call the function.
  if inspectutils.IsCoroutineFunction(fn):
    try:
      loop = asyncio.get_running_loop()
    except RuntimeError:
      # No event loop running, create a new one
      component = asyncio.run(fn(*varargs, **kwargs))
    else:
      # Event loop is already running
      component = loop.run_until_complete(fn(*varargs, **kwargs))
  else:
    component = fn(*varargs, **kwargs)

  if treatment == 'class':
    action = trace.INSTANTIATED_CLASS
  elif treatment == 'routine':
    action = trace.CALLED_ROUTINE
  else:
    action = trace.CALLED_CALLABLE
  component_trace.AddCalledComponent(
      component, target, consumed_args, filename, lineno, capacity,
      action=action)

  return component, remaining_args


def _MakeParseFn(fn, metadata):
  """Creates a parse function for fn.

  Args:
    fn: The function or class to create the parse function for.
    metadata: Additional metadata about the component the parse function is for.
  Returns:
    A parse function for fn. The parse function accepts a list of arguments
    and returns (varargs, kwargs), remaining_args. The original function fn
    can then be called with fn(*varargs, **kwargs). The remaining_args are
    the leftover args from the arguments to the parse function.
  """
  fn_spec = inspectutils.GetFullArgSpec(fn)

  # Note: num
Download .txt
gitextract_5446x807/

├── .github/
│   ├── dependabot.yml
│   ├── scripts/
│   │   └── build.sh
│   └── workflows/
│       └── build.yml
├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── docs/
│   ├── api.md
│   ├── benefits.md
│   ├── guide.md
│   ├── index.md
│   ├── installation.md
│   ├── troubleshooting.md
│   └── using-cli.md
├── examples/
│   ├── __init__.py
│   ├── cipher/
│   │   ├── __init__.py
│   │   ├── cipher.py
│   │   └── cipher_test.py
│   ├── diff/
│   │   ├── __init__.py
│   │   ├── diff.py
│   │   ├── diff_test.py
│   │   └── difffull.py
│   ├── identity/
│   │   ├── __init__.py
│   │   └── identity.py
│   └── widget/
│       ├── __init__.py
│       ├── collector.py
│       ├── collector_test.py
│       ├── widget.py
│       └── widget_test.py
├── fire/
│   ├── __init__.py
│   ├── __main__.py
│   ├── completion.py
│   ├── completion_test.py
│   ├── console/
│   │   ├── README.md
│   │   ├── __init__.py
│   │   ├── console_attr.py
│   │   ├── console_attr_os.py
│   │   ├── console_io.py
│   │   ├── console_pager.py
│   │   ├── encoding.py
│   │   ├── files.py
│   │   ├── platforms.py
│   │   └── text.py
│   ├── core.py
│   ├── core_test.py
│   ├── custom_descriptions.py
│   ├── custom_descriptions_test.py
│   ├── decorators.py
│   ├── decorators_test.py
│   ├── docstrings.py
│   ├── docstrings_fuzz_test.py
│   ├── docstrings_test.py
│   ├── fire_import_test.py
│   ├── fire_test.py
│   ├── formatting.py
│   ├── formatting_test.py
│   ├── formatting_windows.py
│   ├── helptext.py
│   ├── helptext_test.py
│   ├── inspectutils.py
│   ├── inspectutils_test.py
│   ├── interact.py
│   ├── interact_test.py
│   ├── main_test.py
│   ├── parser.py
│   ├── parser_fuzz_test.py
│   ├── parser_test.py
│   ├── test_components.py
│   ├── test_components_bin.py
│   ├── test_components_py3.py
│   ├── test_components_test.py
│   ├── testutils.py
│   ├── testutils_test.py
│   ├── trace.py
│   ├── trace_test.py
│   └── value_types.py
├── mkdocs.yml
├── pylintrc
└── pyproject.toml
Download .txt
SYMBOL INDEX (782 symbols across 54 files)

FILE: examples/cipher/cipher.py
  function caesar_encode (line 32) | def caesar_encode(n=0, text=''):
  function caesar_decode (line 39) | def caesar_decode(n=0, text=''):
  function rot13 (line 43) | def rot13(text):
  function _caesar_shift_char (line 47) | def _caesar_shift_char(n=0, char=' '):
  function main (line 55) | def main():

FILE: examples/cipher/cipher_test.py
  class CipherTest (line 22) | class CipherTest(testutils.BaseTestCase):
    method testCipher (line 24) | def testCipher(self):

FILE: examples/diff/diff.py
  class DiffLibWrapper (line 64) | class DiffLibWrapper(object):
    method __init__ (line 71) | def __init__(self, fromfile, tofile):
    method unified_diff (line 82) | def unified_diff(self, lines=3):
    method ndiff (line 87) | def ndiff(self):
    method make_file (line 90) | def make_file(self, context=False, lines=3):
    method context_diff (line 95) | def context_diff(self, lines=3):
  function main (line 101) | def main():

FILE: examples/diff/diff_test.py
  class DiffTest (line 25) | class DiffTest(testutils.BaseTestCase):
    method setUp (line 31) | def setUp(self):
    method testSetUp (line 43) | def testSetUp(self):
    method testUnifiedDiff (line 47) | def testUnifiedDiff(self):
    method testContextDiff (line 62) | def testContextDiff(self):
    method testNDiff (line 75) | def testNDiff(self):
    method testMakeDiff (line 86) | def testMakeDiff(self):
    method testDiffFull (line 89) | def testDiffFull(self):

FILE: examples/diff/difffull.py
  function main (line 47) | def main():

FILE: examples/identity/identity.py
  function identity (line 20) | def identity(arg=None):
  function main (line 24) | def main(_=None):

FILE: examples/widget/collector.py
  class Collector (line 22) | class Collector(object):
    method __init__ (line 25) | def __init__(self):
    method collect_widgets (line 29) | def collect_widgets(self):
  function main (line 34) | def main():

FILE: examples/widget/collector_test.py
  class CollectorTest (line 23) | class CollectorTest(testutils.BaseTestCase):
    method testCollectorHasWidget (line 25) | def testCollectorHasWidget(self):
    method testCollectorWantsMoreWidgets (line 29) | def testCollectorWantsMoreWidgets(self):
    method testCollectorGetsWantedWidgets (line 33) | def testCollectorGetsWantedWidgets(self):

FILE: examples/widget/widget.py
  class Widget (line 20) | class Widget(object):
    method whack (line 22) | def whack(self, n=1):
    method bang (line 26) | def bang(self, noise='bang'):
  function main (line 31) | def main():

FILE: examples/widget/widget_test.py
  class WidgetTest (line 22) | class WidgetTest(testutils.BaseTestCase):
    method testWidgetWhack (line 24) | def testWidgetWhack(self):
    method testWidgetBang (line 29) | def testWidgetBang(self):

FILE: fire/__main__.py
  function import_from_file_path (line 42) | def import_from_file_path(path):
  function import_from_module_name (line 72) | def import_from_module_name(module_name):
  function import_module (line 78) | def import_module(module_or_filename):
  function main (line 112) | def main(args):

FILE: fire/completion.py
  function Script (line 28) | def Script(name, component, default_options=None, shell='bash'):
  function _BashScript (line 34) | def _BashScript(name, commands, default_options=None):
  function _FishScript (line 191) | def _FishScript(name, commands, default_options=None):
  function MemberVisible (line 284) | def MemberVisible(component, name, member, class_attrs=None, verbose=Fal...
  function VisibleMembers (line 341) | def VisibleMembers(component, class_attrs=None, verbose=False):
  function _CompletionsFromArgs (line 374) | def _CompletionsFromArgs(fn_args):
  function Completions (line 389) | def Completions(component, verbose=False):
  function _FormatForCommand (line 418) | def _FormatForCommand(token):
  function _Commands (line 440) | def _Commands(component, depth=3):
  function _IsOption (line 475) | def _IsOption(arg):
  function _GetMaps (line 479) | def _GetMaps(name, commands, default_options):

FILE: fire/completion_test.py
  class TabCompletionTest (line 22) | class TabCompletionTest(testutils.BaseTestCase):
    method testCompletionBashScript (line 24) | def testCompletionBashScript(self):
    method testCompletionFishScript (line 39) | def testCompletionFishScript(self):
    method testFnCompletions (line 52) | def testFnCompletions(self):
    method testListCompletions (line 61) | def testListCompletions(self):
    method testDictCompletions (line 68) | def testDictCompletions(self):
    method testDictCompletionsVerbose (line 83) | def testDictCompletionsVerbose(self):
    method testDeepDictCompletions (line 98) | def testDeepDictCompletions(self):
    method testDeepDictScript (line 104) | def testDeepDictScript(self):
    method testFnScript (line 112) | def testFnScript(self):
    method testClassScript (line 119) | def testClassScript(self):
    method testDeepDictFishScript (line 127) | def testDeepDictFishScript(self):
    method testFnFishScript (line 135) | def testFnFishScript(self):
    method testClassFishScript (line 142) | def testClassFishScript(self):
    method testNonStringDictCompletions (line 150) | def testNonStringDictCompletions(self):
    method testGeneratorCompletions (line 163) | def testGeneratorCompletions(self):
    method testClassCompletions (line 172) | def testClassCompletions(self):
    method testObjectCompletions (line 176) | def testObjectCompletions(self):
    method testMethodCompletions (line 181) | def testMethodCompletions(self):

FILE: fire/console/console_attr.py
  class BoxLineCharacters (line 105) | class BoxLineCharacters(object):
  class BoxLineCharactersUnicode (line 113) | class BoxLineCharactersUnicode(BoxLineCharacters):
  class BoxLineCharactersAscii (line 139) | class BoxLineCharactersAscii(BoxLineCharacters):
  class BoxLineCharactersScreenReader (line 165) | class BoxLineCharactersScreenReader(BoxLineCharactersAscii):
  class ProgressTrackerSymbols (line 177) | class ProgressTrackerSymbols(object):
  class ProgressTrackerSymbolsUnicode (line 181) | class ProgressTrackerSymbolsUnicode(ProgressTrackerSymbols):
    method spin_marks (line 185) | def spin_marks(self):
  class ProgressTrackerSymbolsAscii (line 195) | class ProgressTrackerSymbolsAscii(ProgressTrackerSymbols):
    method spin_marks (line 199) | def spin_marks(self):
  class ConsoleAttr (line 209) | class ConsoleAttr(object):
    method __init__ (line 248) | def __init__(self, encoding=None, suppress_output=False):
    method _GetConsoleEncoding (line 305) | def _GetConsoleEncoding(self):
    method Colorize (line 321) | def Colorize(self, string, color, justify=None):
    method ConvertOutputToUnicode (line 344) | def ConvertOutputToUnicode(self, buf):
    method GetBoxLineCharacters (line 360) | def GetBoxLineCharacters(self):
    method GetBullets (line 371) | def GetBullets(self):
    method GetProgressTrackerSymbols (line 383) | def GetProgressTrackerSymbols(self):
    method GetControlSequenceIndicator (line 391) | def GetControlSequenceIndicator(self):
    method GetControlSequenceLen (line 400) | def GetControlSequenceLen(self, buf):
    method GetEncoding (line 421) | def GetEncoding(self):
    method GetFontCode (line 425) | def GetFontCode(self, bold=False, italic=False):
    method GetRawKey (line 447) | def GetRawKey(self):
    method GetTermIdentifier (line 456) | def GetTermIdentifier(self):
    method GetTermSize (line 464) | def GetTermSize(self):
    method DisplayWidth (line 472) | def DisplayWidth(self, buf):
    method SplitIntoNormalAndControl (line 509) | def SplitIntoNormalAndControl(self, buf):
    method SplitLine (line 533) | def SplitLine(self, line, width):
    method SupportsAnsi (line 565) | def SupportsAnsi(self):
  class Colorizer (line 570) | class Colorizer(object):
    method __init__ (line 581) | def __init__(self, string, color, justify=None):
    method __eq__ (line 595) | def __eq__(self, other):
    method __ne__ (line 598) | def __ne__(self, other):
    method __gt__ (line 601) | def __gt__(self, other):
    method __lt__ (line 604) | def __lt__(self, other):
    method __ge__ (line 607) | def __ge__(self, other):
    method __le__ (line 610) | def __le__(self, other):
    method __len__ (line 613) | def __len__(self):
    method __str__ (line 616) | def __str__(self):
    method Render (line 619) | def Render(self, stream, justify=None):
  function GetConsoleAttr (line 631) | def GetConsoleAttr(encoding=None, reset=False):
  function ResetConsoleAttr (line 664) | def ResetConsoleAttr(encoding=None):
  function GetCharacterDisplayWidth (line 679) | def GetCharacterDisplayWidth(char):
  function SafeText (line 714) | def SafeText(data, encoding=None, escape=True):
  function EncodeToBytes (line 757) | def EncodeToBytes(data):
  function Decode (line 798) | def Decode(data, encoding=None):

FILE: fire/console/console_attr_os.py
  function GetTermSize (line 28) | def GetTermSize():
  function _GetTermSizePosix (line 52) | def _GetTermSizePosix():
  function _GetTermSizeWindows (line 92) | def _GetTermSizeWindows():
  function _GetTermSizeEnvironment (line 117) | def _GetTermSizeEnvironment():
  function _GetTermSizeTput (line 122) | def _GetTermSizeTput():
  function GetRawKeyFunction (line 141) | def GetRawKeyFunction():
  function _GetRawKeyFunctionPosix (line 158) | def _GetRawKeyFunctionPosix():
  function _GetRawKeyFunctionWindows (line 221) | def _GetRawKeyFunctionWindows():

FILE: fire/console/console_io.py
  function IsInteractive (line 29) | def IsInteractive(output=False, error=False, heuristic=False):
  function More (line 68) | def More(contents, out, prompt=None, check_pager=True):

FILE: fire/console/console_pager.py
  class Pager (line 28) | class Pager(object):
    method __init__ (line 91) | def __init__(self, contents, out=None, prompt=None):
    method _Write (line 124) | def _Write(self, s):
    method _GetSearchCommand (line 128) | def _GetSearchCommand(self, c):
    method _Help (line 159) | def _Help(self):
    method Run (line 169) | def Run(self):

FILE: fire/console/encoding.py
  function Encode (line 26) | def Encode(string, encoding=None):
  function Decode (line 40) | def Decode(data, encoding=None):
  function GetEncodedValue (line 124) | def GetEncodedValue(env, name, default=None):
  function SetEncodedValue (line 144) | def SetEncodedValue(env, name, value, encoding=None):
  function EncodeEnv (line 170) | def EncodeEnv(env, encoding=None):
  function _GetEncoding (line 187) | def _GetEncoding():

FILE: fire/console/files.py
  function _GetSystemPath (line 28) | def _GetSystemPath():
  function _FindExecutableOnPath (line 33) | def _FindExecutableOnPath(executable, path, pathext):
  function _PlatformExecutableExtensions (line 66) | def _PlatformExecutableExtensions(platform):
  function FindExecutableOnPath (line 73) | def FindExecutableOnPath(executable, path=None, pathext=None,

FILE: fire/console/platforms.py
  class Error (line 28) | class Error(Exception):
  class InvalidEnumValue (line 33) | class InvalidEnumValue(Error):  # pylint: disable=g-bad-exception-name
    method __init__ (line 36) | def __init__(self, given, enum_type, options):
  class OperatingSystem (line 50) | class OperatingSystem(object):
    class _OS (line 53) | class _OS(object):
      method __init__ (line 57) | def __init__(self, id, name, file_name):
      method __str__ (line 62) | def __str__(self):
      method __eq__ (line 65) | def __eq__(self, other):
      method __hash__ (line 71) | def __hash__(self):
      method __ne__ (line 74) | def __ne__(self, other):
      method _CmpHelper (line 78) | def _CmpHelper(cls, x, y):
      method __lt__ (line 82) | def __lt__(self, other):
      method __gt__ (line 87) | def __gt__(self, other):
      method __le__ (line 92) | def __le__(self, other):
      method __ge__ (line 95) | def __ge__(self, other):
    method AllValues (line 106) | def AllValues():
    method FromId (line 115) | def FromId(os_id, error_on_unknown=True):
    method Current (line 141) | def Current():
    method IsWindows (line 161) | def IsWindows():
  class Architecture (line 166) | class Architecture(object):
    class _ARCH (line 169) | class _ARCH(object):
      method __init__ (line 173) | def __init__(self, id, name, file_name):
      method __str__ (line 178) | def __str__(self):
      method __eq__ (line 181) | def __eq__(self, other):
      method __hash__ (line 187) | def __hash__(self):
      method __ne__ (line 190) | def __ne__(self, other):
      method _CmpHelper (line 194) | def _CmpHelper(cls, x, y):
      method __lt__ (line 198) | def __lt__(self, other):
      method __gt__ (line 203) | def __gt__(self, other):
      method __le__ (line 208) | def __le__(self, other):
      method __ge__ (line 211) | def __ge__(self, other):
    method AllValues (line 230) | def AllValues():
    method FromId (line 239) | def FromId(architecture_id, error_on_unknown=True):
    method Current (line 265) | def Current():
  class Platform (line 275) | class Platform(object):
    method __init__ (line 278) | def __init__(self, operating_system, architecture):
    method __str__ (line 288) | def __str__(self):
    method Current (line 292) | def Current(os_override=None, arch_override=None):
    method UserAgentFragment (line 307) | def UserAgentFragment(self):
    method AsyncPopenArgs (line 343) | def AsyncPopenArgs(self):
  class PythonVersion (line 380) | class PythonVersion(object):
    method __init__ (line 401) | def __init__(self, version=None):
    method SupportedVersionMessage (line 409) | def SupportedVersionMessage(self, allow_py3):
    method IsCompatible (line 421) | def IsCompatible(self, allow_py3=False, raise_exception=False):

FILE: fire/console/text.py
  class TextAttributes (line 24) | class TextAttributes(object):
    method __init__ (line 27) | def __init__(self, format_str=None, color=None, attrs=None):
    method format_str (line 41) | def format_str(self):
    method color (line 45) | def color(self):
    method attrs (line 49) | def attrs(self):
  class TypedText (line 53) | class TypedText(object):
    method __init__ (line 56) | def __init__(self, texts, text_type=None):
    method __len__ (line 68) | def __len__(self):
    method __add__ (line 74) | def __add__(self, other):
    method __radd__ (line 78) | def __radd__(self, other):
  class _TextTypes (line 83) | class _TextTypes(enum.Enum):
    method __call__ (line 86) | def __call__(self, *args):
  class TextTypes (line 92) | class TextTypes(_TextTypes):

FILE: fire/core.py
  function Fire (line 73) | def Fire(component=None, command=None, name=None, serialize=None):
  function Display (line 167) | def Display(lines, out):
  function CompletionScript (line 172) | def CompletionScript(name, component, shell):
  class FireError (line 177) | class FireError(Exception):
  class FireExit (line 185) | class FireExit(SystemExit):  # pylint: disable=g-bad-exception-name
    method __init__ (line 195) | def __init__(self, code, component_trace):
  function _IsHelpShortcut (line 206) | def _IsHelpShortcut(component_trace, remaining_args):
  function _PrintResult (line 239) | def _PrintResult(component_trace, verbose=False, serialize=None):
  function _DisplayError (line 278) | def _DisplayError(component_trace):
  function _DictAsString (line 305) | def _DictAsString(result, verbose=False):
  function _OneLineResult (line 340) | def _OneLineResult(result):
  function _Fire (line 361) | def _Fire(component, args, parsed_flag_args, context, name=None):
  function _GetMember (line 622) | def _GetMember(component, args):
  function _CallAndUpdateTrace (line 652) | def _CallAndUpdateTrace(component, args, component_trace, treatment='cla...
  function _MakeParseFn (line 705) | def _MakeParseFn(fn, metadata):
  function _ParseArgs (line 763) | def _ParseArgs(fn_args, fn_defaults, num_required_args, kwargs,
  function _ParseKeywordArgs (line 822) | def _ParseKeywordArgs(args, fn_spec):
  function _IsFlag (line 942) | def _IsFlag(argument):
  function _IsSingleCharFlag (line 955) | def _IsSingleCharFlag(argument):
  function _IsMultiCharFlag (line 960) | def _IsMultiCharFlag(argument):
  function _ParseValue (line 965) | def _ParseValue(value, index, arg, metadata):

FILE: fire/core_test.py
  class CoreTest (line 25) | class CoreTest(testutils.BaseTestCase):
    method testOneLineResult (line 27) | def testOneLineResult(self):
    method testOneLineResultCircularRef (line 33) | def testOneLineResultCircularRef(self):
    method testInteractiveMode (line 39) | def testInteractiveMode(self, mock_embed):
    method testInteractiveModeFullArgument (line 46) | def testInteractiveModeFullArgument(self, mock_embed):
    method testInteractiveModeVariables (line 51) | def testInteractiveModeVariables(self, mock_embed):
    method testInteractiveModeVariablesWithName (line 61) | def testInteractiveModeVariablesWithName(self, mock_embed):
    method testHelpWithClass (line 73) | def testHelpWithClass(self):
    method testHelpWithMember (line 81) | def testHelpWithMember(self):
    method testHelpOnErrorInConstructor (line 93) | def testHelpOnErrorInConstructor(self):
    method testHelpWithNamespaceCollision (line 99) | def testHelpWithNamespaceCollision(self):
    method testInvalidParameterRaisesFireExit (line 110) | def testInvalidParameterRaisesFireExit(self):
    method testErrorRaising (line 114) | def testErrorRaising(self):
    method testFireError (line 120) | def testFireError(self):
    method testFireErrorMultipleValues (line 124) | def testFireErrorMultipleValues(self):
    method testPrintEmptyDict (line 128) | def testPrintEmptyDict(self):
    method testPrintOrderedDict (line 134) | def testPrintOrderedDict(self):
    method testPrintNamedTupleField (line 140) | def testPrintNamedTupleField(self):
    method testPrintNamedTupleFieldNameEqualsValue (line 144) | def testPrintNamedTupleFieldNameEqualsValue(self):
    method testPrintNamedTupleIndex (line 148) | def testPrintNamedTupleIndex(self):
    method testPrintSet (line 152) | def testPrintSet(self):
    method testPrintFrozenSet (line 156) | def testPrintFrozenSet(self):
    method testPrintNamedTupleNegativeIndex (line 160) | def testPrintNamedTupleNegativeIndex(self):
    method testCallable (line 164) | def testCallable(self):
    method testCallableWithPositionalArgs (line 172) | def testCallableWithPositionalArgs(self):
    method testStaticMethod (line 178) | def testStaticMethod(self):
    method testClassMethod (line 185) | def testClassMethod(self):
    method testCustomSerialize (line 192) | def testCustomSerialize(self):
    method testLruCacheDecoratorBoundArg (line 216) | def testLruCacheDecoratorBoundArg(self):
    method testLruCacheDecorator (line 221) | def testLruCacheDecorator(self):

FILE: fire/custom_descriptions.py
  function NeedsCustomDescription (line 45) | def NeedsCustomDescription(component):
  function GetStringTypeSummary (line 71) | def GetStringTypeSummary(obj, available_space, line_length):
  function GetStringTypeDescription (line 98) | def GetStringTypeDescription(obj, available_space, line_length):
  function GetSummary (line 131) | def GetSummary(obj, available_space, line_length):
  function GetDescription (line 139) | def GetDescription(obj, available_space, line_length):

FILE: fire/custom_descriptions_test.py
  class CustomDescriptionTest (line 23) | class CustomDescriptionTest(testutils.BaseTestCase):
    method test_string_type_summary_enough_space (line 25) | def test_string_type_summary_enough_space(self):
    method test_string_type_summary_not_enough_space_truncated (line 31) | def test_string_type_summary_not_enough_space_truncated(self):
    method test_string_type_summary_not_enough_space_new_line (line 37) | def test_string_type_summary_not_enough_space_new_line(self):
    method test_string_type_summary_not_enough_space_long_truncated (line 43) | def test_string_type_summary_not_enough_space_long_truncated(self):
    method test_string_type_description_enough_space (line 49) | def test_string_type_description_enough_space(self):
    method test_string_type_description_not_enough_space_truncated (line 55) | def test_string_type_description_not_enough_space_truncated(self):
    method test_string_type_description_not_enough_space_new_line (line 61) | def test_string_type_description_not_enough_space_new_line(self):

FILE: fire/decorators.py
  function SetParseFn (line 29) | def SetParseFn(fn, *arguments):
  function SetParseFns (line 52) | def SetParseFns(*positional, **named):
  function _SetMetadata (line 78) | def _SetMetadata(fn, attribute, value):
  function GetMetadata (line 84) | def GetMetadata(fn) -> Dict[str, Any]:
  function GetParseFns (line 107) | def GetParseFns(fn) -> Dict[str, Any]:

FILE: fire/decorators_test.py
  class NoDefaults (line 22) | class NoDefaults:
    method double (line 26) | def double(self, count):
    method triple (line 30) | def triple(self, count):
    method quadruple (line 34) | def quadruple(self, count):
  function double (line 39) | def double(count):
  class WithDefaults (line 43) | class WithDefaults:
    method example1 (line 46) | def example1(self, arg1=10):
    method example2 (line 50) | def example2(self, arg1=10):
  class MixedArguments (line 54) | class MixedArguments:
    method example3 (line 57) | def example3(self, arg1, arg2):
  class PartialParseFn (line 61) | class PartialParseFn:
    method example4 (line 64) | def example4(self, arg1, arg2):
    method example5 (line 68) | def example5(self, arg1, arg2):
  class WithKwargs (line 72) | class WithKwargs:
    method example6 (line 75) | def example6(self, **kwargs):
  class WithVarArgs (line 82) | class WithVarArgs:
    method example7 (line 85) | def example7(self, arg1, arg2=None, *varargs, **kwargs):  # pylint: di...
  class FireDecoratorsTest (line 89) | class FireDecoratorsTest(testutils.BaseTestCase):
    method testSetParseFnsNamedArgs (line 91) | def testSetParseFnsNamedArgs(self):
    method testSetParseFnsPositionalArgs (line 95) | def testSetParseFnsPositionalArgs(self):
    method testSetParseFnsFnWithPositionalArgs (line 98) | def testSetParseFnsFnWithPositionalArgs(self):
    method testSetParseFnsDefaultsFromPython (line 101) | def testSetParseFnsDefaultsFromPython(self):
    method testSetParseFnsDefaultsFromFire (line 107) | def testSetParseFnsDefaultsFromFire(self):
    method testSetParseFnsNamedDefaultsFromPython (line 117) | def testSetParseFnsNamedDefaultsFromPython(self):
    method testSetParseFnsNamedDefaultsFromFire (line 123) | def testSetParseFnsNamedDefaultsFromFire(self):
    method testSetParseFnsPositionalAndNamed (line 133) | def testSetParseFnsPositionalAndNamed(self):
    method testSetParseFnsOnlySomeTypes (line 137) | def testSetParseFnsOnlySomeTypes(self):
    method testSetParseFnsForKeywordArgs (line 143) | def testSetParseFnsForKeywordArgs(self):
    method testSetParseFn (line 162) | def testSetParseFn(self):

FILE: fire/docstrings.py
  class DocstringInfo (line 58) | class DocstringInfo(
  class ArgInfo (line 66) | class ArgInfo(
  class KwargInfo (line 74) | class KwargInfo(ArgInfo):
  class Namespace (line 79) | class Namespace(dict):
    method __getattr__ (line 82) | def __getattr__(self, key):
    method __setattr__ (line 87) | def __setattr__(self, key, value):
    method __delattr__ (line 90) | def __delattr__(self, key):
  class Sections (line 95) | class Sections(enum.Enum):
  class Formats (line 103) | class Formats(enum.Enum):
  function parse (line 118) | def parse(docstring):
  function _strip_blank_lines (line 211) | def _strip_blank_lines(lines):
  function _is_blank (line 234) | def _is_blank(line):
  function _join_lines (line 238) | def _join_lines(lines):
  function _get_or_create_arg_by_name (line 274) | def _get_or_create_arg_by_name(state, name, is_kwarg=False):
  function _is_arg_name (line 302) | def _is_arg_name(name):
  function _as_arg_name_and_type (line 322) | def _as_arg_name_and_type(text):
  function _as_arg_names (line 345) | def _as_arg_names(names_str):
  function _cast_to_known_type (line 367) | def _cast_to_known_type(name):
  function _consume_google_args_line (line 386) | def _consume_google_args_line(line_info, state):
  function _consume_line (line 411) | def _consume_line(line_info, state):
  function _create_line_info (line 510) | def _create_line_info(line, next_line, previous_line):
  function _update_section_state (line 533) | def _update_section_state(line_info, state):
  function _google_section_permitted (line 577) | def _google_section_permitted(line_info, state):
  function _matches_section_title (line 604) | def _matches_section_title(title, section_title):
  function _matches_section (line 619) | def _matches_section(title, section):
  function _section_from_possible_title (line 639) | def _section_from_possible_title(possible_title):
  function _google_section (line 653) | def _google_section(line_info):
  function _get_after_google_header (line 672) | def _get_after_google_header(line_info):
  function _get_directive (line 678) | def _get_directive(line_info):
  function _get_after_directive (line 696) | def _get_after_directive(line_info):
  function _rst_section (line 705) | def _rst_section(line_info):
  function _line_is_hyphens (line 724) | def _line_is_hyphens(line):
  function _numpy_section (line 729) | def _numpy_section(line_info):
  function _line_is_numpy_parameter_type (line 751) | def _line_is_numpy_parameter_type(line_info):

FILE: fire/docstrings_fuzz_test.py
  class DocstringsFuzzTest (line 26) | class DocstringsFuzzTest(testutils.BaseTestCase):
    method test_fuzz_parse (line 31) | def test_fuzz_parse(self, value):

FILE: fire/docstrings_test.py
  class DocstringsTest (line 27) | class DocstringsTest(testutils.BaseTestCase):
    method test_one_line_simple (line 29) | def test_one_line_simple(self):
    method test_one_line_simple_whitespace (line 37) | def test_one_line_simple_whitespace(self):
    method test_one_line_too_long (line 47) | def test_one_line_too_long(self):
    method test_one_line_runs_over (line 60) | def test_one_line_runs_over(self):
    method test_one_line_runs_over_whitespace (line 73) | def test_one_line_runs_over_whitespace(self):
    method test_google_format_args_only (line 85) | def test_google_format_args_only(self):
    method test_google_format_arg_named_args (line 102) | def test_google_format_arg_named_args(self):
    method test_google_format_typed_args_and_returns (line 115) | def test_google_format_typed_args_and_returns(self):
    method test_google_format_multiline_arg_description (line 143) | def test_google_format_multiline_arg_description(self):
    method test_rst_format_typed_args_and_returns (line 169) | def test_rst_format_typed_args_and_returns(self):
    method test_numpy_format_typed_args_and_returns (line 198) | def test_numpy_format_typed_args_and_returns(self):
    method test_numpy_format_multiline_arg_description (line 232) | def test_numpy_format_multiline_arg_description(self):
    method test_multisection_docstring (line 261) | def test_multisection_docstring(self):
    method test_google_section_with_blank_first_line (line 280) | def test_google_section_with_blank_first_line(self):
    method test_ill_formed_docstring (line 293) | def test_ill_formed_docstring(self):
    method test_strip_blank_lines (line 303) | def test_strip_blank_lines(self):
    method test_numpy_colon_in_description (line 309) | def test_numpy_colon_in_description(self):
    method test_rst_format_typed_args_and_kwargs (line 335) | def test_rst_format_typed_args_and_kwargs(self):

FILE: fire/fire_import_test.py
  class FireImportTest (line 24) | class FireImportTest(testutils.BaseTestCase):
    method testFire (line 27) | def testFire(self):
    method testFireMethods (line 31) | def testFireMethods(self):
    method testNoPrivateMethods (line 34) | def testNoPrivateMethods(self):

FILE: fire/fire_test.py
  class FireTest (line 26) | class FireTest(testutils.BaseTestCase):
    method testFire (line 28) | def testFire(self):
    method testFirePositionalCommand (line 42) | def testFirePositionalCommand(self):
    method testFireInvalidCommandArg (line 47) | def testFireInvalidCommandArg(self):
    method testFireDefaultName (line 52) | def testFireDefaultName(self):
    method testFireNoArgs (line 60) | def testFireNoArgs(self):
    method testFireExceptions (line 63) | def testFireExceptions(self):
    method testFireNamedArgs (line 76) | def testFireNamedArgs(self):
    method testFireNamedArgsSingleHyphen (line 88) | def testFireNamedArgsSingleHyphen(self):
    method testFireNamedArgsWithEquals (line 100) | def testFireNamedArgsWithEquals(self):
    method testFireNamedArgsWithEqualsSingleHyphen (line 106) | def testFireNamedArgsWithEqualsSingleHyphen(self):
    method testFireAllNamedArgs (line 112) | def testFireAllNamedArgs(self):
    method testFireAllNamedArgsOneMissing (line 129) | def testFireAllNamedArgsOneMissing(self):
    method testFirePartialNamedArgs (line 137) | def testFirePartialNamedArgs(self):
    method testFirePartialNamedArgsOneMissing (line 161) | def testFirePartialNamedArgsOneMissing(self):
    method testFireAnnotatedArgs (line 177) | def testFireAnnotatedArgs(self):
    method testFireKeywordOnlyArgs (line 181) | def testFireKeywordOnlyArgs(self):
    method testFireProperties (line 191) | def testFireProperties(self):
    method testFireRecursion (line 195) | def testFireRecursion(self):
    method testFireVarArgs (line 202) | def testFireVarArgs(self):
    method testFireVarArgsWithNamedArgs (line 211) | def testFireVarArgsWithNamedArgs(self):
    method testFireKeywordArgs (line 219) | def testFireKeywordArgs(self):
    method testFireKeywordArgsWithMissingPositionalArgs (line 253) | def testFireKeywordArgsWithMissingPositionalArgs(self):
    method testFireObject (line 261) | def testFireObject(self):
    method testFireDict (line 267) | def testFireDict(self):
    method testFireObjectWithDict (line 275) | def testFireObjectWithDict(self):
    method testFireSet (line 285) | def testFireSet(self):
    method testFireFrozenset (line 290) | def testFireFrozenset(self):
    method testFireList (line 295) | def testFireList(self):
    method testFireObjectWithList (line 301) | def testFireObjectWithList(self):
    method testFireObjectWithTuple (line 307) | def testFireObjectWithTuple(self):
    method testFireObjectWithListAsObject (line 313) | def testFireObjectWithListAsObject(self):
    method testFireObjectWithTupleAsObject (line 318) | def testFireObjectWithTupleAsObject(self):
    method testFireNoComponent (line 323) | def testFireNoComponent(self):
    method testFireUnderscores (line 332) | def testFireUnderscores(self):
    method testFireUnderscoresInArg (line 340) | def testFireUnderscoresInArg(self):
    method testBoolParsing (line 353) | def testBoolParsing(self):
    method testBoolParsingContinued (line 367) | def testBoolParsingContinued(self):
    method testBoolParsingSingleHyphen (line 387) | def testBoolParsingSingleHyphen(self):
    method testBoolParsingLessExpectedCases (line 404) | def testBoolParsingLessExpectedCases(self):
    method testSingleCharFlagParsing (line 435) | def testSingleCharFlagParsing(self):
    method testSingleCharFlagParsingEqualSign (line 460) | def testSingleCharFlagParsingEqualSign(self):
    method testSingleCharFlagParsingExactMatch (line 477) | def testSingleCharFlagParsingExactMatch(self):
    method testSingleCharFlagParsingCapitalLetter (line 494) | def testSingleCharFlagParsingCapitalLetter(self):
    method testBoolParsingWithNo (line 499) | def testBoolParsingWithNo(self):
    method testTraceFlag (line 534) | def testTraceFlag(self):
    method testHelpFlag (line 542) | def testHelpFlag(self):
    method testHelpFlagAndTraceFlag (line 550) | def testHelpFlagAndTraceFlag(self):
    method testTabCompletionNoName (line 559) | def testTabCompletionNoName(self):
    method testTabCompletion (line 564) | def testTabCompletion(self):
    method testTabCompletionWithDict (line 570) | def testTabCompletionWithDict(self):
    method testBasicSeparator (line 577) | def testBasicSeparator(self):
    method testNonComparable (line 596) | def testNonComparable(self):
    method testExtraSeparators (line 609) | def testExtraSeparators(self):
    method testSeparatorForChaining (line 622) | def testSeparatorForChaining(self):
    method testNegativeNumbers (line 644) | def testNegativeNumbers(self):
    method testFloatForExpectedInt (line 649) | def testFloatForExpectedInt(self):
    method testClassInstantiation (line 661) | def testClassInstantiation(self):
    method testTraceErrors (line 669) | def testTraceErrors(self):
    method testClassWithDefaultMethod (line 698) | def testClassWithDefaultMethod(self):
    method testClassWithInvalidProperty (line 703) | def testClassWithInvalidProperty(self):
    method testHelpKwargsDecorator (line 708) | def testHelpKwargsDecorator(self):
    method testFireAsyncio (line 715) | def testFireAsyncio(self):

FILE: fire/formatting.py
  function Indent (line 24) | def Indent(text, spaces=2):
  function Bold (line 31) | def Bold(text):
  function Underline (line 35) | def Underline(text):
  function BoldUnderline (line 39) | def BoldUnderline(text):
  function WrappedJoin (line 43) | def WrappedJoin(items, separator=' | ', width=80):
  function Error (line 66) | def Error(text):
  function EllipsisTruncate (line 70) | def EllipsisTruncate(text, available_space, line_length):
  function EllipsisMiddleTruncate (line 80) | def EllipsisMiddleTruncate(text, available_space, line_length):
  function DoubleQuote (line 92) | def DoubleQuote(text):

FILE: fire/formatting_test.py
  class FormattingTest (line 23) | class FormattingTest(testutils.BaseTestCase):
    method test_bold (line 25) | def test_bold(self):
    method test_underline (line 29) | def test_underline(self):
    method test_indent (line 33) | def test_indent(self):
    method test_indent_multiple_lines (line 37) | def test_indent_multiple_lines(self):
    method test_wrap_one_item (line 41) | def test_wrap_one_item(self):
    method test_wrap_multiple_items (line 45) | def test_wrap_multiple_items(self):
    method test_ellipsis_truncate (line 52) | def test_ellipsis_truncate(self):
    method test_ellipsis_truncate_not_enough_space (line 58) | def test_ellipsis_truncate_not_enough_space(self):
    method test_ellipsis_middle_truncate (line 64) | def test_ellipsis_middle_truncate(self):
    method test_ellipsis_middle_truncate_not_enough_space (line 70) | def test_ellipsis_middle_truncate_not_enough_space(self):

FILE: fire/formatting_windows.py
  function initialize_or_disable (line 30) | def initialize_or_disable():

FILE: fire/helptext.py
  function HelpText (line 50) | def HelpText(component, trace=None, verbose=False):
  function _NameSection (line 97) | def _NameSection(component, info, trace=None, verbose=False) -> tuple[st...
  function _SynopsisSection (line 118) | def _SynopsisSection(component, actions_grouped_by_kind, spec, metadata,
  function _DescriptionSection (line 142) | def _DescriptionSection(component, info) -> tuple[str, str] | None:
  function _CreateKeywordOnlyFlagItem (line 170) | def _CreateKeywordOnlyFlagItem(flag, docstring_info, spec, short_arg):
  function _GetShortFlags (line 176) | def _GetShortFlags(flags):
  function _ArgsAndFlagsSections (line 191) | def _ArgsAndFlagsSections(info, spec, metadata):
  function _UsageDetailsSections (line 287) | def _UsageDetailsSections(component, actions_grouped_by_kind):
  function _GetSummary (line 304) | def _GetSummary(info):
  function _GetDescription (line 309) | def _GetDescription(info):
  function _GetArgsAndFlagsString (line 314) | def _GetArgsAndFlagsString(spec, metadata):
  function _GetPossibleActions (line 363) | def _GetPossibleActions(actions_grouped_by_kind):
  function _GetPossibleActionsString (line 372) | def _GetPossibleActionsString(possible_actions):
  function _GetActionsGroupedByKind (line 378) | def _GetActionsGroupedByKind(component, verbose=False):
  function _GetCurrentCommand (line 405) | def _GetCurrentCommand(trace=None, include_separators=True):
  function _CreateOutputSection (line 414) | def _CreateOutputSection(name: str, content: str) -> str:
  function _CreateArgItem (line 419) | def _CreateArgItem(arg, docstring_info, spec):
  function _CreateFlagItem (line 452) | def _CreateFlagItem(flag, docstring_info, spec, required=False,
  function _GetArgType (line 514) | def _GetArgType(arg, spec):
  function _GetArgDefault (line 537) | def _GetArgDefault(flag, spec):
  function _CreateItem (line 560) | def _CreateItem(name, description, indent=2):
  function _GetArgDescription (line 568) | def _GetArgDescription(name, docstring_info):
  function _MakeUsageDetailsSection (line 576) | def _MakeUsageDetailsSection(action_group):
  function _ValuesUsageDetailsSection (line 597) | def _ValuesUsageDetailsSection(component, values):
  function _NewChoicesSection (line 616) | def _NewChoicesSection(name, choices):
  function UsageText (line 624) | def UsageText(component, trace=None, verbose=False):
  function _GetPossibleActionsUsageString (line 684) | def _GetPossibleActionsUsageString(possible_actions):
  function _UsageAvailabilityLines (line 691) | def _UsageAvailabilityLines(actions_grouped_by_kind):
  function _GetCallableUsageItems (line 703) | def _GetCallableUsageItems(spec, metadata):
  function _KeywordOnlyArguments (line 727) | def _KeywordOnlyArguments(spec, required=True):
  function _GetCallableAvailabilityLines (line 732) | def _GetCallableAvailabilityLines(spec):
  function _CreateAvailabilityLine (line 763) | def _CreateAvailabilityLine(header, items,
  class ActionGroup (line 773) | class ActionGroup:
    method __init__ (line 776) | def __init__(self, name, plural):
    method Add (line 782) | def Add(self, name, member=None):
    method GetItems (line 786) | def GetItems(self):

FILE: fire/helptext_test.py
  class HelpTest (line 27) | class HelpTest(testutils.BaseTestCase):
    method setUp (line 29) | def setUp(self):
    method testHelpTextNoDefaults (line 33) | def testHelpTextNoDefaults(self):
    method testHelpTextNoDefaultsObject (line 43) | def testHelpTextNoDefaultsObject(self):
    method testHelpTextFunction (line 57) | def testHelpTextFunction(self):
    method testHelpTextFunctionWithDefaults (line 70) | def testHelpTextFunctionWithDefaults(self):
    method testHelpTextFunctionWithLongDefaults (line 83) | def testHelpTextFunctionWithLongDefaults(self):
    method testHelpTextFunctionWithKwargs (line 98) | def testHelpTextFunctionWithKwargs(self):
    method testHelpTextFunctionWithKwargsAndDefaults (line 111) | def testHelpTextFunctionWithKwargsAndDefaults(self):
    method testHelpTextFunctionWithDefaultsAndTypes (line 126) | def testHelpTextFunctionWithDefaultsAndTypes(self):
    method testHelpTextFunctionWithTypesAndDefaultNone (line 140) | def testHelpTextFunctionWithTypesAndDefaultNone(self):
    method testHelpTextFunctionWithTypes (line 155) | def testHelpTextFunctionWithTypes(self):
    method testHelpTextFunctionWithLongTypes (line 170) | def testHelpTextFunctionWithLongTypes(self):
    method testHelpTextFunctionWithBuiltin (line 189) | def testHelpTextFunctionWithBuiltin(self):
    method testHelpTextFunctionIntType (line 201) | def testHelpTextFunctionIntType(self):
    method testHelpTextEmptyList (line 211) | def testHelpTextEmptyList(self):
    method testHelpTextShortList (line 226) | def testHelpTextShortList(self):
    method testHelpTextInt (line 244) | def testHelpTextInt(self):
    method testHelpTextNoInit (line 257) | def testHelpTextNoInit(self):
    method testHelpTextKeywordOnlyArgumentsWithDefault (line 265) | def testHelpTextKeywordOnlyArgumentsWithDefault(self):
    method testHelpTextKeywordOnlyArgumentsWithoutDefault (line 272) | def testHelpTextKeywordOnlyArgumentsWithoutDefault(self):
    method testHelpTextFunctionMixedDefaults (line 279) | def testHelpTextFunctionMixedDefaults(self):
    method testHelpScreen (line 288) | def testHelpScreen(self):
    method testHelpScreenForFunctionDocstringWithLineBreak (line 316) | def testHelpScreenForFunctionDocstringWithLineBreak(self):
    method testHelpScreenForFunctionFunctionWithDefaultArgs (line 339) | def testHelpScreenForFunctionFunctionWithDefaultArgs(self):
    method testHelpTextUnderlineFlag (line 360) | def testHelpTextUnderlineFlag(self):
    method testHelpTextBoldCommandName (line 373) | def testHelpTextBoldCommandName(self):
    method testHelpTextObjectWithGroupAndValues (line 385) | def testHelpTextObjectWithGroupAndValues(self):
    method testHelpTextNameSectionCommandWithSeparator (line 400) | def testHelpTextNameSectionCommandWithSeparator(self):
    method testHelpTextNameSectionCommandWithSeparatorVerbose (line 408) | def testHelpTextNameSectionCommandWithSeparatorVerbose(self):
    method testHelpTextMultipleKeywoardArgumentsWithShortArgs (line 416) | def testHelpTextMultipleKeywoardArgumentsWithShortArgs(self):
  class UsageTest (line 431) | class UsageTest(testutils.BaseTestCase):
    method testUsageOutput (line 433) | def testUsageOutput(self):
    method testUsageOutputVerbose (line 448) | def testUsageOutputVerbose(self):
    method testUsageOutputMethod (line 462) | def testUsageOutputMethod(self):
    method testUsageOutputFunctionWithHelp (line 476) | def testUsageOutputFunctionWithHelp(self):
    method testUsageOutputFunctionWithDocstring (line 490) | def testUsageOutputFunctionWithDocstring(self):
    method testUsageOutputFunctionMixedDefaults (line 504) | def testUsageOutputFunctionMixedDefaults(self):
    method testUsageOutputCallable (line 518) | def testUsageOutputCallable(self):
    method testUsageOutputConstructorWithParameter (line 535) | def testUsageOutputConstructorWithParameter(self):
    method testUsageOutputConstructorWithParameterVerbose (line 548) | def testUsageOutputConstructorWithParameterVerbose(self):
    method testUsageOutputEmptyDict (line 562) | def testUsageOutputEmptyDict(self):
    method testUsageOutputNone (line 575) | def testUsageOutputNone(self):
    method testInitRequiresFlagSyntaxSubclassNamedTuple (line 588) | def testInitRequiresFlagSyntaxSubclassNamedTuple(self):

FILE: fire/inspectutils.py
  class FullArgSpec (line 24) | class FullArgSpec:
    method __init__ (line 27) | def __init__(self, args=None, varargs=None, varkw=None, defaults=None,
  function _GetArgSpecInfo (line 49) | def _GetArgSpecInfo(fn):
  function Py3GetFullArgSpec (line 87) | def Py3GetFullArgSpec(fn):
  function GetFullArgSpec (line 161) | def GetFullArgSpec(fn):
  function GetFileAndLine (line 209) | def GetFileAndLine(component):
  function Info (line 236) | def Info(component):
  function _InfoBackup (line 281) | def _InfoBackup(component):
  function IsNamedTuple (line 312) | def IsNamedTuple(component):
  function GetClassAttrsDict (line 334) | def GetClassAttrsDict(component):
  function IsCoroutineFunction (line 345) | def IsCoroutineFunction(fn):

FILE: fire/inspectutils_test.py
  class InspectUtilsTest (line 24) | class InspectUtilsTest(testutils.BaseTestCase):
    method testGetFullArgSpec (line 26) | def testGetFullArgSpec(self):
    method testGetFullArgSpecPy3 (line 36) | def testGetFullArgSpecPy3(self):
    method testGetFullArgSpecFromBuiltin (line 47) | def testGetFullArgSpecFromBuiltin(self):
    method testGetFullArgSpecFromSlotWrapper (line 55) | def testGetFullArgSpecFromSlotWrapper(self):
    method testGetFullArgSpecFromNamedTuple (line 65) | def testGetFullArgSpecFromNamedTuple(self):
    method testGetFullArgSpecFromNamedTupleSubclass (line 75) | def testGetFullArgSpecFromNamedTupleSubclass(self):
    method testGetFullArgSpecFromClassNoInit (line 85) | def testGetFullArgSpecFromClassNoInit(self):
    method testGetFullArgSpecFromMethod (line 95) | def testGetFullArgSpecFromMethod(self):
    method testInfoOne (line 105) | def testInfoOne(self):
    method testInfoClass (line 112) | def testInfoClass(self):
    method testInfoClassNoInit (line 118) | def testInfoClassNoInit(self):
    method testInfoNoDocstring (line 124) | def testInfoNoDocstring(self):

FILE: fire/interact.py
  function Embed (line 26) | def Embed(variables, verbose=False):
  function _AvailableString (line 42) | def _AvailableString(variables, verbose=False):
  function _EmbedIPython (line 81) | def _EmbedIPython(variables, argv=None):
  function _EmbedCode (line 94) | def _EmbedCode(variables):

FILE: fire/interact_test.py
  class InteractTest (line 30) | class InteractTest(testutils.BaseTestCase):
    method testInteract (line 33) | def testInteract(self, mock_interact_method):
    method testInteractVariables (line 39) | def testInteractVariables(self, mock_interact_method):

FILE: fire/main_test.py
  class MainModuleTest (line 24) | class MainModuleTest(testutils.BaseTestCase):
    method testNameSetting (line 27) | def testNameSetting(self):
    method testArgPassing (line 32) | def testArgPassing(self):
  class MainModuleFileTest (line 42) | class MainModuleFileTest(testutils.BaseTestCase):
    method setUp (line 45) | def setUp(self):
    method testFileNameFire (line 53) | def testFileNameFire(self):
    method testFileNameFailure (line 59) | def testFileNameFailure(self):
    method testFileNameModuleDuplication (line 65) | def testFileNameModuleDuplication(self):
    method testFileNameModuleFileFailure (line 78) | def testFileNameModuleFileFailure(self):

FILE: fire/parser.py
  function CreateParser (line 27) | def CreateParser():
  function SeparateFlagArgs (line 39) | def SeparateFlagArgs(args):
  function DefaultParseValue (line 59) | def DefaultParseValue(value):
  function _LiteralEval (line 79) | def _LiteralEval(value):
  function _Replacement (line 119) | def _Replacement(node):

FILE: fire/parser_fuzz_test.py
  class ParserFuzzTest (line 26) | class ParserFuzzTest(testutils.BaseTestCase):
    method testDefaultParseValueFuzz (line 51) | def testDefaultParseValueFuzz(self, value):

FILE: fire/parser_test.py
  class ParserTest (line 21) | class ParserTest(testutils.BaseTestCase):
    method testCreateParser (line 23) | def testCreateParser(self):
    method testSeparateFlagArgs (line 26) | def testSeparateFlagArgs(self):
    method testDefaultParseValueStrings (line 44) | def testDefaultParseValueStrings(self):
    method testDefaultParseValueQuotedStrings (line 50) | def testDefaultParseValueQuotedStrings(self):
    method testDefaultParseValueSpecialStrings (line 58) | def testDefaultParseValueSpecialStrings(self):
    method testDefaultParseValueNumbers (line 66) | def testDefaultParseValueNumbers(self):
    method testDefaultParseValueStringNumbers (line 75) | def testDefaultParseValueStringNumbers(self):
    method testDefaultParseValueQuotedStringNumbers (line 83) | def testDefaultParseValueQuotedStringNumbers(self):
    method testDefaultParseValueOtherNumbers (line 86) | def testDefaultParseValueOtherNumbers(self):
    method testDefaultParseValueLists (line 89) | def testDefaultParseValueLists(self):
    method testDefaultParseValueBareWordsLists (line 96) | def testDefaultParseValueBareWordsLists(self):
    method testDefaultParseValueDict (line 99) | def testDefaultParseValueDict(self):
    method testDefaultParseValueNone (line 103) | def testDefaultParseValueNone(self):
    method testDefaultParseValueBool (line 106) | def testDefaultParseValueBool(self):
    method testDefaultParseValueBareWordsTuple (line 110) | def testDefaultParseValueBareWordsTuple(self):
    method testDefaultParseValueNestedContainers (line 114) | def testDefaultParseValueNestedContainers(self):
    method testDefaultParseValueComments (line 120) | def testDefaultParseValueComments(self):
    method testDefaultParseValueBadLiteral (line 125) | def testDefaultParseValueBadLiteral(self):
    method testDefaultParseValueSyntaxError (line 131) | def testDefaultParseValueSyntaxError(self):
    method testDefaultParseValueIgnoreBinOp (line 135) | def testDefaultParseValueIgnoreBinOp(self):

FILE: fire/test_components.py
  function identity (line 24) | def identity(arg1, arg2, arg3=10, arg4=20, *arg5, **arg6):  # pylint: di...
  function multiplier_with_docstring (line 30) | def multiplier_with_docstring(num, rate=2):
  function function_with_help (line 42) | def function_with_help(help=True):  # pylint: disable=redefined-builtin
  class Empty (line 46) | class Empty:
  class OldStyleEmpty (line 50) | class OldStyleEmpty:  # pylint: disable=old-style-class,no-init
  class WithInit (line 54) | class WithInit:
    method __init__ (line 56) | def __init__(self):
  class ErrorInConstructor (line 60) | class ErrorInConstructor:
    method __init__ (line 62) | def __init__(self, value='value'):
  class WithHelpArg (line 67) | class WithHelpArg:
    method __init__ (line 70) | def __init__(self, help=True):  # pylint: disable=redefined-builtin
  class NoDefaults (line 75) | class NoDefaults:
    method double (line 77) | def double(self, count):
    method triple (line 80) | def triple(self, count):
  class WithDefaults (line 84) | class WithDefaults:
    method double (line 87) | def double(self, count=0):
    method triple (line 98) | def triple(self, count=0):
    method text (line 101) | def text(
  class OldStyleWithDefaults (line 109) | class OldStyleWithDefaults:  # pylint: disable=old-style-class,no-init
    method double (line 111) | def double(self, count=0):
    method triple (line 114) | def triple(self, count=0):
  class MixedDefaults (line 118) | class MixedDefaults:
    method ten (line 120) | def ten(self):
    method sum (line 123) | def sum(self, alpha=0, beta=0):
    method identity (line 126) | def identity(self, alpha, beta='0'):
  class SimilarArgNames (line 130) | class SimilarArgNames:
    method identity (line 132) | def identity(self, bool_one=False, bool_two=False):
    method identity2 (line 135) | def identity2(self, a=None, alpha=None):
  class CapitalizedArgNames (line 139) | class CapitalizedArgNames:
    method sum (line 141) | def sum(self, Delta=1.0, Gamma=2.0):  # pylint: disable=invalid-name
  class Annotations (line 145) | class Annotations:
    method double (line 147) | def double(self, count=0):
    method triple (line 150) | def triple(self, count=0):
  class TypedProperties (line 157) | class TypedProperties:
    method __init__ (line 160) | def __init__(self):
  class VarArgs (line 176) | class VarArgs:
    method cumsums (line 179) | def cumsums(self, *items):
    method varchars (line 190) | def varchars(self, alpha=0, beta=0, *chars):  # pylint: disable=keywor...
  class Underscores (line 194) | class Underscores:
    method __init__ (line 196) | def __init__(self):
    method underscore_function (line 199) | def underscore_function(self, underscore_arg):
  class BoolConverter (line 203) | class BoolConverter:
    method as_bool (line 205) | def as_bool(self, arg=False):
  class ReturnsObj (line 209) | class ReturnsObj:
    method get_obj (line 211) | def get_obj(self, *items):
  class NumberDefaults (line 216) | class NumberDefaults:
    method reciprocal (line 218) | def reciprocal(self, divisor=10.0):
    method integer_reciprocal (line 221) | def integer_reciprocal(self, divisor=10):
  class InstanceVars (line 225) | class InstanceVars:
    method __init__ (line 227) | def __init__(self, arg1, arg2):
    method run (line 231) | def run(self, arg1, arg2):
  class Kwargs (line 235) | class Kwargs:
    method props (line 237) | def props(self, **kwargs):
    method upper (line 240) | def upper(self, **kwargs):
    method run (line 243) | def run(self, positional, named=None, **kwargs):
  class ErrorRaiser (line 247) | class ErrorRaiser:
    method fail (line 249) | def fail(self):
  class NonComparable (line 253) | class NonComparable:
    method __eq__ (line 255) | def __eq__(self, other):
    method __ne__ (line 258) | def __ne__(self, other):
  class EmptyDictOutput (line 262) | class EmptyDictOutput:
    method totally_empty (line 264) | def totally_empty(self):
    method nothing_printable (line 267) | def nothing_printable(self):
  class CircularReference (line 271) | class CircularReference:
    method create (line 273) | def create(self):
  class OrderedDictionary (line 279) | class OrderedDictionary:
    method empty (line 281) | def empty(self):
    method non_empty (line 284) | def non_empty(self):
  class NamedTuple (line 291) | class NamedTuple:
    method point (line 294) | def point(self):
    method matching_names (line 300) | def matching_names(self):
  class CallableWithPositionalArgs (line 307) | class CallableWithPositionalArgs:
    method __call__ (line 312) | def __call__(self, x, y):
    method fn (line 315) | def fn(self, x):
  class SubPoint (line 322) | class SubPoint(NamedTuplePoint):
    method coordinate_sum (line 325) | def coordinate_sum(self):
  class CallableWithKeywordArgument (line 329) | class CallableWithKeywordArgument:
    method __call__ (line 332) | def __call__(self, **kwargs):
    method print_msg (line 336) | def print_msg(self, msg):
  class ClassWithDocstring (line 343) | class ClassWithDocstring:
    method __init__ (line 349) | def __init__(self, message='Hello!'):
    method print_msg (line 359) | def print_msg(self, msg=None):
  class ClassWithMultilineDocstring (line 366) | class ClassWithMultilineDocstring:
    method example_generator (line 374) | def example_generator(n):
  function simple_set (line 394) | def simple_set():
  function simple_frozenset (line 398) | def simple_frozenset():
  class Subdict (line 402) | class Subdict(dict):
  class Color (line 410) | class Color(enum.Enum):
  class HasStaticAndClassMethods (line 416) | class HasStaticAndClassMethods:
    method __init__ (line 421) | def __init__(self, instance_state):
    method static_fn (line 425) | def static_fn(args):
    method class_fn (line 429) | def class_fn(cls, args):
  function function_with_varargs (line 433) | def function_with_varargs(arg1, arg2, arg3=1, *varargs):  # pylint: disa...
  function function_with_keyword_arguments (line 448) | def function_with_keyword_arguments(arg1, arg2=3, **kwargs):
  function fn_with_code_in_docstring (line 453) | def fn_with_code_in_docstring():
  class BinaryCanvas (line 470) | class BinaryCanvas:
    method __init__ (line 473) | def __init__(self, size=10):
    method __str__ (line 479) | def __str__(self):
    method show (line 483) | def show(self):
    method move (line 487) | def move(self, row, col):
    method on (line 492) | def on(self):
    method off (line 495) | def off(self):
    method set (line 498) | def set(self, value):
  class DefaultMethod (line 503) | class DefaultMethod:
    method double (line 505) | def double(self, number):
    method __getattr__ (line 508) | def __getattr__(self, name):
  class InvalidProperty (line 514) | class InvalidProperty:
    method double (line 516) | def double(self, number):
    method prop (line 520) | def prop(self):
  function simple_decorator (line 524) | def simple_decorator(f):
  function decorated_method (line 532) | def decorated_method(name='World'):
  function fn_with_kwarg (line 537) | def fn_with_kwarg(arg1, arg2, **kwargs):
  function fn_with_kwarg_and_defaults (line 548) | def fn_with_kwarg_and_defaults(arg1, arg2, opt=True, **kwargs):
  function fn_with_multiple_defaults (line 559) | def fn_with_multiple_defaults(first='first', last='last', late='late'):

FILE: fire/test_components_bin.py
  function main (line 24) | def main():

FILE: fire/test_components_py3.py
  function identity (line 23) | def identity(arg1, arg2: int, arg3=10, arg4: int = 20, *arg5,
  class HelpTextComponent (line 28) | class HelpTextComponent:
    method identity (line 30) | def identity(self, *, alpha, beta='0'):
  class KeywordOnly (line 34) | class KeywordOnly:
    method double (line 36) | def double(self, *, count):
    method triple (line 39) | def triple(self, *, count):
    method with_default (line 42) | def with_default(self, *, x="x"):
  class LruCacheDecoratedMethod (line 46) | class LruCacheDecoratedMethod:
    method lru_cache_in_class (line 49) | def lru_cache_in_class(self, arg1):
  function lru_cache_decorated (line 54) | def lru_cache_decorated(arg1):
  class WithAsyncio (line 58) | class WithAsyncio:
    method double (line 60) | async def double(self, count=0):
  class WithTypes (line 64) | class WithTypes:
    method double (line 67) | def double(self, count: float) -> float:
    method long_type (line 78) | def long_type(
  class WithDefaultsAndTypes (line 86) | class WithDefaultsAndTypes:
    method double (line 89) | def double(self, count: float = 0) -> float:
    method get_int (line 100) | def get_int(self, value: int = None):

FILE: fire/test_components_test.py
  class TestComponentsTest (line 21) | class TestComponentsTest(testutils.BaseTestCase):
    method testTestComponents (line 24) | def testTestComponents(self):
    method testNonComparable (line 28) | def testNonComparable(self):

FILE: fire/testutils.py
  class BaseTestCase (line 29) | class BaseTestCase(unittest.TestCase):
    method assertOutputMatches (line 33) | def assertOutputMatches(self, stdout='.*', stderr='.*', capture=True):
    method assertRaisesFireExit (line 71) | def assertRaisesFireExit(self, code, regexp='.*'):
  function ChangeDirectory (line 97) | def ChangeDirectory(directory):

FILE: fire/testutils_test.py
  class TestTestUtils (line 22) | class TestTestUtils(testutils.BaseTestCase):
    method testNoCheckOnException (line 25) | def testNoCheckOnException(self):
    method testCheckStdoutOrStderrNone (line 30) | def testCheckStdoutOrStderrNone(self):
    method testCorrectOrderingOfAssertRaises (line 44) | def testCorrectOrderingOfAssertRaises(self):

FILE: fire/trace.py
  class FireTrace (line 41) | class FireTrace:
    method __init__ (line 49) | def __init__(self, initial_component, name=None, separator='-', verbos...
    method GetResult (line 63) | def GetResult(self):
    method GetLastHealthyElement (line 67) | def GetLastHealthyElement(self):
    method HasError (line 80) | def HasError(self):
    method AddAccessedProperty (line 84) | def AddAccessedProperty(self, component, target, args, filename, lineno):
    method AddCalledComponent (line 95) | def AddCalledComponent(self, component, target, args, filename, lineno,
    method AddCompletionScript (line 121) | def AddCompletionScript(self, script):
    method AddInteractiveMode (line 128) | def AddInteractiveMode(self):
    method AddError (line 132) | def AddError(self, error, args):
    method AddSeparator (line 136) | def AddSeparator(self):
    method _Quote (line 160) | def _Quote(self, arg):
    method GetCommand (line 166) | def GetCommand(self, include_separators=True):
    method NeedsSeparator (line 192) | def NeedsSeparator(self):
    method __str__ (line 210) | def __str__(self):
    method NeedsSeparatingHyphenHyphen (line 217) | def NeedsSeparatingHyphenHyphen(self, flag='help'):
  class FireTraceElement (line 239) | class FireTraceElement:
    method __init__ (line 246) | def __init__(self,
    method HasError (line 277) | def HasError(self):
    method HasCapacity (line 280) | def HasCapacity(self):
    method HasSeparator (line 283) | def HasSeparator(self):
    method AddSeparator (line 286) | def AddSeparator(self):
    method ErrorAsStr (line 289) | def ErrorAsStr(self):
    method __str__ (line 292) | def __str__(self):

FILE: fire/trace_test.py
  class FireTraceTest (line 21) | class FireTraceTest(testutils.BaseTestCase):
    method testFireTraceInitialization (line 23) | def testFireTraceInitialization(self):
    method testFireTraceGetResult (line 28) | def testFireTraceGetResult(self):
    method testFireTraceHasError (line 34) | def testFireTraceHasError(self):
    method testAddAccessedProperty (line 42) | def testAddAccessedProperty(self):
    method testAddCalledCallable (line 50) | def testAddCalledCallable(self):
    method testAddCalledRoutine (line 59) | def testAddCalledRoutine(self):
    method testAddInstantiatedClass (line 68) | def testAddInstantiatedClass(self):
    method testAddCompletionScript (line 78) | def testAddCompletionScript(self):
    method testAddInteractiveMode (line 85) | def testAddInteractiveMode(self):
    method testGetCommand (line 92) | def testGetCommand(self):
    method testGetCommandWithQuotes (line 99) | def testGetCommandWithQuotes(self):
    method testGetCommandWithFlagQuotes (line 106) | def testGetCommandWithFlagQuotes(self):
  class FireTraceElementTest (line 114) | class FireTraceElementTest(testutils.BaseTestCase):
    method testFireTraceElementHasError (line 116) | def testFireTraceElementHasError(self):
    method testFireTraceElementAsStringNoMetadata (line 123) | def testFireTraceElementAsStringNoMetadata(self):
    method testFireTraceElementAsStringWithTarget (line 130) | def testFireTraceElementAsStringWithTarget(self):
    method testFireTraceElementAsStringWithTargetAndLineNo (line 138) | def testFireTraceElementAsStringWithTargetAndLineNo(self):

FILE: fire/value_types.py
  function IsGroup (line 26) | def IsGroup(component):
  function IsCommand (line 31) | def IsCommand(component):
  function IsValue (line 35) | def IsValue(component):
  function IsSimpleGroup (line 39) | def IsSimpleGroup(component):
  function HasCustomStr (line 58) | def HasCustomStr(component):
Condensed preview — 79 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (465K chars).
[
  {
    "path": ".github/dependabot.yml",
    "chars": 749,
    "preview": "# Basic dependabot.yml file with minimum configuration for two package managers\n\nversion: 2\nupdates:\n  # Enable version "
  },
  {
    "path": ".github/scripts/build.sh",
    "chars": 1121,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": ".github/workflows/build.yml",
    "chars": 909,
    "preview": "name: Python Fire\n\non:\n  push:\n    branches: [\"master\"]\n  pull_request:\n    branches: [\"master\"]\n\ndefaults:\n  run:\n    s"
  },
  {
    "path": ".gitignore",
    "chars": 1172,
    "preview": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packagi"
  },
  {
    "path": "CONTRIBUTING.md",
    "chars": 2521,
    "preview": "# How to contribute\n\nWe'd love to accept your patches and contributions to this project. There are\njust a few small guid"
  },
  {
    "path": "LICENSE",
    "chars": 573,
    "preview": "Copyright 2017 Google Inc. All rights reserved.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may"
  },
  {
    "path": "README.md",
    "chars": 4049,
    "preview": "# Python Fire [![PyPI](https://img.shields.io/pypi/pyversions/fire.svg?style=plastic)](https://github.com/google/python-"
  },
  {
    "path": "docs/api.md",
    "chars": 3037,
    "preview": "## Python Fire Quick Reference\n\n| Setup   | Command             | Notes\n| ------- | ------------------- | ----------\n| i"
  },
  {
    "path": "docs/benefits.md",
    "chars": 2700,
    "preview": "# Benefits of Python Fire\n\n<a name=\"simple-cli\"></a>\n## Create CLIs in Python\n\nIt's dead simple. Simply write the functi"
  },
  {
    "path": "docs/guide.md",
    "chars": 19863,
    "preview": "## The Python Fire Guide\n\n### Introduction\n\nWelcome to the Python Fire guide! Python Fire is a Python library that will "
  },
  {
    "path": "docs/index.md",
    "chars": 4047,
    "preview": "# Python Fire [![PyPI](https://img.shields.io/pypi/pyversions/fire.svg?style=plastic)](https://github.com/google/python-"
  },
  {
    "path": "docs/installation.md",
    "chars": 333,
    "preview": "# Installation\n\nTo install Python Fire with pip, run: `pip install fire`\n\nTo install Python Fire with conda, run: `conda"
  },
  {
    "path": "docs/troubleshooting.md",
    "chars": 651,
    "preview": "# Troubleshooting\n\nThis page describes known issues that users of Python Fire have run into. If you\nhave an issue not re"
  },
  {
    "path": "docs/using-cli.md",
    "chars": 9714,
    "preview": "# Using a Fire CLI\n\n## Basic usage\n\nEvery Fire command corresponds to a Python component.\n\nThe simplest Fire command con"
  },
  {
    "path": "examples/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "examples/cipher/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "examples/cipher/cipher.py",
    "chars": 1668,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/cipher/cipher_test.py",
    "chars": 1174,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/diff/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "examples/diff/diff.py",
    "chars": 3189,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/diff/diff_test.py",
    "chars": 2676,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/diff/difffull.py",
    "chars": 1757,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/identity/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "examples/identity/identity.py",
    "chars": 790,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/widget/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "examples/widget/collector.py",
    "chars": 1127,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/widget/collector_test.py",
    "chars": 1190,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/widget/widget.py",
    "chars": 976,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "examples/widget/widget_test.py",
    "chars": 1081,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/__init__.py",
    "chars": 676,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/__main__.py",
    "chars": 3788,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/completion.py",
    "chars": 15992,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/completion_test.py",
    "chars": 6266,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/console/README.md",
    "chars": 228,
    "preview": "This is the console package from googlecloudsdk, as used by Python Fire.\nPython Fire does not accept pull requests modif"
  },
  {
    "path": "fire/console/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "fire/console/console_attr.py",
    "chars": 23495,
    "preview": "# -*- coding: utf-8 -*- #\n\n# Copyright 2015 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Vers"
  },
  {
    "path": "fire/console/console_attr_os.py",
    "chars": 7458,
    "preview": "# -*- coding: utf-8 -*- #\n# Copyright 2015 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Versi"
  },
  {
    "path": "fire/console/console_io.py",
    "chars": 4149,
    "preview": "# -*- coding: utf-8 -*- #\n# Copyright 2013 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Versi"
  },
  {
    "path": "fire/console/console_pager.py",
    "chars": 9563,
    "preview": "# -*- coding: utf-8 -*- #\n# Copyright 2015 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Versi"
  },
  {
    "path": "fire/console/encoding.py",
    "chars": 6039,
    "preview": "# -*- coding: utf-8 -*- #\n\n# Copyright 2015 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Vers"
  },
  {
    "path": "fire/console/files.py",
    "chars": 4056,
    "preview": "# -*- coding: utf-8 -*- #\n# Copyright 2013 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Versi"
  },
  {
    "path": "fire/console/platforms.py",
    "chars": 16487,
    "preview": "# -*- coding: utf-8 -*- #\n# Copyright 2013 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Versi"
  },
  {
    "path": "fire/console/text.py",
    "chars": 2776,
    "preview": "# -*- coding: utf-8 -*- #\n# Copyright 2018 Google LLC. All Rights Reserved.\n#\n# Licensed under the Apache License, Versi"
  },
  {
    "path": "fire/core.py",
    "chars": 36731,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/core_test.py",
    "chars": 9613,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/custom_descriptions.py",
    "chars": 5247,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/custom_descriptions_test.py",
    "chars": 2654,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/decorators.py",
    "chars": 3561,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/decorators_test.py",
    "chars": 5582,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/docstrings.py",
    "chars": 25023,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/docstrings_fuzz_test.py",
    "chars": 1100,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/docstrings_test.py",
    "chars": 12284,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/fire_import_test.py",
    "chars": 1107,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/fire_test.py",
    "chars": 29218,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/formatting.py",
    "chars": 2716,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/formatting_test.py",
    "chars": 2647,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/formatting_windows.py",
    "chars": 1984,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/helptext.py",
    "chars": 27341,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/helptext_test.py",
    "chars": 22705,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/inspectutils.py",
    "chars": 11966,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/inspectutils_test.py",
    "chars": 4997,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/interact.py",
    "chars": 3036,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/interact_test.py",
    "chars": 1436,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/main_test.py",
    "chars": 3318,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/parser.py",
    "chars": 4697,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/parser_fuzz_test.py",
    "chars": 3041,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/parser_test.py",
    "chars": 6379,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/test_components.py",
    "chars": 11723,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/test_components_bin.py",
    "chars": 807,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/test_components_py3.py",
    "chars": 2394,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/test_components_test.py",
    "chars": 1230,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/testutils.py",
    "chars": 3450,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/testutils_test.py",
    "chars": 1753,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/trace.py",
    "chars": 10314,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/trace_test.py",
    "chars": 5112,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "fire/value_types.py",
    "chars": 2615,
    "preview": "# Copyright (C) 2018 Google Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use t"
  },
  {
    "path": "mkdocs.yml",
    "chars": 307,
    "preview": "site_name: Python Fire\ntheme: readthedocs\nmarkdown_extensions: [fenced_code]\nnav:\n    - Overview: index.md\n    - Install"
  },
  {
    "path": "pylintrc",
    "chars": 6494,
    "preview": "[MASTER]\n\n# Specify a configuration file.\n#rcfile=\n\n# Python code to execute, usually for sys.path manipulation such as\n"
  },
  {
    "path": "pyproject.toml",
    "chars": 1862,
    "preview": "[build-system]\nrequires = [\"setuptools>=45\", \"wheel\"]\nbuild-backend = \"setuptools.build_meta\"\n\n[project]\nname = \"fire\"\nv"
  }
]

About this extraction

This page contains the full source code of the google/python-fire GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 79 files (434.1 KB), approximately 108.0k tokens, and a symbol index with 782 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!