A management command system for FastAPI and Flask inspired by Django management commands.
Project description
Cli Manager
A Python package that enables you to create and manage custom management commands, similar to Django's management system for FastAPI, Flask and other similar frameworks. This package uses Python's click
to define, register, and execute commands for your application dynamically.
Features
- Dynamic Command Registration: Automatically discover and register commands located in specific directories.
- Class-Based Commands: Easily define reusable commands by subclassing
BaseCommand
. - Custom Arguments: Commands can specify their own arguments and options, which will be automatically handled by the command-line interface.
- Pluggable and Extendable: Easily integrate this package with any FastAPI app or third-party package.
Installation
Install the package via pip
:
pip install cli-manager
Usage
1. Define Your Command
To create a custom command, define a Python script in your project and subclass BaseCommand
. Implement the run
method to include your logic, and use get_arguments
to specify any arguments the command will accept.
# src/scripts/mycommand.py
from cmd_manager import BaseCommand, Argument
class Command(BaseCommand):
def get_arguments(self):
return [
Argument('arg1', is_argument=True),
Argument('--n', is_argument=False, type=int),
]
def run(self, *args, **kwargs):
print(f"Running command with args: {args}, kwargs: {kwargs}")
To Argument class accept all the parameters which click.Argument
and click.Option
accept. By using is_argument=True/False
, both type of argument can be differentiated.
2. Register Commands
In your main CLI runner file, use the ManagementCommandSystem
to register and organize all your commands dynamically. This method discovers all commands within a specified package (like src.scripts
) and registers them.
# cli_runner.py
from cmd_manager import ManagementCommandSystem
# Initialize the management command system
management_system = ManagementCommandSystem()
# Register all commands in the 'src.scripts' package
management_system.register(package='src.scripts')
# Create the Click CLI group
cli = management_system.create_cli()
if __name__ == '__main__':
cli()
This code sets up the command system and links the command logic to a FastAPI instance. All commands from the specified package (src.scripts
) will automatically become available as CLI commands.
3. Run Commands
Once your commands are registered, you can run them using the CLI:
python cli_runner.py mycommand arg1_value --arg2 123
In this case, mycommand
is the command name, and arg1_value
and --arg2 123
are the arguments passed to the command.
4. Using Management Commands from External Packages
If you have installed another FastAPI package with its own set of management commands, you can also register those commands in your CLI by specifying the package name.
management_system.register(package='external_package.scripts')
To avoid command name conflicts between multiple packages, you can apply a prefix:
management_system.register(prefix='ext-', package='external_package.scripts')
This way, all commands from external_package
will be prefixed with ext-
, avoiding any conflicts with similarly named commands in your project.
Here’s another example where you define a simple greet
command:
Example
Example can be seen in example folder. This example can be run by running following command
python example_runner.py whats_my_name
Authors
Contributing
Contributions are always welcome!
Please read contributing.md to get familiar how to get started.
Please adhere to the project's code of conduct.
Feedback And Support Please open an issue and follow the template, so the community can help you.
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
Built Distribution
File details
Details for the file cmd_manager-0.1.0.tar.gz
.
File metadata
- Download URL: cmd_manager-0.1.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.18 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86dce57a7794047fbc725d2d0dbeaa73cfec3a64e7302063fe12a8ca17e50d6a |
|
MD5 | ef3956ff27795ede05438675d57aebf8 |
|
BLAKE2b-256 | 1c83484fa39279fa3abfcc565454ffba0f714776dfa291def93cd494e7768b83 |
File details
Details for the file cmd_manager-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: cmd_manager-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.18 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e086081f51ca0b5dc71c0f8ed2b7792744f980b13b77447779fcaf41038a036 |
|
MD5 | f8cf1edc5dd3f9441a41020c03024e8c |
|
BLAKE2b-256 | 80dc9e41e536ea0e58b4f6061cd1cdd4e142c20a7f9b5a50c670281eb4db0971 |