introducing mnemonics

This commit is contained in:
Miroslav Stampar 2011-06-15 11:58:50 +00:00
parent d55a242908
commit 1d93a03eeb
2 changed files with 76 additions and 0 deletions

View File

@ -2614,3 +2614,69 @@ def isNoneValue(value):
return not any(value)
else:
return value is None
def expandMnemonics(mnemonics, parser, args):
"""
Expand mnemonic options
"""
class MnemonicNode:
def __init__(self):
self.next = {}
self.current = []
head = MnemonicNode()
pointer = None
for group in parser.option_groups:
for option in group.option_list:
for opt in option._long_opts + option._short_opts:
pointer = head
for char in opt:
if char == "-":
continue
elif char not in pointer.next:
pointer.next[char] = MnemonicNode()
pointer = pointer.next[char]
pointer.current.append(option)
for mnemonic in mnemonics.split(','):
found = None
name = mnemonic.split('=')[0].replace("-", "")
value = mnemonic.split('=')[1] if len(mnemonic.split('=')) > 1 else None
pointer = head
for char in name:
if char in pointer.next:
pointer = pointer.next[char]
else:
pointer = None
if pointer in (None, head):
errMsg = "mnemonic '%s' can't be resolved to any parameter name" % name
logger.error(errMsg)
elif len(pointer.current) > 1:
options = {}
for option in pointer.current:
for opt in option._long_opts + option._short_opts:
options[opt.strip('-')] = option
if name in options:
found = name
debugMsg = "mnemonic '%s' resolved to %s). " % (name, found)
logger.debug(debugMsg)
else:
found = sorted(options.keys(), key=lambda x: len(x))[0]
warnMsg = "detected ambiguity (mnemonic '%s' can be resolved to %s). " % (name, ", ".join(options.keys()))
warnMsg += "resolved to shortest parameter name (%s)" % found
logger.warn(warnMsg)
found = options[found]
else:
found = pointer.current[0]
debugMsg = "mnemonic '%s' resolved to %s). " % (name, found)
logger.debug(debugMsg)
if found:
value = found.convert_value(found, value)
if value is not None:
setattr(args, found.dest, value)
else:
setattr(args, found.dest, True)

View File

@ -14,6 +14,7 @@ from optparse import OptionGroup
from optparse import OptionParser
from optparse import SUPPRESS_HELP
from lib.core.common import expandMnemonics
from lib.core.common import getUnicode
from lib.core.data import logger
from lib.core.settings import IS_WIN
@ -28,6 +29,7 @@ def cmdLineParser():
usage = "%s%s [options]" % ("python " if not IS_WIN else "", \
"\"%s\"" % sys.argv[0] if " " in sys.argv[0] else sys.argv[0])
parser = OptionParser(usage=usage, version=VERSION_STRING)
try:
@ -493,6 +495,9 @@ def cmdLineParser():
# Miscellaneous options
miscellaneous = OptionGroup(parser, "Miscellaneous")
miscellaneous.add_option("-z", dest="mnemonics",
help="Use mnemonics for shorter parameter setup")
miscellaneous.add_option("--beep", dest="beep",
action="store_true", default=False,
help="Alert when sql injection found")
@ -589,6 +594,11 @@ def cmdLineParser():
(args, _) = parser.parse_args(args)
for i in xrange(len(sys.argv)-1):
if sys.argv[i] == '-z':
expandMnemonics(sys.argv[i+1], parser, args)
break
if not any([args.direct, args.url, args.logFile, args.bulkFile, args.googleDork, args.configFile, \
args.requestFile, args.updateAll, args.smokeTest, args.liveTest, args.realTest, args.wizard, args.dependencies]):
errMsg = "missing a mandatory parameter (-d, -u, -l, -m, -r, -g, -c, --wizard, --update or --dependencies), "