ReplBuilder
pip install replbuilder
Quickly build a repl cli prompt for custom commands in python.
argparse is used for quick and easy parsing interface, you should be able to utilize the full power of argparse as your command parser.
A globally scoped context object can be optionally passed into each command, making context sharing and state possible. It also supports custom exception handlers, and aliases, and more.
This is a standalone package, it does not depend on any other packages.
For example, see project hnterminal, browse, vote and comment on HN in your terminal.
Example
see example calculator repl for example implementation. The gist can be concluded in a few lines:
add_cmd = ReplCommand("add", Calculator.basic_parser(), calculator.add, "Add 2 numbers")
sub_cmd = ReplCommand("sub", Calculator.basic_parser(), calculator.sub, "Subtract second number from first")
fact_cmd = ReplCommand("factorial", Calculator.factorial_parser(), calculator.factorial, "factorial with exception handler", exception_handler=exception_handler)
say_cmd = ReplCommand("cowsay", Cow.get_cowsay_parser(), cow.cowsay, "say stuff, demo optional and context usage", use_context=True)
mood_cmd = ReplCommand("cowmood", argparse.ArgumentParser(), cow.cowmood, "Mood of the cow changes with global context object", use_context=True)
calc_commands = [add_cmd, sub_cmd, fact_cmd]
cow_commands = [say_cmd, mood_cmd]
context = ContextObj()
runner = ReplRunner("calculator", context) # context is optional, but it helps maintaining state and avoiding duplicate work.
runner.add_commands(calc_commands, namespace="Calculator") # namespace is optional
runner.add_commands(cow_commands, namespace="Cow")
runner.run()
run it python example_calculator_repl.py
Part of the repl is colorized for better visibility:
Release files for replbuilder 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| replbuilder-1.2.1.tar.gz | 6.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| replbuilder-1.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:13.3 kB
Release files / replbuilder-1.2.1.tar.gz
| Download URL | replbuilder-1.2.1.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a82deea81c691e9f89159b51f088033695422dc292be00950682eacd706405d1
|
|
BLAKE2b-256 checksum How to use checksums |
dd21096e0f92ea5170ad26fcd1a91bfef9c67f7dbed0b49aa3c2949af11f28a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.13
|
Release files / replbuilder-1.2.1-py3-none-any.whl
| Download URL | replbuilder-1.2.1-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7409ccc0180c63c4d517c9d68caa405bf9e319455a7e25ee3527991dc4a4b6e3
|
|
BLAKE2b-256 checksum How to use checksums |
33952acd4aa114bde8f2ea548c31c7209227d6b54929c69b9e0cae722cba4302
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.13
|