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.
Metadata
Release files for fly-cli 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fly-cli-0.1.1.tar.gz | 7.1 kB | Without provenance | Details for fly-cli-0.1.1.tar.gz |
Release files / fly-cli-0.1.1.tar.gz
| Download URL | fly-cli-0.1.1.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2d0a1b6b6f495f4a191f085e158614ec31c5940bcd59b4b5f926a1cfa061be7b
|
|
BLAKE2b-256 checksum How to use checksums |
220604f7bdd4434c0e0a0c0e7456374e0d5099b7b4a43ceae932f5cbd0b808c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.8
|
Security for fly-cli 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.