Skip to main content

Quickly build a custom command REPL environment with this tool

Project description

ReplBuilder

pip install replbuilder

Quickly build a repl cli prompt for custom commands in python.

argparse is used for quick and easy parsing interface, some options are overriden for using it within a repl prompt. But you should be able to utilize the full power of argparse as your repl parser.

A globally scoped context object can be optionally passed into each command, making context sharing and state possible.

Each command can also accommodate optional custom exception handler, allowing for the same exception handler to be passed into multiple different commands.

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:

example repl run

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

replbuilder-1.0.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

replbuilder-1.0.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file replbuilder-1.0.3.tar.gz.

File metadata

  • Download URL: replbuilder-1.0.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for replbuilder-1.0.3.tar.gz
Algorithm Hash digest
SHA256 e759b2a7b239073a1aa413baadb6984cd45c696015dc4a50bfa37a8493d0c207
MD5 91029faaa4c26c95a041ccbdb2de7959
BLAKE2b-256 dfd2a90bb1caaed1c34c67aea730ca58d71aeb08710a74491c4926cc1e298280

See more details on using hashes here.

File details

Details for the file replbuilder-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: replbuilder-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for replbuilder-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4d828e52c1d68f7e2eeb3c58c810df8229ccbdd314d3b04bf57b85b33cb634f8
MD5 de6b3064be1158ac9f8254f324bd6a98
BLAKE2b-256 7ad70b0ad3131cf322fc2fd091270184976689628825fdf3779dd60e3eaa2206

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