Python CLI Framework and Argument Parsing with Tab Completion - python

I am looking for a good module that is already out there for parsing arguments on custom CLI interfaces. If you can imagine there is a CLI that gives you a prompt:
framework> commands go here....
It starts to get tedious when you add in arguments, example:
framework> command <argument1> <argument2> ...
I'd like to know if there is any sort of module that will not only make it easy to create these CLI interfaces, but one that has tab completion for each argument in a command chain. I'm tired of reinventing the wheel each time I create something like this!
framework> comma<tab>
framework> command arg<tab>
framework> command argument parame<tab>
framework> command argument parameter
You get the idea. If there is no module already out there for this sort of thing, any suggestions on an elegant solution to creating something like this in a modularized fashion, it would be much appreciated.
Please note that I do not want to parse arguments passed in through the parent process command shell (such as bash), I want to do the processing from within an infinite while loop within the script itself. For example:
while (True):
cmd = raw_input("framework> ")
framework.process_command(cmd)

Take a look at Click, "a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary": http://click.pocoo.org/5/

in python you have a build in module called argparse to manage CLI parameters application, now take a look to this module argparse extension for tab completion https://pypi.python.org/pypi/argcomplete/0.8.4

Looks like the builtin cmd module offers what you're looking for. It supports tab completion for commands and arguments. Here's a tutorial from PyMOTW. Another example here.
If you need some more customizations, there's also cmd2 which extends the builtin cmd.

Related

How do I create new command line flags in Ubuntu

I have files saved into a "file1.ors" format. In this case, they are similar to dictionary entries. I have been tasked with now converting them to other formats, such as HTML, Bibtext, or JSON. However, my supervisor wants me to use python from the command line, and he wants the choice to be decided with new command-line flags. For example,
$ orsconvert.py --json file1.ors
which would take file1.ors and convert it into a JSON format. The issue is that I cannot even find anything on how to create/define my own command line flags that I can use with the program. Any suggestions on how to do this?
Use library argparser, this lib comes pre-installed with Python and very userful for creating command line options. Also, will provide a leverage to use -h command to get all the necessay help (check the first example in the doc).
As an alternative to the built-in argparse library — which in my experience is rather weird to use, especially when you want to implement a complex command line interface —, you might also want to look into Typer which uses Python's new type annotations feature and is somewhat easier to grasp.

Is it possible to embed an argsparse parser in a function?

I define an argsparse object like this:
parser = argparse.ArgumentParser(description='{desc}', formatter_class=argparse.ArgumentDefaultsHelpFormatter)\
.format(desc=description)
Then I add arguments like this:
parser.add_argument("--config", "-c", help='config',
default='dbConf').format(dbConf=dbConfig)
Since I use these two lines in many scripts, I want to embed them into a function.
However, to my understanding the parser.add_argument() listens to the command line.
Is it possible to embed these lines into a function?
Is it possible to embed these lines into a function?
Yes. add_argument doesn't interact with the command line at all, though even if it did that would make little difference.
The one bit of argparse which interacts with the CLI input is the parse_args method, and what it does by default is access the global sys.argv attribute and process it. I wrote by default because you can also provide a list of strings as first parameter and it'll process that instead (if you click the link you'll see the official documentation does that to demonstrate various things in the examples).
So yes, you can very much have a function which creates an ArgumentParser and starts configuring it, then returns it for more configuration and ultimately, well, parsing the arguments.

python cmd module context based completion

In python cmd module, when I press the TAB button, I get the list of possible commands that are available and that I have defined in my cmd.Cmd class.
My question is whether it is possible to show a set of commands based on the context like in a Cisco router CLI for instance?
I don't have any experience with this lib, but doc says
If completion is enabled, completing commands will be done automatically, and completing of commands args is done by calling complete_foo() with arguments text, line, begidx, and endidx. text is the string prefix we are attempting to match: all returned matches must begin with it. line is the current input line with leading whitespace removed, begidx and endidx are the beginning and ending indexes of the prefix text, which could be used to provide different completion depending upon which position the argument is in.
So, may be you can implement context based completion in complete_foo function
I have found an answer to my question. One can use nested interpreters, so a nested interpreter is another interpreter object that will have its own commands, this way each context can be modeled by a nested interpreter. Here is a nice explanation and example:
object inheritance and nested cmd
If you want something like Cisco take a look at ishell,
ishell helps you to easily create an interactive shell for your application. It supports command completion, dynamic arguments, a command history, and chaining of commands.
https://github.com/italorossi/ishell
There's a cisco like cli example at https://github.com/italorossi/ishell/blob/master/examples/cisco.py
PS: I'm the author.

Python Twisted using argparse module

I'm building Python Twisted applications and would like to use the argparse module to parse command line options, instead of the Twisted usage.Options, which seems kind of old school.
I'd like to run the app with twistd, and am concerned about argparse interfering with twistd's command line argument parsing. I am seeking pointers, suggestions or opinions on what I'm trying to do.
Your twistd plugins are required to parse command-line options by providing a class with a parseOptions method that takes an argv-style list. Beyond that, what you do is up to you. If you supply a class with that method that calls into argparse, great.

argparse optional dependency for optional argument

I need to get to work something like this:
./foo.py [-b option [-a]]
with argparse. I thought about using sub_parsers, but I don't know how to make it work.
The straightforward way is to simply add rules in the codes that gets your options to throw an error if -a is there and not b (and document it in the user help). You can also override the usage string using the usage keyword: http://docs.python.org/dev/library/argparse.html#usage
sub_parsers are nice, but I don't think they fit your needs. Their usecase is more like putting a lot of features in a single executable (think svn add, svn ci, svn co, etc)

Categories