Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Last active April 8, 2021 22:19
Show Gist options
  • Save fuzzy/de0552a3649cbe8e19e32e7e11d50a68 to your computer and use it in GitHub Desktop.
Save fuzzy/de0552a3649cbe8e19e32e7e11d50a68 to your computer and use it in GitHub Desktop.
def _argParse(self, args):
if len(args) > 1:
if args[1] not in self.config.arguments.keys():
self.showHelp()
else:
# We really don't need the program name
args.pop(0)
# ok, we have a valid command, now let's see if we
# have a valid handler
if self.config.arguments[args[0]]['handler']:
# it appears so, so let's record our handler
command = args[0]
handlerObj = self.config.arguments[args[0]]['handler']
else:
# it appears not, so let's bitch about it.
raise(ValueError, 'Handler for this command not yet implimented.')
args.pop(0)
# We can start building our handler reference
handlerStr = 'self.%s(' % handlerObj
endItem = ''
# Now let's care about our suboptions
for arg in args:
if arg[0] == '-':
for opt in self.config.arguments[command].options:
if opt[0] == arg:
handlerStr += '%s=%r,' % (opt[2][0], opt[2][1])
else:
endItem = arg
if endItem != '':
handlerStr += 'arg="%s")' % endItem
else:
if handlerStr[-1:][0] == ',':
tmpStr = '%s)' % handlerStr[:-1]
handlerStr = tmpStr
else:
handlerStr += ')'
# And finally, let's handle the shit
eval(handlerStr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment