Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: Replbuilder, quickly build a Python REPL CLI prompt (github.com/aperocky)
61 points by Aperocky on Feb 21, 2023 | hide | past | favorite | 22 comments
`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.



As a side note, if you want an efficient python REPL and you don't need custom commands like offered in the project above, you can do the following:

- 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.


I was actually intending for it to be custom commands, since I think it would be hard to beat even the vanilla python repl if we are talking about writing python.

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.


Interesting! You can also inherit from code.InteractiveConsole[0] to build a REPL. That's how the new sqlite3 REPL is implemented[1]

[0]: https://docs.python.org/3/library/code.html#code.Interactive...

[1]: https://github.com/python/cpython/blob/02d9f1504beebd98dea80...


OOO, I did not actually know this.

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.


This looks really nice.

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.


Great job! Heres a similar tool but for the Click command line toolkit instead of argparse:

https://github.com/click-contrib/click-repl

(Also should work with Typer and other libraries wrapping Click)


I did see click-repl when I was searching but I had no idea what click is and assumed it had something to do with mouse.

Naming is hard. Thanks for your reference, it looks pretty cool.


Not diminishing, just making sure I understand right after looking at the readme. Is this equivalent to having all of those commands be subcommands, except you call the outer one once and then you’re dropped into a context where you don’t have to call the outer one again until you exit? Useful for commands you tend to hammer on, like any hosting CLI.

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.


That's a very good point, I think right now it would be for the custom implementation to carry state, i.e. load the answer to the last question in self.result, and go on and do anything with the stored value from there.

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.


I'm really looking forward to trying this out, there have been multiple occasions over the last few years when I went looking for something exactly like this and came up empty.


I was fairly surprised when I went to pypi and tried to look for the same.

But here it is, feature requests welcome.


This is really cool. I immediatly thought it would be dope to have something like the aws or gcp cli tools incorporate something like this to be more interactive.

> gcp create vm ...

vm created ...

> gcp create pub-sub ...

pub sub created ...

etc etc


The aws cli does support aliases, which is more limited, but still helpful.

https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-a...


My daughters took Python last semester in HS and they had to build a calculator. Me being dad-programmer for 20+ years (Java mostly) pushed them to make something like this REPL builder. Of course it wasn't as nice, but they did get a decent grade out of it.


What I'm missing from cmd.Cmd is colored input and vim-style autocomplete. Maybe there is a readline replacement that is unbuffered and erases the line with \r characters to print an ansi colored interpretation of the line every time a key is pressed.


It would probably need quite a lot of magic to recreate `set -o vi` of the regular shell.


This is neat due to the commands. You’ll probably want to look into prompt-toolkit aka ptpython for the repl part. Surprised no one has mentioned it, it is quite robust and I thought well-known.


I hate to be a naysayer, but if you really want something like this -- just use ipython.

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.


> 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.


> 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.

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/


Looks like it supports inputs with syntax highlighting. This is something I've been looking for. Thanks for mentioning ppt.

https://python-prompt-toolkit.readthedocs.io/en/master/pages...


Ah this makes a lot of sense, I see my lack of naming skills cuts both ways.

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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: