Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycommandparse-0.2.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycommandparse-0.2.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

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

Hashes for pycommandparse-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cb662726aa024025e895930f97d34350714188eadf459c9df66600659e07a33e
MD5 735289565a2ecb4a8965df23bd8f0082
BLAKE2b-256 05c13051f68df1fd3f336305675a258a193a4221f01395fc18e789f6a7b20bfc

See more details on using hashes here.

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

Hashes for pycommandparse-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dbe8c8f628052bd8c09ef544bf7de58abe32dcded89b1c6c906bd2c370a7e586
MD5 113184cafd85e699f5731303f198ba1a
BLAKE2b-256 5d1d339b3b1aa2bfc5eec9b09500e62ce6d5e7fe270034cb9b2f766e494c9363

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page