3

I'm looking to create a CLI application in Python that can be run directly from the command line:

$ myapp command --flag --argument foo

or can be run as a REPL application:

$ myapp
(context)> command --flag --argument foo
(context)> other --with different --flags

I took a look at Click (including this question) and argparse, which seem to be good for the former but have limited support for the latter, and cmd2, which seems to be good for the latter but have limited support for the former.

Is there a recommendation for what to use in this situation? I have considered simply using two different libraries with their own wrappers to the same core "business logic", but I would prefer to use something built for this purpose, if such a thing exists.

asked Sep 5, 2021 at 11:12

2 Answers 2

1

I'm in the same boat. Python Prompt Toolkit looks excellent for creating a REPL type app. In fact the author has created a "better Python REPL" using it as well as full screen apps like VIM clone in pure python. I'm just starting with it but it looks good.

Interestingly, there was a bug filed about command line arg parsing and the author responded that something like click or argparse could do that. So I think you can combine the two libraries to achieve what you're looking for.

Finally, the issue of accepting strings from the REPL and parsing them for args is similar to this question on Stack Overflow and the answers might help.

answered Sep 7, 2021 at 15:24
Sign up to request clarification or add additional context in comments.

2 Comments

So it seems like using Click-decorated functions that get invoked in a Prompt Toolkit session is the way to go. Thanks!
I found another library that bridges the two. github.com/click-contrib/click-repl creates a repl that exposes click commands as repl commands and will prompt for your args.
0

Nessaid cli, is a CLI builder tool mainly for REPL applications, though any valid repl expression can be fed to the program from command line. It has the following features.

Grammar driven CLI definitions Support for multiple context sub clis in main cli Support for mandatory, optional and alternative arguments. Support for a set of argents and expressions in any order Support for custom token definitions Auto completion and suggestions in repl mode. Support for arguments with spaces and special characters with enclosed quotes.

The project page: https://github.com/saithalavi/nessaid_cli

Basic CLI implementation: https://github.com/saithalavi/nessaid_cli/blob/master/doc/simple-cli/simple_cli.py

A basic router CLI example: https://github.com/saithalavi/nessaid_cli/tree/master/doc/router-box-cli

Documentation is mainly for the repl environment. But any command sequence acceptable in interactive mode can be executed from commandline by feeding the arguments to the class method execute_args or feeding the entire line to execute_line of the subclass of NessaidCmd.

That is like,

from nessaid_cli.cmd import NessaidCmd
class MyCli(NessaidCmd):
 def do_command1(self):
 """Cli grammar and code for this command"""
 pass
 aync def do_command2(self, arg1, arg2):
 """Cli grammar and code for this command"""
 pass
import sys
import asyncio
if __name__ == '__main__':
 asyncio.get_event_loop().run_until_complete(
 MyCli.execute_args(sys.argv[1:]))

For REPL mode, the class can be initialized and the run() method can be invoked.

Like

MyCli(prompt="# ").run()
answered Sep 17, 2021 at 20:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.