Skip to main content

Python static command parser. Like argparse, but without connection to CLI.

Project description

Statikomand

Python static command parser.

Like argparse, but without connection to CLI.

All CLI parsers interacts with user terminal in order to parse a command (python argparse calls sys.argv for example).

This python module provides tools for doing the same job, without any interaction with the user terminal. It just parses a raw string into a structured object.

It also provides a way to declare completion of arguments names and parameters.

Getting Started

pip install statikomand

Command parser

from statikomand.komand_parser import KomandParser

parser = KomandParser()

parser.add_argument("-f", "--flag1", label="flag")
parser.add_argument("-d", "--delete", label="del")
parser.add_argument("POS1", label="pos1")
parser.add_argument("POS2")

code = "param1 param2 -f 'flag1 parameter' -d delete_param"
args = parser.parse(code)

print(args.pos1)  # value for POS1 positional argument : 'param1'
print(args.POS2)  # value for POS2 positional argument : 'param2'

Command completion

Each argument can have a completer method, which is a function that take one string argument, and returns a list of strings (proposition of completions)

The 'do_complete' method of the parser tries to complete the flag names, and calls the do_complete method of positional and / or flag arguments; according to the position of the last caracter.

Here is an example :

from statikomand.komand_parser import KomandParser
def completer_1(code: str):
    return ["completion1", "completion2"]


def completer_2(code: str):
    return [code + code + code]


parser = KomandParser()

parser.add_argument("-f", "--flag1", label="flag", completer=completer_1)
parser.add_argument("-d", "--delete", label="del", completer=completer_2)
parser.add_argument("POS1", completer=completer_1)
parser.add_argument("POS2", completer=completer_2)

# completes positional parameter 1 with completer_1
code = "this-code-will-be-replaced-by-completer-1"
res = parser.do_complete(code)
print(res)  # proposition of completions for code : ['completion1', 'completion2']


# completes flag's name
code2 = "param1 param2 --fl"
res2 = parser.do_complete(code2)
print(res2)  # proposition of completions for code : ['--flag1']

# completes flag1 parameter with flag1 completer
code3 = "param1 param2 --flag1 blabla"
res3 = parser.do_complete(code3)
print(res3)  # proposition of completions for code : ['completion1', 'completion1']

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

statikomand-0.0.3.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file statikomand-0.0.3.tar.gz.

File metadata

  • Download URL: statikomand-0.0.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for statikomand-0.0.3.tar.gz
Algorithm Hash digest
SHA256 f4cf6aa31460b6322a385f8397a358c34903da09feb6f5c7e0f7aa562cb8f4ec
MD5 d4bb20a6b527abcb1582035f281b2091
BLAKE2b-256 f9364365b3b36facb8d8ccc48b23a795f41c78d2a1826d4d450c8ec048820718

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