예제 #1
0
파일: tokens.py 프로젝트: skg-net/ops-cli
class TKeyword(Token):
    def __init__(self, *args, **kwargs):
        Token.__init__(self, **kwargs)
        if len(args) > 1:
            raise TypeError('only one keyword can be declared at a time')
        self.word = Str_help(args[0])

    def __repr__(self):
        return "<%s '%s'>" % (self.__class__.__name__, str(self.word))

    def nail(self, word):
        if not self.verify(word):
            raise ValueError("invalid keyword")
        self.value = self.word

    def enum(self):
        return [self.word]

    def complete(self, word):
        if self.word.startswith(word):
            return [self.word]
        else:
            return []

    def syntax(self):
        return [self.word]

    def verify(self, word):
        # Must match the declared keyword, or at least complete to it.
        return self.word.startswith(word)
예제 #2
0
class TKeyword(Token):
    def __init__(self, *args, **kwargs):
        Token.__init__(self, **kwargs)
        if len(args) > 1:
            raise TypeError('only one keyword can be declared at a time')
        self.word = Str_help(args[0])

    def __repr__(self):
        return "<%s '%s'>" % (self.__class__.__name__, str(self.word))

    def nail(self, word):
        if not self.verify(word):
            raise ValueError("invalid keyword")
        self.value = self.word

    def enum(self):
        return [self.word]

    def complete(self, word):
        if self.word.startswith(word):
            return [self.word]
        else:
            return []

    def syntax(self):
        return [self.word]

    def verify(self, word):
        # Must match the declared keyword, or at least complete to it.
        return self.word.startswith(word)
예제 #3
0
파일: tokens.py 프로젝트: skg-net/ops-cli
 def __init__(self, *args, **kwargs):
     Token.__init__(self, **kwargs)
     self.allowed = []
     for arg in args:
         if not isinstance(arg, str) and not isinstance(arg, tuple):
             raise TypeError('only strings (and help) are allowed')
         self.allowed.append(Str_help(arg))
예제 #4
0
 def __init__(self, *args, **kwargs):
     Token.__init__(self, **kwargs)
     if len(args) > 1:
         raise TypeError('only one keyword can be declared at a time')
     self.word = Str_help(args[0])
예제 #5
0
파일: tokens.py 프로젝트: skg-net/ops-cli
 def __init__(self, *args, **kwargs):
     Token.__init__(self, **kwargs)
     if len(args) > 1:
         raise TypeError('only one keyword can be declared at a time')
     self.word = Str_help(args[0])
예제 #6
0
파일: tokens.py 프로젝트: skg-net/ops-cli
 def syntax(self):
     return [
         Str_help(('<IPv4 address>', self.help_text)),
         Str_help(('<IPv6 address>', self.help_text))
     ]
예제 #7
0
파일: tokens.py 프로젝트: skg-net/ops-cli
 def syntax(self):
     return [Str_help(('<interface>', self.help_text))]
예제 #8
0
파일: tokens.py 프로젝트: skg-net/ops-cli
 def syntax(self):
     if self.min_int is not None and self.max_int is not None:
         summary = "%d-%d" % (self.min_int, self.max_int)
     else:
         summary = '<number>'
     return [Str_help((summary, self.help_text))]
예제 #9
0
파일: cli.py 프로젝트: skg-net/ops-cli
 def helpline(self, cmdobj, prefix=None):
     if prefix:
         words = cmdobj.command[len(prefix):]
     else:
         words = cmdobj.command
     return Str_help((' '.join(words), cmdobj.__doc__))
예제 #10
0
파일: cli.py 프로젝트: skg-net/ops-cli
    def qhelp(self, line):
        '''Called when ? is pressed. line is the text up to that point.
        Returns help items to be shown, as a list of (command, helptext).'''
        items = []
        words = line.split()
        cmdtree = context_get().cmdtree
        if words:
            matches = self.find_command(cmdtree, words)
            if not matches:
                raise Exception(CLI_ERR_NOMATCH)
            if line[-1].isspace():
                # Last character is a space, so whatever comes before has
                # to match a command unambiguously if we are to continue.
                if len(matches) != 1:
                    raise Exception(CLI_ERR_AMBIGUOUS)

                cmd_complete = False
                cmdobj = matches[0]
                for key in cmdobj.branch:
                    items.append(self.helpline(cmdobj.branch[key], words))
                if hasattr(cmdobj, 'options'):
                    opt_words = words[len(cmdobj.command):]
                    if not opt_words and F_NO_OPTS_OK in cmdobj.flags:
                        # Didn't use any options, but that's ok.
                        cmd_complete = True
                    elif len(opt_words) == len(cmdobj.options):
                        # Used all options, definitely a complete command.
                        cmd_complete = True
                    elif opt_words:
                        # Only some options were used, check if we missed
                        # any required ones.
                        try:
                            opt_tokens = tokenize_options(
                                opt_words, cmdobj.options)
                            check_required_options(opt_tokens, cmdobj.options)
                            # Didn't bomb out, so all is well.
                            cmd_complete = True
                        except:
                            pass
                    items.extend(help_options(cmdobj, words))
                else:
                    # Command has no options.
                    cmd_complete = True
                if cmd_complete and hasattr(cmdobj, 'run'):
                    items.insert(0, Str_help(('<cr>', cmdobj.__doc__)))
            else:
                # Possibly incomplete match, not ending in space.
                for cmdobj in matches:
                    if len(words) <= len(cmdobj.command):
                        # It's part of the command
                        items.append(self.helpline(cmdobj, words[:-1]))
                    else:
                        # Must be an option.
                        items.extend(complete_options(cmdobj, words))
        else:
            # On empty line: show all commands in this context.
            for key in cmdtree.branch:
                items.append(self.helpline(cmdtree.branch[key]))
            try:
                global_tree = get_cmdtree('global')
                for key in global_tree.branch:
                    items.append(self.helpline(global_tree.branch[key]))
            except ValueError:
                pass

        return sorted(items)