Building a tree-based readline completer


R

roey.katz

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/xiang...evelopment/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...ter-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'. Atthe end of a successful completion, an additonal <tab> results in an offerto 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 optionnamed "FILENAME" marked in this way shall complete to FILENAME=" insteadof 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.
 
Ad

Advertisements

R

roey.katz

Thank you. In looking over these classes, I see though that even them, I would run against the same limitations, though.

- Roey
 
D

Dave Angel

them, I would run against the same limitations, though.

Please don't double space your quotes. And if googlegarbage is doing
it for you, then use a less buggy way of posting.
 
Ad

Advertisements

R

roey.katz

them, I would run against the same limitations, though.
Please don't double space your quotes. And if googlegarbage is doing
it for you, then use a less buggy way of posting.

Alright. Now, after the past three emails of going around my question, can anyone actually address it?

Thanks again!
- Roey
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top