Building a tree-based readline completer

roey.katz at gmail.com roey.katz at gmail.com
Mon Nov 18 16:47:24 CET 2013


Hello all, for my project[1] I am trying to build a replacement completer for python's Cmd class which accepts a tree of command and option names[2].  I've been trying all sorts of approaches (like the ARLCompleter mention here: https://sites.google.com/site/xiangyangsite/home/technical-tips/software-development/python/python-readline-completions), but so far am up for a loss as to how to get this done.  

Can anyone help me out here?  I've placed a bounty on this with BountySource:  https://www.bountysource.com/issues/1319877-implement-a-generalized-completer-function-validator-and-filename-completer.

Thanks!
- Roey Katz
Bywaf developer 


(1) Bywaf, a command-line tool and framework for bypassing web application firewalls and more:  https://www.owasp.org/index.php/OWASP_Bywaf_Project

(2) What I'm looking for is a completer which accepts a tree of commands and option names.  Option names complete with a '=', and commands and sub-commands complete with a ' ', as in this hypothetical example:

> com<TAB PRESSED>
command com2
> comm<TAB PRESSED>
> command <TAB PRESSED>
> command OPT<TAB PRESSED>
OPTION_1 OPTION_2
> command OPTION_1<TAB PRESSED>
> command OPTION_1=<TAB PRESSED>
> command OPTION_1=


Ideally, I'd like to be able to pass in a dictionary tree of commands and options, for example:

params_tree = { 
  'command': {
      'OPTION_1=':None,
      'OPTION_2=':None,
      }
  'com2': None,
}

Where '=' indicates an option name.   An example of a more general tree, with more special symbols, would look like this:


# dictionary passed into the general completer function
params_tree = {

   # tab-complete 'quit'
   'quit':None,

   # tab-complete "TARGET_IP", and allow any string as its value
    'TARGET_IP=': '*',  

    # tab-complete "drive".  
    'drive': {

        # tab-complete "car", "truck", "bike" and also allow any other string.
        'VEHICLE=': ('car', 'truck, 'bike', '*'),  

       # tab-complete "hard disk", "sd card" and "remote share".
        'SAVE=': ('hard disk', 'sd card', 'remote share') 
        },

    # tab-complete "eat"
    'eat': {

        # tab-complete "FRUIT=", then tab-complete 'apple' or 'pear'.  At the end of a successful completion, an additonal <tab> results in an offer to insert a comma and complete additional items
        'FRUIT=+': ('apple', 'pear'),   

        # tab-complete "FRUIT=", then tab-complete 'water' or 'juice'.  At the end of a successful completion, an additonal <tab> results in an offer to insert a comma and complete additional items
        'DRINK=+': ('water', 'juice')
        },

    # tab-complete "cat", then tab-complete filenames.  At the end of a successful completion, close the quote, then upon another <tab>, offer to complete with a comma, allowing for completion of additional items
    'cat': 'FILE=/+'  
}

Notes:

An element in this dictionary can take None, a string, a tuple or a dictionary of more elements.

  *  "=' indicates that this is a plugin option.
  *  "/' indicates this plugin option should complete beginning with the path specified after the =, provided that this path meets security guidelines (i.e. it resolves to a path under a specific directory). A plugin option named "FILENAME" marked in this way shall complete to FILENAME=" instead of FILENAME=.
  *  "+" indicates that this option can be specified more than once. Multiple values will be collected into a list instead of a string.
  *  a "*" indicates that this option accepts any single value. User can specify any number of options, but only one of command from the same level


Additionally, the tree completer should should support escaping characters and nested quoted strings (alternating single- and double-quotes), and all in Unicode.




More information about the Python-list mailing list