Show HN: Replbuilder, quickly build a Python REPL CLI prompt (github.com)
`pip install replbuilder`
Making a small tool for easier repl building, no more manual argument parsing. Perfect for creating ops tools and other context heavy cli operations.
Making a small tool for easier repl building, no more manual argument parsing. Perfect for creating ops tools and other context heavy cli operations.
23 comments
[ 8.9 ms ] story [ 70.9 ms ] threadBut here it is, feature requests welcome.
You can put any python you want in a file and run $ipython -i main.py and it'll drop you in a shell with whatever you want in scope. It can load configs from your ~/config directory, have a command line history, use tab completion, and you can the full language to compose those operations and inspect your results.
The purpose is not to do this however, the goal for a repl cli is usually to invoke a set of particular, already implemented commands, not on the fly python input and output. The implementation will be predefined and packaged, repl are only used to run a list of specific commands with arguments that implementation has already defined.
The purpose of such a repl is usually for context heavy operations where you don't want to instantiate all the context each time you call or be doing IPC calls. This is a simple way to keep a single process running and having a repl cli to communicate with it. The repl itself is not for composing code, but for executing commands. Think of something like the command line shell to query a nosql db or a list of command to interact with a particular system.
That's a very strange definition for a REPL, I would just call that an (interactive) CLI. Maybe that's why you couldn't find anything when you were doing your search? I used python-prompt-toolkit [0] when building such interfaces. pgcli [1] is an example of such an interface built with prompt-toolkit.
It has a lot of nice autocomplete and readline emulation options. Maybe it's something you can integrate with your project.
[0] https://github.com/prompt-toolkit/python-prompt-toolkit
[1] https://www.pgcli.com/
https://python-prompt-toolkit.readthedocs.io/en/master/pages...
It does seem the project you linked do refer themselves as repl in some capacity and is much more than what I had in mind, but these are great references.
https://github.com/click-contrib/click-repl
(Also should work with Typer and other libraries wrapping Click)
Naming is hard. Thanks for your reference, it looks pretty cool.
[0]: https://docs.python.org/3/library/code.html#code.Interactive...
[1]: https://github.com/python/cpython/blob/02d9f1504beebd98dea80...
Though I was more intending for each command/group of commands to get its own file/implementation, so it's less like a SQL engine and more like a client.
> gcp create vm ...
vm created ...
> gcp create pub-sub ...
pub sub created ...
etc etc
https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-a...
I've been spending a lot of time with python lately because of new project work, I had never really used python before. It's been really cool to keep finding stuff like this.
The equivalent of something like in the .net world (eg https://github.com/dotnet/command-line-api) and even powershell modules (https://learn.microsoft.com/en-us/powershell/module/microsof...) have a steeper learning curve and take significantly MORE work to set up for the end-user.
It seems like you’re not carrying any state, which is generally a great place to be. It might be nice to be able to explicitly pass in the return value from the last call as a argument to the next call. The calculator example is an obvious case. I’ve also wanted that when using many CLIs. It’s sort of like an after-the-fact pipe. If I create a server and the want to attach a database to a server, it seems like “server I created two seconds ago” is a good default. It’d be fine if I had to type _ or whatever.
argparse also basically give you the ability to utilize one command subcommand to do anything, but sometimes having the top level command freedom is nice.
- install ipython and rich
- set PYTHONSTARTUP to auto load whatever you like on start up
- call rich.pretty.install() in your startup script for a boosted repr()
- set %autocall to 1 to benefit from optional parentheses
- use ! to call shell commands
This covers most people needs for a non-custom REPL.
Still no reload() though, but python really doesn't like that.
But I just couldn't find much about small projects that enable quick custom command repl building. Either the package is enormous and a swiss army knife or they cater to a specific language (e.g. SQL). I'd like this package to be a small building stone for custom command cli of any kind.