6

I have already developed class1.py, class2.py, etc. with functions implemented inside each class. e.g. Operator.py has add, minus, time, divide functions. How can I build a command line interface for these classes?

Also for this CLI, is it an infinite loop inside the main() for interaction?

And how can the CLI give some feedback such as, suggesting the user for the next operation or to input right command or type --help and check all the available commands, like the Bash shells?

Also it seems there is the optparse module from Python. Are there some good, complete, or high quality samples showing how a CLI is built? I would like to take this chance to learn how to write a CLI program.

I have already developed several classes, and also a GUI to call the methods from these classes. Now I want to have a CLI, like the GUI, to use these classes. e.g. I have classes like CDContainer (with method like addCD, removeCD, etc), CD (with method like play, stop, pause), and I have a GUI that could be interacted. Now I want to have a CLI, which under the bash, I could run this CLI and call createCDContainer, addCD, removeCD commands.

If I use cmd,

class CDContainerCLI(cmd.Cmd):
            
    def do_CDContainer(self, line):
        print "create CD container"

    def do_addcd(self, line):
        print "add cd into the container"

how do I add some options here? e.g., I want to addcd --track 3 --cdname thriller.

I think --track 3 --cdname thriller they are the 4 arguments for the addcd function. How to implement that?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
pepero
  • 7,095
  • 7
  • 41
  • 72

4 Answers4

5

Derive from cmd.Cmd, overriding the various methods as necessary.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • hi, lgnacio, thank you so much for your suggestions. I tried with cmd.Cmd, and it is very good. but there is a question, how to add options. e.g. If I def do_login, how can I add --username, --password, as options? – pepero Oct 23 '10 at 17:28
1

Use the cmd module:

Other packages

You can also use various modules hosted at pypi that is built on top of cmd module

Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
1

I'm not sure what you're looking for: it seems you want to build an interactive shell, but you also mention optparse, which is designed to streamline the creation of a module interface that can be run from the system shell rather than providing a shell in itself.

It looks like you do want your module to implement its own interactive shell, but maybe you also want to have some commands accessible from the command line, e.g. by calling your_script the_command --argument value --other-argument right from bash or what have you. This is the sort of thing that optparse is meant to provide, but it has been deprecated in favour of argparse. argparse is in the Python 2.7 standard library and can be installed in the standard way (e.g. as a dependency of your module, or by separate installation via PyPI, etc.) for older Pythons.

argparse makes it relatively straightforward to link particular options or subcommands to entry points (i.e. function calls) in your code. The documentation is quite detailed, and is worth a thorough read if you're likely to want to make a few such interfaces. For advanced usage you can do some interesting stuff, and make your code a bit more manageable, by creating a custom action.

intuited
  • 23,174
  • 7
  • 66
  • 88
  • hi, intuited, thank you for your comment. I add some specific comments about what I want to do. If I use cmd, I could have cli, but i do not know how to have options. – pepero Oct 23 '10 at 17:48
  • I don't think `cmd` provides any support for exporting python functionality to calls from the system shell; that *is* what `argparse` does. You want to put the code that actually does stuff in a separate module/class, and call into it from both `cmd` and `argparse`. It looks like you'll want to use [sub-commands](http://docs.python.org/library/argparse.html#argparse.ArgumentParser.add_subparsers) to get what you want. But Ignacio is right: you should ask another question especially since you've already accepted one unrelated to this issue. – intuited Oct 23 '10 at 20:49
0

I think what he's asking about is how to easily deal with optional arguments within the interactive shell, so when you use the program it will look something like this:

$ myprogram
(Cmd) addcd --track 3 --cdname thriller

So running myprogram opens up its own command prompt, to which commands such as addcd can be issued, along with optional arguments, and then processed.

The best way to do this, I think, would be to use argparse along with cmd. Instead of parsing sys.argv, the parse_args method can be passed a list of strings. So something like below:

def do_addcd(self, line):
    parser = argparse.ArgumentParser(prog='addcd')
    parser.add_argument('--track', type=int)
    parser.add_argument('--cdname')

    args = parser.parse_args(line.split())
    newcd = CD(args.track, args.cdname)

The problem with doing something like this, as I have found out myself by trying to do exactly this sort of thing, is that parse_args tends to exit the entire program if you supply it with the wrong number of arguments, among other errors. The desired behaviour in this use case would be to simply exit back to your custom interactive shell, but that won't be easy to do without either some hacky workaround or subclassing ArgumentParser and overriding parse_args.

ztangent
  • 1,811
  • 2
  • 14
  • 11
  • As a relatively clean solution to the problem I described above (which I just discovered), you can derive a class from ArgumentParser and override the error method so that it does not exit but just raises an exception, which can then be caught within your Cmd methods. – ztangent Dec 16 '10 at 11:08
  • This sounds like quite a worthwhile usage, it might be worth filing a featurerequest against python for this not to just quit. – Stuart Axon Feb 25 '14 at 11:14