• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    Python
  • Created almost 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python helper tool for building and running a repl with custom commands

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

More Repositories

1

cellular-automata

TypeScript
45
star
2

diaryman

Lazy (wo)man's CLI diary manager
Shell
27
star
3

hnterminal

terminal client for browsing hacker news
Python
14
star
4

prehistoric-simulation

Simulator in browser
TypeScript
9
star
5

diarycli

diaryman.sh as pip package
Python
7
star
6

PortScan

command line port scan utility written in python
Python
7
star
7

prehistoric

A simulator in browser
TypeScript
5
star
8

hydro-sim

Build a random terrain with water and lakes and evaporating seas in term based simulation.
TypeScript
5
star
9

unix-setup

setups, .bashrc and .vimrc for new unix like computers and virtual machines.
Shell
4
star
10

particle_canvas

A remake of the particle game on HTML5 canvas with javascript
JavaScript
3
star
11

Scraper

A lightweight scraping class with recursive support and csv packing utilities
Python
3
star
12

wallpinger

A web application in django to check which site is blocked by the GFWoPRC
Python
2
star
13

sqlitedao

Simple dao for sqlite for personal/desktop projects
Python
2
star
14

conways-game-of-life

ts and webpage for Conway's game of life. RIP.
TypeScript
2
star
15

flock-behavior

Python
1
star
16

terrain_map_generator

Python
1
star
17

Robot_Simulation

Java
1
star
18

wolf-mafia

A server/client architecture to replace god/judge for 10+ person game of mafia known as wolf
Python
1
star
19

weighted-markov-generator

Weighted markov text generator, intended for either node or client side use.
TypeScript
1
star
20

2dheat

Heat!
Python
1
star
21

MinimalPipeline

A minimal framework for independently running tasks to form a pipeline.
Python
1
star
22

ksp_eng

Kerbal Space Career Mode Autopilot Scripts.
KerboScript
1
star
23

sound-analyzer

Python
1
star
24

batch-compress-video-cli

Batch compress raw gopro videos.
Python
1
star
25

astrophysics

some calculation for imagination
Python
1
star
26

shadojava

Dispatch - Operator task simulation core.
Java
1
star
27

shado

PHP
1
star
28

survey_server

A survey web application in PHP with access control, custom creation and administration of surveys
PHP
1
star