Tips for a *nix python cli - Programming On Unix

Users browsing this thread: 1 Guest(s)
venam
Administrators
Hello *nixer,
In this thread I'll give some tips for doing a python cli program. Feedbacks and comments are highly appreciated.

[*]Taking program arguments:
Users normally expect to be able to directly give the program arguments from the command line and to receive help when adding the -h or --help argument.
One way of doing that would be to parse the argc and argv. To do that you'll need to import sys. This method is more of a waste of time then anything else.
A better way of doing that would be to use the module optparse. It handles the help, the long arguments, and the short arguments.
Here's an example using optparse:
Code:
#!/usr/bin/env python
import optparse
def main():
    p = optparse.OptionParser()
    p.add_option('--person', '-p', default="world")
    options, arguments = p.parse_args()
    print 'Hello %s' % options.person

if __name__ == '__main__':
    main()

You can also find it here: https://paste.xinu.at/49o/
Here's an example of the output of the above program:
Code:
raptor ~ $ python2 python_cli.py --help                                                <
Usage: python_cli.py [options]

Options:
  -h, --help            show this help message and exit
  -p PERSON, --person=PERSON

[*]Tab completion:
Tab completion can be really nifty and adds a *nix feel to the program. If you search around the internet you won't find a lot of answers on how to do it. Here's a module https://raw.github.com/venam/badaboum/ma...mpleter.py that you can change as desired that when added to the program as followed makes tab completion work when taking the user's input.
Code:
import readline,completer

self.comp = completer.Completer()
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab:complete")
readline.set_completer(self.comp.complete)

[*]Creating a prompt/terminal-like/interpreter:
Some people might want to take things a step forward and create a prompt/terminal-like environment in their program. I mean something similar to the python interpreter, meterpreter, gdb, etc.. It is fairly easy to accomplish. All you have to do is play around with the tab completion and make it change how it completes depending of which part of the program the user is in. (State Pattern)

[*]Adding a choice between cli and curses:
Curses interfaces are not cli but they can give the program a feeling of completeness. There's many hard way of creating curses interfaces like using the curses module. To not loose some neurons along the way use the urwid module http://excess.org/urwid/tutorial.html. It helps creating curses as if the components were like the one of a GUI-like (text-box,button,label,etc..).

That's it! May the force be with you.


Messages In This Thread
Tips for a *nix python cli - by venam - 11-06-2013, 05:36 AM
RE: Tips for a *nix python cli - by gurhush - 12-06-2013, 07:30 PM
RE: Tips for a *nix python cli - by venam - 12-10-2014, 07:25 AM
RE: Tips for a *nix python cli - by b4dtR1p - 12-10-2014, 08:38 AM
RE: Tips for a *nix python cli - by projektile - 13-10-2014, 03:09 AM
RE: Tips for a *nix python cli - by ashen - 14-09-2015, 08:45 PM
RE: Tips for a *nix python cli - by venam - 15-09-2015, 01:52 AM
RE: Tips for a *nix python cli - by darthlukan - 15-09-2015, 02:18 AM
RE: Tips for a *nix python cli - by venam - 15-09-2015, 03:16 AM
RE: Tips for a *nix python cli - by darthlukan - 15-09-2015, 10:29 AM
RE: Tips for a *nix python cli - by ki113d - 17-09-2015, 10:15 PM
RE: Tips for a *nix python cli - by venam - 03-10-2015, 09:57 AM