cmdtools-py


Namecmdtools-py JSON
Version 1.7.0 PyPI version JSON
download
home_pagehttps://github.com/HugeBrain16/cmdtools
Summarya module for parsing and processing commands.
upload_time2021-06-07 17:18:26
maintainer
docs_urlNone
authorHugeBrain16
requires_python
licenseMIT
keywords command-parser command-processor command cmd cmd-parser
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # cmdtools
[![tests](https://github.com/HugeBrain16/cmdtools/actions/workflows/python-package.yml/badge.svg)](https://github.com/HugeBrain16/cmdtools/actions/workflows/python-package.yml)

a module for parsing and processing commands.

## Installation
to install this module you can use the methods below 

- using pip: 
    + from pypi: `pip install cmdtools-py`  
    + from github repository: `pip install git+https://github.com/HugeBrain16/cmdtools.git`  

- from source: `python setup.py install`  

## Examples
Basic example
```py
import cmdtools

def ping():
    print("pong.")

cmd = cmdtools.Cmd('/ping')

cmd.process_cmd(ping)
```

Parse command with arguments
```py
import cmdtools

def greet(name):
    print(f"Hello, {name}, nice to meet you")

cmd = cmdtools.Cmd('/greet "Josh"')

cmd.process_cmd(greet)
```

Parsing command with more than one argument and different data types
```py
import cmdtools

def give(name, item_name, item_amount):
    print(f"You gave {item_amount} {item_name}s to {name}")

cmd = cmdtools.Cmd('/give "Josh" "Apple" 10', convert_args=True) # convert command arguments into specific datatypes

# check command
if cmd.match_args('ssi', max_args=3): # format indicates ['str','str','int'], only match 3 arguments
    cmd.process_cmd(give)
else:
    print('Correct Usage: /give <name: [str]> <item-name: [str]> <item-amount: [int]>')
```

command with attributes
```py
import cmdtools

def test():
    print(test.text)

cmd = cmdtools.Cmd('/test')

cmd.process_cmd(test,
    attrs={ # assign attributes to the callback
        'text': "Hello World"
    }
)
```

command with error handling example

using callback

```py
import cmdtools

def error_add(error):
    if isinstance(error, cmdtools.MissingRequiredArgument):
        if error.param == 'num1':
            print('you need to specify the first number')
        if error.param == 'num2':
            print('you need to specify the second number')

def add(num1, num2):
    print(num1 + num2)

cmd = cmdtools.Cmd('/add', convert_args=True)
cmd.process_cmd(add, error_add)
```

or using python error handler

```py
import cmdtools

def add(num1, num2):
    print(num1 + num2)

cmd = cmdtools.Cmd('/add')

try:
    cmd.process_cmd(add)
except Exception as error:
    if isinstance(error.exception, cmdtools.MissingRequiredArgument):
        if error.exception.param == "num1":
            print('you need to specify the first number')
        if error.exception.param == "num2":
            print('you need to specify the second number')
```

asynchronous support
```py
import cmdtools
import asyncio

async def _say(text):
    print(text)

async def main():
    cmd = cmdtools.Cmd('/say "Hello World"')

    await cmd.aio_process_cmd(_say)

asyncio.run(main())
```

## Exceptions
- ParsingError
- MissingRequiredArgument
- ProcessError


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/HugeBrain16/cmdtools",
    "name": "cmdtools-py",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "command-parser command-processor command cmd cmd-parser",
    "author": "HugeBrain16",
    "author_email": "joshtuck373@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/58/d4/b84bfa36a9b2d7f339bf22f79dca3e5f129790a4b2666e89ec223edd92cc/cmdtools-py-1.7.0.tar.gz",
    "platform": "",
    "description": "# cmdtools\n[![tests](https://github.com/HugeBrain16/cmdtools/actions/workflows/python-package.yml/badge.svg)](https://github.com/HugeBrain16/cmdtools/actions/workflows/python-package.yml)\n\na module for parsing and processing commands.\n\n## Installation\nto install this module you can use the methods below \n\n- using pip: \n    + from pypi: `pip install cmdtools-py`  \n    + from github repository: `pip install git+https://github.com/HugeBrain16/cmdtools.git`  \n\n- from source: `python setup.py install`  \n\n## Examples\nBasic example\n```py\nimport cmdtools\n\ndef ping():\n    print(\"pong.\")\n\ncmd = cmdtools.Cmd('/ping')\n\ncmd.process_cmd(ping)\n```\n\nParse command with arguments\n```py\nimport cmdtools\n\ndef greet(name):\n    print(f\"Hello, {name}, nice to meet you\")\n\ncmd = cmdtools.Cmd('/greet \"Josh\"')\n\ncmd.process_cmd(greet)\n```\n\nParsing command with more than one argument and different data types\n```py\nimport cmdtools\n\ndef give(name, item_name, item_amount):\n    print(f\"You gave {item_amount} {item_name}s to {name}\")\n\ncmd = cmdtools.Cmd('/give \"Josh\" \"Apple\" 10', convert_args=True) # convert command arguments into specific datatypes\n\n# check command\nif cmd.match_args('ssi', max_args=3): # format indicates ['str','str','int'], only match 3 arguments\n    cmd.process_cmd(give)\nelse:\n    print('Correct Usage: /give <name: [str]> <item-name: [str]> <item-amount: [int]>')\n```\n\ncommand with attributes\n```py\nimport cmdtools\n\ndef test():\n    print(test.text)\n\ncmd = cmdtools.Cmd('/test')\n\ncmd.process_cmd(test,\n    attrs={ # assign attributes to the callback\n        'text': \"Hello World\"\n    }\n)\n```\n\ncommand with error handling example\n\nusing callback\n\n```py\nimport cmdtools\n\ndef error_add(error):\n    if isinstance(error, cmdtools.MissingRequiredArgument):\n        if error.param == 'num1':\n            print('you need to specify the first number')\n        if error.param == 'num2':\n            print('you need to specify the second number')\n\ndef add(num1, num2):\n    print(num1 + num2)\n\ncmd = cmdtools.Cmd('/add', convert_args=True)\ncmd.process_cmd(add, error_add)\n```\n\nor using python error handler\n\n```py\nimport cmdtools\n\ndef add(num1, num2):\n    print(num1 + num2)\n\ncmd = cmdtools.Cmd('/add')\n\ntry:\n    cmd.process_cmd(add)\nexcept Exception as error:\n    if isinstance(error.exception, cmdtools.MissingRequiredArgument):\n        if error.exception.param == \"num1\":\n            print('you need to specify the first number')\n        if error.exception.param == \"num2\":\n            print('you need to specify the second number')\n```\n\nasynchronous support\n```py\nimport cmdtools\nimport asyncio\n\nasync def _say(text):\n    print(text)\n\nasync def main():\n    cmd = cmdtools.Cmd('/say \"Hello World\"')\n\n    await cmd.aio_process_cmd(_say)\n\nasyncio.run(main())\n```\n\n## Exceptions\n- ParsingError\n- MissingRequiredArgument\n- ProcessError\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "a module for parsing and processing commands.",
    "version": "1.7.0",
    "split_keywords": [
        "command-parser",
        "command-processor",
        "command",
        "cmd",
        "cmd-parser"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "e0663ac85566c80d27f0e2f7999b32ca",
                "sha256": "0685957bd198bcbea20cd1681b1c0875389f3c672bd7b5b00efe85ff6e9402e2"
            },
            "downloads": -1,
            "filename": "cmdtools_py-1.7.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e0663ac85566c80d27f0e2f7999b32ca",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5426,
            "upload_time": "2021-06-07T17:18:24",
            "upload_time_iso_8601": "2021-06-07T17:18:24.517813Z",
            "url": "https://files.pythonhosted.org/packages/0b/53/8b91ba2a7dbd0591c60caaa9041b799b7cc66e46548ecdc1d9d53e9f53da/cmdtools_py-1.7.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "22dcd45be97542d2d335dcf217860458",
                "sha256": "da75d6d059db7bbd7307ed3c7b1cc71af656157616d9dcc872f5e141427cc023"
            },
            "downloads": -1,
            "filename": "cmdtools-py-1.7.0.tar.gz",
            "has_sig": false,
            "md5_digest": "22dcd45be97542d2d335dcf217860458",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5442,
            "upload_time": "2021-06-07T17:18:26",
            "upload_time_iso_8601": "2021-06-07T17:18:26.706434Z",
            "url": "https://files.pythonhosted.org/packages/58/d4/b84bfa36a9b2d7f339bf22f79dca3e5f129790a4b2666e89ec223edd92cc/cmdtools-py-1.7.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-06-07 17:18:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": null,
    "github_project": "HugeBrain16",
    "error": "Could not fetch GitHub repository",
    "lcname": "cmdtools-py"
}
        
Elapsed time: 0.37104s