### Simple Hello World Alternative Source: https://github.com/google/python-fire/blob/master/docs/guide.md An extremely simple Python Fire CLI example that exposes string variables. It demonstrates how to create a CLI with minimal code, allowing users to access predefined string values. ```python import fire english = 'Hello World' spanish = 'Hola Mundo' fire.Fire() ``` -------------------------------- ### Building Class CLI Usage Examples Source: https://github.com/google/python-fire/blob/master/docs/guide.md Provides various examples of calling the Building class CLI. It illustrates different ways to pass arguments to the constructor and the `climb_stairs` method, including positional arguments, flags, and the interchangeability of hyphens and underscores. ```bash python example.py --name="Sherrerd Hall" --stories=3 climb_stairs 10 ``` ```bash python example.py --name="Sherrerd Hall" climb_stairs --stairs_per_story=10 ``` ```bash python example.py --name="Sherrerd Hall" climb_stairs --stairs-per-story 10 ``` ```bash python example.py climb-stairs --stairs-per-story 10 --name="Sherrerd Hall" ``` -------------------------------- ### Install Python Fire from Source Source: https://github.com/google/python-fire/blob/master/docs/installation.md Installs the Python Fire library directly from its source code repository. This involves cloning the repository and then running the setup script. ```bash git clone https://github.com/google/python-fire.git cd python-fire python setup.py install ``` -------------------------------- ### Building Class CLI Example Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates creating a Python Fire CLI with a class that has a constructor and a method. It shows how to pass arguments to the constructor and methods using both positional and flag syntax, including handling of underscores and hyphens. ```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) ``` -------------------------------- ### Install Python Fire Source: https://github.com/google/python-fire/blob/master/docs/api.md Installs the Python Fire library from PyPI using pip. ```bash pip install fire ``` -------------------------------- ### CLI Usage Examples Source: https://github.com/google/python-fire/blob/master/README.md Provides examples of how to interact with CLIs generated by Python Fire from the command line, including passing arguments and accessing help. ```bash python hello.py # Hello World! python hello.py --name=David # Hello David! python hello.py --help # Shows usage information. ``` ```bash python calculator.py double 10 # 20 python calculator.py double --number=15 # 30 ``` -------------------------------- ### Install Python Fire from Source for Development Source: https://github.com/google/python-fire/blob/master/docs/installation.md Installs the Python Fire library from its source code in a development mode, allowing for direct modifications and testing of the library's code. ```bash git clone https://github.com/google/python-fire.git cd python-fire python setup.py develop ``` -------------------------------- ### Hello World with fire.Fire() Source: https://github.com/google/python-fire/blob/master/docs/guide.md Exposes a specific function to the command line by passing it directly to fire.Fire(). This simplifies calling the function from the CLI. ```python import fire def hello(name): return f'Hello {name}!' if __name__ == '__main__': fire.Fire(hello) ``` -------------------------------- ### Hello World with fire.Fire() Source: https://github.com/google/python-fire/blob/master/docs/guide.md Exposes all functions in a Python script to the command line using fire.Fire(). The CLI can then call these functions by name. ```python import fire def hello(name): return f'Hello {name}!' if __name__ == '__main__': fire.Fire() ``` -------------------------------- ### Hello World using a main function Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates organizing Fire CLI logic within a main function, which can then be called by fire.Fire(). This is useful for more structured applications or when using entry points. ```python import fire def hello(name): return f'Hello {name}!' def main(): fire.Fire(hello) if __name__ == '__main__': main() ``` -------------------------------- ### Airport Property Access CLI Source: https://github.com/google/python-fire/blob/master/docs/guide.md Example of accessing class properties via the CLI. `fire.Fire(Airport)` allows accessing attributes like `code`, `name`, and `city`. ```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) ``` -------------------------------- ### Pipeline CLI Usage Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates invoking commands from the nested Pipeline CLI structure. ```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. ``` -------------------------------- ### Chaining Function Calls CLI Usage Source: https://github.com/google/python-fire/blob/master/docs/guide.md Example of using the Python Fire CLI to chain function calls. This command sequence manipulates a BinaryCanvas object, demonstrating the chaining capability by calling `move`, `on`, and `show` methods sequentially. ```bash python example.py --code=ALB city upper ``` ```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 ``` -------------------------------- ### Running Tests in Python Fire Source: https://github.com/google/python-fire/blob/master/CONTRIBUTING.md Instructions on how to run the project's tests locally. This involves installing test dependencies and executing the pytest command. ```shell pip install pytest mock termcolor hypothesis pytest ``` -------------------------------- ### BrokenCalculator Class CLI Usage Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates invoking the BrokenCalculator class commands and passing constructor arguments. ```bash $ python example.py add 10 20 31 $ python example.py multiply 10 20 201 $ python example.py add 10 20 --offset=0 30 $ python example.py multiply 10 20 --offset=0 200 ``` -------------------------------- ### Chaining Function Calls Example Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates how to chain function calls in a Python Fire CLI. The BinaryCanvas class returns self from its methods to enable chaining. This allows for sequential operations on the object. ```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) ``` -------------------------------- ### Calculator Class CLI Usage Source: https://github.com/google/python-fire/blob/master/docs/guide.md Shows how to invoke the Calculator class commands from the command line. ```bash $ python example.py add 10 20 30 $ python example.py multiply 10 20 200 ``` -------------------------------- ### Calculator Class CLI Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates exposing class methods as commands using `fire.Fire(Calculator)`. Supports positional and named arguments for methods. ```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) ``` -------------------------------- ### Creating a CLI with Python Fire Source: https://github.com/google/python-fire/blob/master/docs/guide.md This section details the basic steps to create a Command Line Interface (CLI) using the Python Fire library. It covers importing the library and calling the Fire function to expose Python code as a CLI. ```python import fire # Turns the current module into a Fire CLI. fire.Fire() # Turns a specific component into a Fire CLI. # fire.Fire(component) ``` -------------------------------- ### Install Python Fire with Pip Source: https://github.com/google/python-fire/blob/master/docs/installation.md Installs the Python Fire library using the pip package manager. This is the most common method for installing Python packages. ```bash pip install fire ``` -------------------------------- ### Airport Property Access CLI Usage Source: https://github.com/google/python-fire/blob/master/docs/guide.md Shows how to use the Airport CLI to retrieve airport information by code. ```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 ``` -------------------------------- ### Simple Hello World Alternative CLI Usage Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates how to use the simple Python Fire CLI to retrieve predefined string values. This shows accessing 'english' and 'spanish' variables from the CLI. ```bash python example.py english ``` ```bash python example.py spanish ``` -------------------------------- ### BrokenCalculator Class CLI with Constructor Arguments Source: https://github.com/google/python-fire/blob/master/docs/guide.md Illustrates using constructor arguments (`__init__`) with `fire.Fire(BrokenCalculator)`. Constructor arguments must use the `--flag` syntax. ```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) ``` -------------------------------- ### Pipeline CLI with Grouped Commands Source: https://github.com/google/python-fire/blob/master/docs/guide.md Shows how to create a CLI with nested commands by composing classes. `fire.Fire(Pipeline)` exposes `ingestion` and `digestion` subcommands. ```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) ``` -------------------------------- ### Getting Help Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Displays usage information for a Fire CLI or a specific command. Help is also automatically shown on errors like incorrect argument counts or non-existent members. ```bash widget -- --help ``` -------------------------------- ### Install Python Fire with Conda Source: https://github.com/google/python-fire/blob/master/docs/installation.md Installs the Python Fire library using the conda package manager, specifically from the conda-forge channel. This is useful for users managing environments with conda. ```bash conda install fire -c conda-forge ``` -------------------------------- ### Separator Usage Examples (Bash) Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Illustrates the behavior of the default separator and how to change it to pass arguments that conflict with the default separator. ```bash display hello display hello upper display hello - upper display - SEP -- --separator SEP ``` -------------------------------- ### Functions with *varargs and **kwargs Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates how Google Python Fire handles functions that accept variable numbers of positional (*varargs) and keyword (**kwargs) arguments. It shows how to pass arguments and use separators. ```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) ``` ```bash python example.py dog cat elephant cat dog elephant ``` ```bash python example.py dog cat elephant - upper CAT DOG ELEPHANT ``` ```bash python example.py dog cat elephant upper cat dog upper elephant ``` ```bash python example.py dog cat elephant -- --separator=X CAT DOG ELEPHANT ``` -------------------------------- ### Getting a Fire Trace Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Requests a trace of the Fire command execution, providing step-by-step information on how the command was processed. Also displayed alongside help during errors. ```bash widget whack 5 -- --trace ``` -------------------------------- ### Python Fire fire.Fire() Arguments Source: https://github.com/google/python-fire/blob/master/docs/guide.md Details the available arguments for the `fire.Fire()` function, which controls how a Python component is exposed as a CLI. This includes specifying the component, command, CLI name, and serialization method. ```APIDOC fire.Fire(component=None, command=None, name=None, serialize=None) Arguments: component: The Python object (module, class, function, dict, etc.) to turn into a CLI. If omitted, defaults to a dictionary of all locals and globals. command: A string or list of arguments to be executed by the CLI. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If omitted, `sys.argv[1:]` (command line arguments) are used by default. name: The name of the CLI, used in help screens. If omitted, it is inferred automatically. serialize: A custom serialization function. If omitted, simple types are serialized via their built-in `str` method, and objects with a custom `__str__` method are serialized with that. If specified, all objects are serialized to text via the provided method. ``` -------------------------------- ### Exposing Multiple Commands with fire.Fire() Source: https://github.com/google/python-fire/blob/master/docs/guide.md Selectively exposes specific functions to the command line by passing them as key-value pairs in a dictionary to fire.Fire(). This allows for controlled exposure of functionality. ```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, }) ``` -------------------------------- ### Async Functions Source: https://github.com/google/python-fire/blob/master/docs/guide.md Illustrates how Google Python Fire can execute asynchronous functions (coroutines). Fire blocks until the async function completes. ```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) ``` -------------------------------- ### Exposing Multiple Commands with fire.Fire() Source: https://github.com/google/python-fire/blob/master/docs/guide.md Exposes methods of a Python object (like a class instance) to the command line by passing the object to fire.Fire(). This is a convenient way to group related 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) ``` -------------------------------- ### Using Fire Flags Source: https://github.com/google/python-fire/blob/master/docs/guide.md Demonstrates how to use flags with Python Fire CLIs. Flags are arguments that modify the behavior of the CLI and are typically separated from the main command by an isolated '--'. ```bash # Enter interactive mode python example.py -- --interactive # Show help and usage information python example.py -- --help # or python example.py --help # or python example.py -h # Set a custom separator python example.py -- --separator=X # Generate a completion script for a shell python example.py -- --completion [shell] # Get a Fire trace for the command python example.py -- --trace # Include private members in the output python example.py -- --verbose ``` -------------------------------- ### Exposing Multiple Commands with fire.Fire() Source: https://github.com/google/python-fire/blob/master/docs/guide.md Exposes multiple functions (e.g., 'add', 'multiply') from a Python script to the command line by calling fire.Fire() without arguments. Fire automatically handles function discovery. ```python import fire def add(x, y): return x + y def multiply(x, y): return x * y if __name__ == '__main__': fire.Fire() ``` -------------------------------- ### Boolean Arguments and Flags Source: https://github.com/google/python-fire/blob/master/docs/guide.md Explains how Google Python Fire handles boolean values and flags. It covers direct boolean assignments, `--flag` for True, and `--noflag` for False. It also addresses potential conflicts between boolean flags and subsequent arguments. ```bash python example.py --obj=True bool ``` ```bash python example.py --obj=False bool ``` ```bash python example.py --obj bool ``` ```bash python example.py --noobj bool ``` -------------------------------- ### Fire Without Code Changes Source: https://github.com/google/python-fire/blob/master/docs/guide.md Shows how to use Python Fire to create a CLI for a Python file without modifying the file itself. Fire can be invoked using the '-m' flag with the module or file path. ```bash python -m fire example hello --name=World ``` ```bash python -m fire example.py hello --name=World ``` -------------------------------- ### Separator Usage Example Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Demonstrates how to use and change the separator argument for Fire CLIs. The default separator is '-'. Changing the separator allows passing the default separator character as an argument. ```python def display(arg1, arg2='!'): return arg1 + arg2 ``` -------------------------------- ### Argument Type Parsing Source: https://github.com/google/python-fire/blob/master/docs/guide.md Shows how Google Python Fire automatically determines argument types from their values, supporting various Python literals like numbers, strings, tuples, lists, dictionaries, and booleans. It also highlights the importance of proper quoting for strings and nested collections. ```python import fire fire.Fire(lambda obj: type(obj).__name__) ``` ```bash python example.py 10 int ``` ```bash python example.py 10.0 float ``` ```bash python example.py hello str ``` ```bash python example.py '(1,2)' tuple ``` ```bash python example.py [1,2] list ``` ```bash python example.py True bool ``` ```bash python example.py {name:David} dict ``` ```bash python example.py 10 int ``` ```bash python example.py "10" int ``` ```bash python example.py '"10"' str ``` ```bash python example.py "'10'" str ``` ```bash python example.py \"10\" str ``` ```bash python example.py '{"name": "David Bieber"}' dict ``` ```bash python example.py {"name":'"David Bieber"'} dict ``` ```bash python example.py {"name":"David Bieber"} str ``` ```bash python example.py '{"name": "Justin Bieber"}' dict ``` -------------------------------- ### Instantiating a Class with Arguments Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Shows how to instantiate a Python class from the command line using Python Fire, passing arguments to the class's __init__ method using flag syntax. ```python class Greeter: def __init__(self, name='World'): self.name = name def greet(self): print(f'Hello, {self.name}!') # Command line usage: # python your_script.py Greeter --name=Fire # python your_script.py Greeter --greet ``` -------------------------------- ### Using a Fire CLI Source: https://github.com/google/python-fire/blob/master/docs/api.md Demonstrates common flags and arguments used when interacting with a Fire-generated CLI. Flags are typically separated by `--`. ```bash # Show the help screen command --help # Enter interactive mode command -- --interactive # Set a custom separator command -- --separator=X # Generate completion script for a shell command -- --completion [shell] # Get a Fire trace for the command command -- --trace # Enable verbose output command -- --verbose ``` -------------------------------- ### Create a CLI from a Component Source: https://github.com/google/python-fire/blob/master/docs/api.md Turns a specific Python component (like a class or function) into a Fire CLI. This is useful for exposing only a part of your codebase. ```python import fire class MyComponent: def greet(self, name): return f'Hello, {name}!' fire.Fire(MyComponent) ``` -------------------------------- ### Create a CLI from a Module Source: https://github.com/google/python-fire/blob/master/docs/api.md Turns the current Python module into a Fire CLI. This allows you to expose your module's functions and classes as command-line arguments. ```python import fire fire.Fire() ``` -------------------------------- ### Basic Function CLI Source: https://github.com/google/python-fire/blob/master/docs/index.md Demonstrates how to create a command-line interface (CLI) from a Python function using the `fire` library. The CLI allows users to pass arguments to the function and view help information. ```python import fire def hello(name="World"): return "Hello %s!" % name if __name__ == '__main__': fire.Fire(hello) ``` ```bash python hello.py python hello.py --name=David python hello.py --help ``` -------------------------------- ### Using Python Fire from the Command Line Source: https://github.com/google/python-fire/blob/master/docs/api.md Executes a Python module as a Fire CLI without modifying its code. This is useful for exploring or interacting with existing Python modules directly from the terminal. ```bash # Execute the 'calendar' module as a CLI and show its help python -m fire calendar -h # Execute a specific Python file as a CLI python -m fire path/to/your_module.py -- --help ``` -------------------------------- ### Basic Function CLI Source: https://github.com/google/python-fire/blob/master/README.md Demonstrates how to create a command-line interface from a Python function using Python Fire. The CLI can accept arguments to customize its behavior. ```python import fire def hello(name="World"): return "Hello %s!" % name if __name__ == '__main__': fire.Fire(hello) ``` -------------------------------- ### Basic Class CLI Source: https://github.com/google/python-fire/blob/master/docs/index.md Illustrates how to generate a CLI from a Python class using the `fire` library. This allows users to access and execute methods of the class from the command line. ```python import fire class Calculator(object): """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator) ``` ```bash python calculator.py double 10 python calculator.py double --number=15 ``` -------------------------------- ### Linting Python Fire Code Source: https://github.com/google/python-fire/blob/master/CONTRIBUTING.md Command to run the linter on the Python Fire codebase. This helps ensure code quality and adherence to style guidelines. ```shell pylint fire ``` -------------------------------- ### Python Fire CLI Commands Source: https://github.com/google/python-fire/blob/master/docs/index.md Provides a reference for common commands and flags used with Python Fire CLIs. This includes help, REPL, separator, completion, trace, and verbose options. ```APIDOC fire.Fire() Turns the current module into a Fire CLI. fire.Fire(component) Turns `component` into a Fire CLI. Using a CLI: Help: Command: `command --help` or `command -- --help` Notes: Shows usage information. REPL: Command: `command -- --interactive` Notes: Enters interactive mode. Separator: Command: `command -- --separator=X` Notes: Sets the separator to `X`. The default separator is `-`. Completion: Command: `command -- --completion [shell]` Notes: Generates a completion script for the CLI. Trace: Command: `command -- --trace` Notes: Gets a Fire trace for the command. Verbose: Command: `command -- --verbose` Notes: Enables verbose output. ``` -------------------------------- ### fire.Fire() Arguments Source: https://github.com/google/python-fire/blob/master/docs/api.md Details the arguments that can be passed to the `fire.Fire()` function to customize CLI behavior, including component selection, command overriding, CLI naming, and serialization. ```APIDOC fire.Fire(component=None, command=None, name=None, serialize=None) Arguments: component: The Python object (class, function, dict, etc.) to expose as a CLI. If omitted, defaults to a dictionary of all locals and globals. command: A string or list of arguments to simulate command-line input. If a string, it's split into arguments. If omitted, `sys.argv[1:]` is used. name: The name of the CLI, used in help screens. If omitted, it's inferred automatically. serialize: A custom serialization function. If omitted, objects are serialized using their `__str__` method. ``` -------------------------------- ### Basic Class CLI Source: https://github.com/google/python-fire/blob/master/README.md Illustrates how to generate a CLI from a Python class using Python Fire. Methods of the class can be invoked via the command line. ```python import fire class Calculator(object): """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator) ``` -------------------------------- ### Explore Code in a Python REPL Source: https://github.com/google/python-fire/blob/master/docs/benefits.md Using the `--interactive` flag with Fire launches an IPython REPL pre-populated with your project's variables and modules. This eliminates the need for manual imports and variable definitions, speeding up code exploration. ```python import fire my_variable = 10 def multiply(x): return x * my_variable if __name__ == '__main__': fire.Fire(multiply, '--interactive') ``` -------------------------------- ### Calling a Function with Named Arguments Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Demonstrates how to call a Python function from the command line using named arguments with Python Fire. This allows for explicit parameter passing, improving clarity. ```python def double(value=0): return 2 * value # Command line usage: # python your_script.py double --value 5 ``` -------------------------------- ### Passing Flags to Fire CLI Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Demonstrates how to pass flags to a Python Fire CLI, which are distinguished from regular arguments by a standalone '--'. Flags are typically used for configuration or global settings. ```python # Example command: # python your_script.py -- --alsologtostderr ``` -------------------------------- ### Interactive Mode Usage Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Enters interactive mode, launching an IPython REPL with the specified variable pre-defined. Requires the 'ipython' package for IPython REPL. ```bash widget -- --interactive widget -- -i ``` -------------------------------- ### Transition Between Bash and Python Source: https://github.com/google/python-fire/blob/master/docs/benefits.md Fire enables direct execution of Python functions from Bash, allowing seamless integration with familiar Unix tools like `grep` and `wc`. It also makes it easy to write one-off scripts in Python that might otherwise be written in Bash. ```bash python your_script.py --arg1 value1 --arg2 value2 | grep 'pattern' ``` ```python import fire def process_data(input_file): with open(input_file, 'r') as f: data = f.read() return data.upper() if __name__ == '__main__': fire.Fire(process_data) ``` -------------------------------- ### Calling a Function with Positional Arguments Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Illustrates calling a Python function using positional arguments via the Python Fire CLI. This method is suitable when the order of arguments is known and consistent. ```python def double(value=0): return 2 * value # Command line usage: # python your_script.py double 5 ``` -------------------------------- ### Completion Script Generation Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Generates a shell completion script for the Fire CLI. The script can be saved to a file and sourced for shell integration. Supports different shells like bash and fish. ```bash widget -- --completion > ~/.widget-completion widget -- --completion fish ``` -------------------------------- ### Accessing List/Tuple Elements by Index Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Illustrates accessing an element from a list or tuple returned by a function using its index as a command-line argument with Python Fire. ```python def get_list(): return [10, 20, 30] # Command line usage: # python your_script.py get_list 2 ``` -------------------------------- ### Accessing Object Methods Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Demonstrates accessing a method of a Python object from the command line using Python Fire. The method name is used directly as a subcommand. ```python class MyClass: def whack(self): print('Whacked!') # Command line usage: # python your_script.py MyClass --whack ``` -------------------------------- ### Accessing Dictionary Values by Key Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Explains how to retrieve a value from a dictionary returned by a function using its key as a command-line argument with Python Fire. ```python def get_dict(): return {'key': 'value'} # Command line usage: # python your_script.py get_dict --key ``` -------------------------------- ### Explore Existing Code as a CLI Source: https://github.com/google/python-fire/blob/master/docs/benefits.md You can use Fire to turn existing Python modules, even those without accessible source code, into CLIs. This allows for easy exploration of their functionality and creation of powerful tools, similar to using libraries like difflib or PIL. ```python import fire import difflib if __name__ == '__main__': fire.Fire(difflib) ``` -------------------------------- ### Python Fire CLI Flags Source: https://github.com/google/python-fire/blob/master/README.md Details common flags used with Python Fire CLIs for controlling behavior such as help, interactive mode, argument separation, completion, tracing, and verbosity. ```APIDOC Using a CLI | Command | Notes :--------------| :-------------------------------------- | :--------- [Help] | `command --help` or `command -- --help` | [REPL] | `command -- --interactive` | Enters interactive mode. [Separator] | `command -- --separator=X` | Sets the separator to `X`. The default separator is `-`. [Completion] | `command -- --completion [shell]` | Generates a completion script for the CLI. [Trace] | `command -- --trace` | Gets a Fire trace for the command. [Verbose] | `command -- --verbose` | ``` -------------------------------- ### Create CLIs in Python Source: https://github.com/google/python-fire/blob/master/docs/benefits.md Python Fire allows you to create command-line interfaces (CLIs) by simply exposing your Python functions, modules, or classes. A single line of code integrating Fire makes your CLI ready for use. ```python import fire def hello(name): return f'Hello {name}!' if __name__ == '__main__': fire.Fire(hello) ``` -------------------------------- ### Develop and Debug Python Code with Fire Source: https://github.com/google/python-fire/blob/master/docs/benefits.md Fire helps in developing and debugging Python code by allowing you to quickly test functionality without modifying a main method. The `--interactive` flag enables an IPython REPL with pre-loaded imports and variables, streamlining the debugging process. ```python import fire def add(a, b): return a + b if __name__ == '__main__': fire.Fire(add, '--interactive') ``` -------------------------------- ### Accessing Object Properties Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Shows how to access a property of a Python object exposed through a Fire CLI. The property name is converted from snake_case to kebab-case for command-line usage. ```python class MyClass: def __init__(self): self.high_score = 100 # Command line usage: # python your_script.py MyClass --high-score ``` -------------------------------- ### Verbose Mode Source: https://github.com/google/python-fire/blob/master/docs/using-cli.md Enables verbose mode by adding the -v or --verbose flag. This can reveal private members in the usage string, primarily for debugging purposes. ```bash widget -- --verbose widget -- -v ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.