def parse_task(self, cmdline_args=None, main_task_cls=None): parser = ErrorWrappedArgumentParser() add_global_parameters(parser) if main_task_cls: add_task_parameters(parser, main_task_cls) else: orderedtasks = '{%s}' % ','.join(sorted(Register.get_reg().keys())) subparsers = parser.add_subparsers(dest='command', metavar=orderedtasks) for name, cls in Register.get_reg().iteritems(): subparser = subparsers.add_parser(name) if cls == Register.AMBIGUOUS_CLASS: continue add_task_parameters(subparser, cls) # Add global params here as well so that we can support both: # test.py --global-param xyz Test --n 42 # test.py Test --n 42 --global-param xyz add_global_parameters(subparser) args = parser.parse_args(args=cmdline_args) if main_task_cls: task_cls = main_task_cls else: task_cls = Register.get_task_cls(args.command) # Notice that this is not side effect free because it might set global params set_global_parameters(args) task_params = get_task_parameters(task_cls, args) return [task_cls(**task_params)]
def parse_task(self, cmdline_args=None, main_task_cls=None): parser = ErrorWrappedArgumentParser() self.add_global_parameters(parser) if main_task_cls: self.add_task_parameters(parser, main_task_cls) else: orderedtasks = '{%s}' % ','.join(sorted(Register.get_reg().keys())) subparsers = parser.add_subparsers(dest='command', metavar=orderedtasks) for name, cls in Register.get_reg().iteritems(): subparser = subparsers.add_parser(name) if cls == Register.AMBIGUOUS_CLASS: continue self.add_task_parameters(subparser, cls) # Add global params here as well so that we can support both: # test.py --global-param xyz Test --n 42 # test.py Test --n 42 --global-param xyz self.add_global_parameters(subparser) args = parser.parse_args(args=cmdline_args) params = vars(args) # convert to a str -> str hash if main_task_cls: task_cls = main_task_cls else: task_cls = Register.get_task_cls(args.command) # Notice that this is not side effect free because it might set global params task = task_cls.from_str_params(params, Register.get_global_params()) return [task]
def parse_task(self, cmdline_args=None, main_task_cls=None): parser = ErrorWrappedArgumentParser() self.add_global_parameters(parser) if main_task_cls: self.add_task_parameters(parser, main_task_cls) else: orderedtasks = '{%s}' % ','.join(sorted(Register.get_reg().keys())) subparsers = parser.add_subparsers(dest='command', metavar=orderedtasks) for name, cls in Register.get_reg().iteritems(): subparser = subparsers.add_parser(name) if cls == Register.AMBIGUOUS_CLASS: continue self.add_task_parameters(subparser, cls) # Add global params here as well so that we can support both: # test.py --global-param xyz Test --n 42 # test.py Test --n 42 --global-param xyz self.add_global_parameters(subparser) args = parser.parse_args(args=cmdline_args) params = vars(args) # convert to a str -> str hash if main_task_cls: task_cls = main_task_cls else: task_cls = Register.get_reg()[args.command] if task_cls == Register.AMBIGUOUS_CLASS: raise Exception('%s is ambigiuous' % args.command) # Notice that this is not side effect free because it might set global params task = task_cls.from_input(params, Register.get_global_params()) return [task]
def error(self, message): result = re.match("argument .+: invalid choice: '(\w+)'.+", message) if result: arg = result.group(1) weightedTasks = [(self._editdistance(arg, task), task) for task in Register.get_reg().keys()] orderedTasks = sorted(weightedTasks, key=lambda pair: pair[0]) candidates = [task for (dist, task) in orderedTasks if dist <= 5 and dist < len(task)] displaystring = "" if candidates: displaystring = "No task %s. Did you mean:\n%s" % (arg, '\n'.join(candidates)) else: displaystring = "No task %s." % arg super(ErrorWrappedArgumentParser, self).error(displaystring) else: super(ErrorWrappedArgumentParser, self).error(message)
def error(self, message): result = re.match("argument .+: invalid choice: '(\w+)'.+", message) if result: arg = result.group(1) weightedTasks = [(self._editdistance(arg, task), task) for task in Register.get_reg().keys()] orderedTasks = sorted(weightedTasks, key=lambda pair: pair[0]) candidates = [task for (dist, task) in orderedTasks if dist <= 5 and dist < len(task)] displaystring = "" if candidates: displaystring = "No task %s. Did you mean:\n%s" % (arg, '\n'.join(candidates)) else: displaystring = "No task %s." % arg super(ErrorWrappedArgumentParser, self).error(displaystring) else: super(ErrorWrappedArgumentParser, self).error(message)
def parse_task(self, cmdline_args=None, main_task_cls=None): parser = argparse.ArgumentParser() add_global_parameters(parser) if main_task_cls: add_task_parameters(parser, main_task_cls) args = parser.parse_args(args=cmdline_args) task_cls = main_task_cls else: task_names = sorted(Register.get_reg().keys()) # Parse global arguments and pull out the task name. # We used to do this using subparsers+command, but some issues with # argparse across different versions of Python (2.7.9) made it hard. args, unknown = parser.parse_known_args(args=cmdline_args) if len(unknown) == 0: raise SystemExit('No task specified') task_name = unknown[0] if task_name not in task_names: error_task_names(task_name, task_names) task_cls = Register.get_task_cls(task_name) # Add a subparser to parse task-specific arguments subparsers = parser.add_subparsers(dest='command') subparser = subparsers.add_parser(task_name) # Add both task and global params here so that we can support both: # test.py --global-param xyz Test --n 42 # test.py Test --n 42 --global-param xyz add_global_parameters(subparser) add_task_parameters(subparser, task_cls) # Workaround for bug in argparse for Python 2.7.9 # See https://mail.python.org/pipermail/python-dev/2015-January/137699.html subargs = parser.parse_args(args=cmdline_args) for key, value in vars(subargs).items(): if value: # Either True (for boolean args) or non-None (everything else) setattr(args, key, value) # Notice that this is not side effect free because it might set global params set_global_parameters(args) task_params = get_task_parameters(task_cls, args) return [task_cls(**task_params)]
def parse(self, cmdline_args=None, main_task_cls=None): global_params = list(Register.get_global_params()) parser = PassThroughOptionParser() tasks_str = '/'.join(sorted([name for name in Register.get_reg()])) def add_task_option(p): if main_task_cls: p.add_option('--task', help='Task to run (' + tasks_str + ') [default: %default]', default=main_task_cls.task_family) else: p.add_option('--task', help='Task to run (%s)' % tasks_str) def _add_parameter(parser, param_name, param): description = [param_name] if param.description: description.append(param.description) if param.has_default: description.append(" [default: %s]" % (param.default,)) if param.is_list: action = "append" elif param.is_boolean: action = "store_true" else: action = "store" parser.add_option('--' + param_name.replace('_', '-'), help=' '.join(description), default=None, action=action) for param_name, param in global_params: _add_parameter(parser, param_name, param) add_task_option(parser) options, args = parser.parse_args(args=cmdline_args) task_cls_name = options.task if self.__existing_optparse: parser = self.__existing_optparse else: parser = optparse.OptionParser() add_task_option(parser) if task_cls_name not in Register.get_reg(): raise Exception('Error: %s is not a valid tasks (must be %s)' % (task_cls_name, tasks_str)) # Register all parameters as a big mess task_cls = Register.get_reg()[task_cls_name] if task_cls == Register.AMBIGUOUS_CLASS: raise Exception('%s is ambiguous' % task_cls_name) params = task_cls.get_nonglobal_params() for param_name, param in global_params: _add_parameter(parser, param_name, param) for param_name, param in params: _add_parameter(parser, param_name, param) # Parse and run options, args = parser.parse_args(args=cmdline_args) params = {} for k, v in vars(options).iteritems(): if k != 'task': params[k] = v task = task_cls.from_input(params, global_params) return [task]
def parse(self, cmdline_args=None, main_task_cls=None): parser = ErrorWrappedArgumentParser() def _add_parameter(parser, param_name, param, prefix=None): description = [] if prefix: description.append('%s.%s' % (prefix, param_name)) else: description.append(param_name) if param.description: description.append(param.description) if param.has_default: description.append(" [default: %s]" % (param.default,)) if param.is_list: action = "append" elif param.is_boolean: action = "store_true" else: action = "store" parser.add_argument('--' + param_name.replace('_', '-'), help=' '.join(description), default=None, action=action) def _add_task_parameters(parser, cls): for param_name, param in cls.get_nonglobal_params(): _add_parameter(parser, param_name, param, cls.task_family) def _add_global_parameters(parser): for param_name, param in Register.get_global_params(): _add_parameter(parser, param_name, param) _add_global_parameters(parser) if main_task_cls: _add_task_parameters(parser, main_task_cls) else: orderedtasks = '{%s}' % ','.join(sorted(Register.get_reg().keys())) subparsers = parser.add_subparsers(dest='command', metavar=orderedtasks) for name, cls in Register.get_reg().iteritems(): subparser = subparsers.add_parser(name) if cls == Register.AMBIGUOUS_CLASS: continue _add_task_parameters(subparser, cls) # Add global params here as well so that we can support both: # test.py --global-param xyz Test --n 42 # test.py Test --n 42 --global-param xyz _add_global_parameters(subparser) args = parser.parse_args(args=cmdline_args) params = vars(args) # convert to a str -> str hash if main_task_cls: task_cls = main_task_cls else: task_cls = Register.get_reg()[args.command] if task_cls == Register.AMBIGUOUS_CLASS: raise Exception('%s is ambigiuous' % args.command) # Notice that this is not side effect free because it might set global params task = task_cls.from_input(params, Register.get_global_params()) return [task]
def parse(self, cmdline_args=None, main_task_cls=None): global_params = list(Register.get_global_params()) parser = PassThroughOptionParser() tasks_str = '/'.join(sorted([name for name in Register.get_reg()])) def add_task_option(p): if main_task_cls: p.add_option('--task', help='Task to run (' + tasks_str + ') [default: %default]', default=main_task_cls.task_family) else: p.add_option('--task', help='Task to run (%s)' % tasks_str) def _add_parameter(parser, param_name, param): description = [param_name] if param.description: description.append(param.description) if param.has_default: description.append(" [default: %s]" % (param.default, )) if param.is_list: action = "append" elif param.is_boolean: action = "store_true" else: action = "store" parser.add_option('--' + param_name.replace('_', '-'), help=' '.join(description), default=None, action=action) for param_name, param in global_params: _add_parameter(parser, param_name, param) add_task_option(parser) options, args = parser.parse_args(args=cmdline_args) task_cls_name = options.task if self.__existing_optparse: parser = self.__existing_optparse else: parser = optparse.OptionParser() add_task_option(parser) if task_cls_name not in Register.get_reg(): raise Exception('Error: %s is not a valid tasks (must be %s)' % (task_cls_name, tasks_str)) # Register all parameters as a big mess task_cls = Register.get_reg()[task_cls_name] if task_cls == Register.AMBIGUOUS_CLASS: raise Exception('%s is ambiguous' % task_cls_name) params = task_cls.get_nonglobal_params() for param_name, param in global_params: _add_parameter(parser, param_name, param) for param_name, param in params: _add_parameter(parser, param_name, param) # Parse and run options, args = parser.parse_args(args=cmdline_args) params = {} for k, v in vars(options).iteritems(): if k != 'task': params[k] = v task = task_cls.from_input(params, global_params) return [task]
def parse(self, cmdline_args=None, main_task_cls=None): import argparse parser = argparse.ArgumentParser() def _add_parameter(parser, param_name, param, prefix=None): description = [] if prefix: description.append('%s.%s' % (prefix, param_name)) else: description.append(param_name) if param.description: description.append(param.description) if param.has_default: description.append(" [default: %s]" % (param.default, )) if param.is_list: action = "append" elif param.is_boolean: action = "store_true" else: action = "store" parser.add_argument('--' + param_name.replace('_', '-'), help=' '.join(description), default=None, action=action) def _add_task_parameters(parser, cls): for param_name, param in cls.get_nonglobal_params(): _add_parameter(parser, param_name, param, cls.task_family) def _add_global_parameters(parser): for param_name, param in Register.get_global_params(): _add_parameter(parser, param_name, param) _add_global_parameters(parser) if main_task_cls: _add_task_parameters(parser, main_task_cls) else: subparsers = parser.add_subparsers(dest='command') for name, cls in Register.get_reg().iteritems(): subparser = subparsers.add_parser(name) if cls == Register.AMBIGUOUS_CLASS: continue _add_task_parameters(subparser, cls) # Add global params here as well so that we can support both: # test.py --global-param xyz Test --n 42 # test.py Test --n 42 --global-param xyz _add_global_parameters(subparser) args = parser.parse_args(args=cmdline_args) params = vars(args) # convert to a str -> str hash if main_task_cls: task_cls = main_task_cls else: task_cls = Register.get_reg()[args.command] if task_cls == Register.AMBIGUOUS_CLASS: raise Exception('%s is ambigiuous' % args.command) # Notice that this is not side effect free because it might set global params task = task_cls.from_input(params, Register.get_global_params()) return [task]