a module for parsing and processing commands.
Project description
cmdtools
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 pypi:
-
from source:
python setup.py install
Examples
Basic example
import cmdtools
def ping():
print("pong.")
cmd = cmdtools.Cmd('/ping')
cmd.process_cmd(ping)
Parse command with arguments
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
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
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
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
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
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
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
cmdtools-py-2.1.0.tar.gz
(5.5 kB
view hashes)
Built Distribution
Close
Hashes for cmdtools_py-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bc0f871427d6136d4ce4cb1101dc7ec3b278e591fc0a6e0ae161afe4b3e8958 |
|
MD5 | 8dd1350d4b340c14863fe46161af262f |
|
BLAKE2b-256 | 07b409921cd5d8341b1665ba2e96c968004eeb9d67812456a9e03077bfc6c1b3 |