Created
April 27, 2014 02:10
-
-
Save rgs1/11336099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python 2.7 | |
from __future__ import print_function | |
from cmd import Cmd | |
from functools import partial | |
import readline | |
class Param(object): | |
def __init__(self, name, options=None): | |
self._name = name | |
self._named_param = "%s:" % (name) | |
if options: | |
self._options = options | |
else: | |
self._options = [] | |
@property | |
def name(self): | |
return self._name | |
@property | |
def named_param(self): | |
return self._named_param | |
@property | |
def options(self): | |
return self._options | |
class BoolParam(Param): | |
def __init__(self, name): | |
super(BoolParam, self).__init__(name, ["t", "f"]) | |
def complete(params, text, line, begidx, endidx): | |
for param in params: | |
if line.endswith(param.named_param): | |
return param.options | |
rv = [] | |
for param in params: | |
if param.name.startswith(text): | |
rv.append(param.name) | |
if len(rv) == 0: | |
rv = [p.name for p in params] | |
return rv | |
def complete_for(*params): | |
return partial(complete, params) | |
class Shell(Cmd): | |
def do_create(self, args): | |
print(args) | |
complete_create = complete_for( | |
Param("path", ["/foo"]), | |
Param("value", []), | |
BoolParam("ephemeral"), | |
BoolParam("sequence") | |
) | |
if __name__ == "__main__": | |
s = Shell() | |
s.cmdloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment