cmd-manager


Namecmd-manager JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/mhsiddiqui/cmd-manager
SummaryA management command system for FastAPI and Flask inspired by Django management commands.
upload_time2024-10-18 06:50:05
maintainerNone
docs_urlNone
authorMuhammad Hassan Siddiqui
requires_pythonNone
licenseMIT
keywords fastapi flask starlette management-commands
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Build](https://github.com/mhsiddiqui/cmd-manager/actions/workflows/build.yml/badge.svg)](https://github.com/mhsiddiqui/cmd-manager/actions/workflows/build.yml)
# 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`:

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

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

```python
# 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:

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

```python
management_system.register(package='external_package.scripts')
```

To avoid command name conflicts between multiple packages, you can apply a prefix:

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

```bash
python example_runner.py whats_my_name
```

## Authors
[@mhsiddiqui](https://github.com/mhsiddiqui)

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mhsiddiqui/cmd-manager",
    "name": "cmd-manager",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "fastapi, flask, starlette, management-commands",
    "author": "Muhammad Hassan Siddiqui",
    "author_email": "mhassan.eeng@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/1c/83/484fa39279fa3abfcc565454ffba0f714776dfa291def93cd494e7768b83/cmd_manager-0.1.0.tar.gz",
    "platform": null,
    "description": "[![Build](https://github.com/mhsiddiqui/cmd-manager/actions/workflows/build.yml/badge.svg)](https://github.com/mhsiddiqui/cmd-manager/actions/workflows/build.yml)\n# Cli Manager\n\nA 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.\n\n## Features\n\n- **Dynamic Command Registration:** Automatically discover and register commands located in specific directories.\n- **Class-Based Commands:** Easily define reusable commands by subclassing `BaseCommand`.\n- **Custom Arguments:** Commands can specify their own arguments and options, which will be automatically handled by the command-line interface.\n- **Pluggable and Extendable:** Easily integrate this package with any FastAPI app or third-party package.\n\n## Installation\n\nInstall the package via `pip`:\n\n```bash\npip install cli-manager\n```\n\n## Usage\n\n### 1. Define Your Command\n\nTo 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.\n\n```python\n# src/scripts/mycommand.py\n\nfrom cmd_manager import BaseCommand, Argument\n\nclass Command(BaseCommand):\n    def get_arguments(self):\n        return [\n            Argument('arg1', is_argument=True),\n            Argument('--n', is_argument=False, type=int),\n        ]\n\n    def run(self, *args, **kwargs):\n        print(f\"Running command with args: {args}, kwargs: {kwargs}\")\n```\n\nTo 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.\n\n\n### 2. Register Commands\n\nIn 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.\n\n```python\n# cli_runner.py\n\nfrom cmd_manager import ManagementCommandSystem\n\n# Initialize the management command system\nmanagement_system = ManagementCommandSystem()\n\n# Register all commands in the 'src.scripts' package\nmanagement_system.register(package='src.scripts')\n\n# Create the Click CLI group\ncli = management_system.create_cli()\n\nif __name__ == '__main__':\n    cli()\n```\n\nThis 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.\n\n### 3. Run Commands\n\nOnce your commands are registered, you can run them using the CLI:\n\n```bash\npython cli_runner.py mycommand arg1_value --arg2 123\n```\n\nIn this case, `mycommand` is the command name, and `arg1_value` and `--arg2 123` are the arguments passed to the command.\n\n### 4. Using Management Commands from External Packages\n\nIf 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.\n\n```python\nmanagement_system.register(package='external_package.scripts')\n```\n\nTo avoid command name conflicts between multiple packages, you can apply a prefix:\n\n```python\nmanagement_system.register(prefix='ext-', package='external_package.scripts')\n```\n\nThis way, all commands from `external_package` will be prefixed with `ext-`, avoiding any conflicts with similarly named commands in your project.\n\nHere\u2019s another example where you define a simple `greet` command:\n\n### Example\n\nExample can be seen in example folder. This example can be run by running following command\n\n```bash\npython example_runner.py whats_my_name\n```\n\n## Authors\n[@mhsiddiqui](https://github.com/mhsiddiqui)\n\n## Contributing\nContributions are always welcome!\n\nPlease read contributing.md to get familiar how to get started.\n\nPlease adhere to the project's code of conduct.\n\nFeedback And Support\nPlease open an issue and follow the template, so the community can help you.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A management command system for FastAPI and Flask inspired by Django management commands.",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/mhsiddiqui/cmd-manager",
        "Repository": "https://github.com/mhsiddiqui/cmd-manager"
    },
    "split_keywords": [
        "fastapi",
        " flask",
        " starlette",
        " management-commands"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "80dc9e41e536ea0e58b4f6061cd1cdd4e142c20a7f9b5a50c670281eb4db0971",
                "md5": "f8cf1edc5dd3f9441a41020c03024e8c",
                "sha256": "9e086081f51ca0b5dc71c0f8ed2b7792744f980b13b77447779fcaf41038a036"
            },
            "downloads": -1,
            "filename": "cmd_manager-0.1.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f8cf1edc5dd3f9441a41020c03024e8c",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 5730,
            "upload_time": "2024-10-18T06:50:03",
            "upload_time_iso_8601": "2024-10-18T06:50:03.676927Z",
            "url": "https://files.pythonhosted.org/packages/80/dc/9e41e536ea0e58b4f6061cd1cdd4e142c20a7f9b5a50c670281eb4db0971/cmd_manager-0.1.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1c83484fa39279fa3abfcc565454ffba0f714776dfa291def93cd494e7768b83",
                "md5": "ef3956ff27795ede05438675d57aebf8",
                "sha256": "86dce57a7794047fbc725d2d0dbeaa73cfec3a64e7302063fe12a8ca17e50d6a"
            },
            "downloads": -1,
            "filename": "cmd_manager-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ef3956ff27795ede05438675d57aebf8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4683,
            "upload_time": "2024-10-18T06:50:05",
            "upload_time_iso_8601": "2024-10-18T06:50:05.404837Z",
            "url": "https://files.pythonhosted.org/packages/1c/83/484fa39279fa3abfcc565454ffba0f714776dfa291def93cd494e7768b83/cmd_manager-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-18 06:50:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mhsiddiqui",
    "github_project": "cmd-manager",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "cmd-manager"
}
        
Elapsed time: 0.32910s