Invoke class methods from the command-line.
Project description
Invoke object methods from the command-line. WIP.
install
Add this package to your dependencies in requirements.txt or use pip install:
pip install fly-cli
usage
Let’s say you have a class Greeter in module app (app.py) that you want to create a CLI for:
class Greeter: def greet(name, greeting='hi there'): """ Greet someone by name. :param name: The name of the person to be greeted. :param greeting: The greeting message to use. """ print(f'{greeting}, {name}')
Run this command to generate a stub:
fly stub app:Greeter greeter
This will create a binary called greeter with the following contents:
#!/usr/bin/env python from fly_cli import FlyCLI from app import Greeter def main(): fly = FlyCLI() fly(Greeter()) if __name__ == '__main__': main()
The FlyCLI class inspects the object and generates a sub-command for each method, analysing the method parameters to add arguments to the parser.
Now you can run the script to call an instance of the class:
> ./greeter greet --greeting="hola" bob hola, bob
Edit the script to add constructor arguments or configuration if required.
Help text is generated from method documentation, parsing Sphinx parameter declarations, type declarations and default values to generate argument help.
Listing subcommands:
./greeter --help usage: greeter [-h] command ... positional arguments: command greet Greet someone by name. optional arguments: -h, --help show this help message and exit
Subcommand help:
> ./greeter greet --help usage: greeter greet [-h] [--greeting GREETING] name positional arguments: name The name of the person to be greeted. optional arguments: -h, --help show this help message and exit --greeting GREETING The greeting message to use. Default is "hi there".
To add more subcommands, add more methods on the class.
If you add type annotations to your parameters, it will coerce the values into the specified type when parsing arguments.
Boolean parameters become string arguments accepting various forms of truthiness: on, off, yes, no, 1, 0, true, false. This is converted to a boolean before the method is called.
fly-cli eats its own dogfood, the console script fly wraps the FlyCLI class.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file fly-cli-0.1.1.tar.gz
.
File metadata
- Download URL: fly-cli-0.1.1.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d0a1b6b6f495f4a191f085e158614ec31c5940bcd59b4b5f926a1cfa061be7b |
|
MD5 | e1069604e1954349001227ca533ad0cf |
|
BLAKE2b-256 | 220604f7bdd4434c0e0a0c0e7456374e0d5099b7b4a43ceae932f5cbd0b808c4 |