def get_request_opts_help(): # Get help for our 'request' options. info = "The following options can be encoded in each request\n" \ " (but don't specify the leading --)" hf = HelpFormatter() tp = optparse.OptionParser("", formatter=hf) og = optparse.OptionGroup(tp, info) for opt in opts.get_request_options(): og.add_option(opt) tp.add_option_group(og) return tp.format_option_help(hf)
def main(): # build the args we support. start = datetime.datetime.now() all_args = {} for n, v in globals().iteritems(): # explicit check for functions so twisted classes don't match.. if type(v) == type(main) and getattr(v, "__doc__", None): all_args[n.replace("_", "-")] = v all_arg_names = sorted(all_args.keys()) description = ( __doc__ + "\nCommands\n help\n " + "\n ".join(all_args.keys()) + "\nUse '%prog help command-name' for help on a specific command.\n" ) parser = optparse.OptionParser("%prog [options]", description=description, formatter=HelpFormatter()) for opt in opts.get_program_options(): parser.add_option(opt) for opt in opts.get_request_options(): parser.add_option(opt) options, args = parser.parse_args() opts.setup_logging(options) init_config(options.config) proto.init_protocols() # patch up keys. if options.keys: def _fix_unicode(result): if isinstance(result, list): for i, v in enumerate(result): result[i] = _fix_unicode(result[i]) elif isinstance(result, unicode): result = result.encode("utf-8") return result for i, val in enumerate(options.keys): try: # help windows - replace single quote with double... val = val.replace("'", '"') options.keys[i] = _fix_unicode(json.loads(val)) except ValueError, why: parser.error("Invalid key value %r: %s" % (val, why))
def options_from_request(req): def mp_error(msg): # monkey-patch! raise RequestFailed(400, error=msg) parser = optparse.OptionParser() parser.error = mp_error for opt in opts.get_request_options(): parser.add_option(opt) # parse the options - the easy way - make a 'command-line'... argv = [''] # argv[0] - ignored... for name, val in req['query'].iteritems(): if val: argv.append("--%s=%s" % (name, val)) else: argv.append("--%s" % (name,)) options, _ = parser.parse_args(argv) return options