00001
00002
00003 def quotedParse(cmdstring):
00004 '''Take a the given command string and parse it into a list using
00005 space delimiters but respecting (one layer of) quotes.'''
00006
00007 tokens = []
00008 for arg in cmdstring.split(" "):
00009 if '' != arg:
00010 tokens.append(arg)
00011 pass
00012 continue
00013
00014 firsts = map(lambda x: x[0], tokens)
00015 lasts = map(lambda x: x[-1], tokens)
00016
00017 quoteIndex = -1
00018 quoteMark = None
00019 for f in firsts:
00020 quoteIndex += 1
00021 if f == '"' or f == "'":
00022 quoteMark = f
00023 break;
00024 continue
00025
00026 if quoteMark is None:
00027 return tokens
00028
00029 endIndex = 0
00030 endFound = None
00031 for l in lasts:
00032 endIndex += 1
00033 if l == quoteMark:
00034 endFound = True
00035 break
00036 continue
00037
00038 if not endFound:
00039 raise ValueError, "end quote not found"
00040
00041 return tokens[:quoteIndex] + [" ".join(tokens[quoteIndex:endIndex])[1:-1]] + tokens[endIndex:]
00042
00043 if '__main__' == __name__:
00044
00045 import sys
00046 from optparse import OptionParser
00047
00048 parser = OptionParser(usage="cmdline.py [options]")
00049 parser.add_option("-m", "--module", action="append",
00050 help="Load given module and pass optional argument list")
00051 parser.disable_interspersed_args()
00052 (options,args) = parser.parse_args(args=sys.argv[1:])
00053
00054 for cmdline in options.module:
00055 print '|%s| --> |%s|'%(cmdline,'|'.join(quotedParse(cmdline)))
00056