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.
2 Answers 2
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.
2 Comments
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()
Comments
Explore related questions
See similar questions with these tags.