Skip to main content

Quickly build a custom command REPL environment with this tool

Project description

ReplBuilder

PyPI version

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:

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.2.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

replbuilder-1.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: replbuilder-1.2.0.tar.gz
  • Upload date:
  • Size: 6.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.2.0.tar.gz
Algorithm Hash digest
SHA256 934f5fcf0c81714298a1b523f7445f871ff11118ffa50003e45e6d80c00b1ae3
MD5 8fe023f062a813c19340886106627caa
BLAKE2b-256 af40c5f50ffe45ea9330d36ebfe4155e63299dae8dab2ce202ba9f8772bf87a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: replbuilder-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79c84c718a0570955c02f87c692fb37cca45bfa4fc43bc9cd4b1c17b5f42e2e0
MD5 d408a152a0c77da5a58573ec52cb9222
BLAKE2b-256 0b241616c8d66e8bec2c274b304347e84c9d588f4eb92691ae9dba932ef6d012

See more details on using hashes here.

Supported by

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