A collection of classes that help parse commands.
Project description
Pycommandparse
Structure
In pycommandparse, each function is bound to a pycommandparse.Command instance.
The Parser takes these as inputs.
Internally, the parser has a dictionary with keys having all names and aliases with the values being the corresponding Command instances.
The Parser matches the imcoming commands to the given names/aliases and runs the command with the arguments.
Examples
Defining and Adding Commands
Method 1: Directly defining the command.
from pycommandparse.parsers import BaseParser
parser = BaseParser()
# Ways to define commands
## 1. directly via pycommandparse.Command
from pycommandparse import Command
def mult(x, y):
return int(x)*int(y)
multiplication = Command(name="multiply",
command=mult,
usage="multiply *args", description="Multiplies 2 numbers you input",
number_of_args=2,
aliases=['product'])
parser.add_command(multiplication)
parser.parse_run("multiply 3 5") # Outputs "15"
Method 2: Decorators.
from pycommandparse.parsers import BaseParser
## 2. Using decorator
### Not specifying number_of_args will allow any number of arguments.
@parser.command(name="add",
aliases=['sum'],
usage="add *numbers",
description="Adds what you input.")
def add(*numbers):
return sum([int(number) for number in numbers])
parser.parse_run("add 3 5 6 7") # Outputs "21"
parser.parse_run("add 2 4") # Outputs "6"
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycommandparse-0.2.0.tar.gz.
File metadata
- Download URL: pycommandparse-0.2.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.6 Linux/5.10.43.3-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb662726aa024025e895930f97d34350714188eadf459c9df66600659e07a33e
|
|
| MD5 |
735289565a2ecb4a8965df23bd8f0082
|
|
| BLAKE2b-256 |
05c13051f68df1fd3f336305675a258a193a4221f01395fc18e789f6a7b20bfc
|
File details
Details for the file pycommandparse-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pycommandparse-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.6 Linux/5.10.43.3-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbe8c8f628052bd8c09ef544bf7de58abe32dcded89b1c6c906bd2c370a7e586
|
|
| MD5 |
113184cafd85e699f5731303f198ba1a
|
|
| BLAKE2b-256 |
5d1d339b3b1aa2bfc5eec9b09500e62ce6d5e7fe270034cb9b2f766e494c9363
|