5 ms·
Show HN: Replbuilder, quickly build a Python REPL CLI prompt
`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.
- deleted 4y ago[deleted]
- oakashes 4y agoI'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.
- Aperocky 4y agoI was fairly surprised when I went to pypi and tried to look for the same. But here it is, feature requests welcome.
- nurettin 4y agoWhat 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.
- Aperocky 4y agoIt would probably need quite a lot of magic to recreate `set -o vi` of the regular shell.
- kjeetgill 4y agoI 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.
- Aperocky 4y ago> 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.
- doix 4y ago> 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 https://github.com/prompt-toolkit/python-prompt-toolkit [1] https://www.pgcli.com/ https://www.pgcli.com/
- nurettin 4y agoLooks 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/asking_for_input.html#syntax-highlighting https://python-prompt-toolkit.readthedocs.io/en/master/pages...
- 4y ago
- notpushkin 4y agoGreat job! Heres a similar tool but for the Click command line toolkit instead of argparse: https://github.com/click-contrib/click-repl https://github.com/click-contrib/click-repl (Also should work with Typer and other libraries wrapping Click)
- Aperocky 4y agoI 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.
- polyrand 4y agoInteresting! 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.InteractiveConsole https://docs.python.org/3/library/code.html#code.Interactive... [1]: https://github.com/python/cpython/blob/02d9f1504beebd98dea807f4b8761d3675a500d0/Lib/sqlite3/__main__.py#L37 https://github.com/python/cpython/blob/02d9f1504beebd98dea80...
- Aperocky 4y agoOOO, 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.
- gigatexal 4y agoThis 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
- tyingq 4y agoThe aws cli does support aliases, which is more limited, but still helpful. https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-alias.html https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-a...
- crispyambulance 4y agoThis 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 https://github.com/dotnet/command-line-api) and even powershell modules (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.3 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.
- travisjungroth 4y agoNot 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.
- Aperocky 4y agoThat'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.
- stuff4ben 4y agoMy 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.
- BiteCode_dev 4y agoAs 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.
- Aperocky 4y agoI 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.
- mixmastamyk 4y agoThis 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.