0

I have an command line interpreter (or "line-oriented command interpreter" in the python docs for the cmd module) for a program that I'd like to add command line utility interface to.

For example, now a session looks like this: (% for shell prompt, :) is my custom prompt)
% tasks (invokes command line interpreter)
:) clockHours Teaching_Sara_to_coregister_T1_to_T2
:) exit

In addition, I want this interface:
% tasks clockHours Teaching_Sara_to_coregister_T1_to_T2

I envision custom interpreter commands mapped onto subcommands in the command line utility. Does there exist a library for doing these together? It would be great not to have to write completion code twice, command structure code twice, etc. If not, any advice for me if I try to implement this behavior, or thoughts on how useful it might be?

Obviously I lose the advantage of simple temporary variables, which is why I was using the interpreter approach to begin with, but many of my custom interpreter commands do not rely on this behavior, or could be easily modified not to require it - it is that subset that I want command line utility subcommands for.

0

2 Answers 2

1

cmd module may be enough for what you want if I correctly understand your problem.

Your final solution may be close to below example:

import cmd
import sys

class MyCmd(cmd.Cmd):
    def do_hello(self, line):
        print "hello"
    def do_exit(self, line):
        return True

if __name__ == '__main__':
    my_cmd = MyCmd()

    if len(sys.argv) > 1:
        my_cmd.onecmd(' '.join(sys.argv[1:]))
    else:
        my_cmd.cmdloop()

giving this behavior:

C:\_work\home>jython cmdsample.py hello
hello

C:\_work\home>jython cmdsample.py
(Cmd) hello
hello
(Cmd) exit

C:\_work\home>
Sign up to request clarification or add additional context in comments.

1 Comment

Good lookin'- thanks. There is the issue of autocompletion, but that's a shell thing I guess - ideally I write wrappers such that I can specify autocompletion behavior once, and it gets used in both situations, but it the autocompletion mechanisms might be too different for this to make sense.
1

Another thing that you may find useful is cmdln module.

3 Comments

Hm, I'll look into it - I've used cmd2 before, but never cmdln.
I'm confused by the documentation - does this do cmd.py-like things at all (like ftp), or just command line stuff (like git, hg)?
The primary purpose of the tool is to create command line stuff like git, hg. I didn't investigate compatibility with cmd module to provide interactive features with the same interface, but it seems natural to have this support there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.