cloudnativelabs / kube-shell

Kubernetes shell: An integrated shell for working with the Kubernetes
Apache License 2.0
2.38k stars 172 forks source link

explore possibility of using python argparse to pass the parameters for kubectl #14

Closed murali-reddy closed 7 years ago

murali-reddy commented 7 years ago

Current custom parser in completer.py is not really decent piece of code. Explore if it can offloaded to Python's argparse.

vogxn commented 7 years ago

Ran into a bunch of troubles doing this. My initial idea was to read the command tree into a nested set of parsers created using argparse. However, argparse is not tolerant if parts of the command line don't adhere to the command tree it holds. It does a hard os.exit() which is problematic.

Going to switch back to a simple object tree representation. We will walk the tree and identify the extent of the traversal until tokens on command line are identified. The children of the node we reach point to the list of possible suggestions.


// CommandTree represents the Node (command) and its children (subcommands/options)
type CommandTree struct {
    Node string,
    Children []CommandTree
}

func Walk(t *CommandTree, parsed []string, unparsed []string) (suggestions []string) {
    token = unparsed.Pop()   // get first token
    if t.Node == token {
       parsed = append(parsed, token)
       for _, child := range(t.Children) {
            // walk all children with rest of the tokens
            Walk(child, parsed, unparsed)
       }
    } else {
        if len(unparsed) > 0 {
            suggestions := make([]string)
            for _, suggestion := range(t.Children) {
                   suggestions = append(suggestions, suggestion)
            }
            return  // suggestions come from children
        }
    }
}
vogxn commented 7 years ago

I've taken the above mentioned approach and reduced the clutter in the completer.py module. There is now a parser.py (complementing a lexer.py) that reads the entire command set into a tree rooted at kubectl. Given a command line, we walk the nodes of this tree as far as we can and return the children of the last node in the path as suggestions to fuzzyfinder.

TODO:

murali-reddy commented 7 years ago

In case any one wants try out please check this branch