views:

102

answers:

4

Hi, guys. I have seen some CLI questions here, but I still want to ask this question for more detailed answers.

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

also for this CLI, is it a 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 optparse module from python. is 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.

what i want is: 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?

+2  A: 

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

Ignacio Vazquez-Abrams
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
+1  A: 

Use the cmd module:

Other packages

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

pyfunc
+1  A: 

StackOverflow - How to write a shell in Python

smmv
I think the preferred way is warning about duplication in the question's comments.
tokland
@tokland: Ok, thank you!
smmv
+1  A: 

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
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
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