def _create_run(self, command_name=None, config_updates=None, named_configs=(), meta_info=None, options=None): command_name = command_name or self.default_command if command_name is None: raise RuntimeError('No command found to be run. Specify a command ' 'or define a main function.') default_options = self.get_default_options() if options: default_options.update(options) options = default_options # call option hooks for oh in self.option_hooks: oh(options=options) run = create_run(self, command_name, config_updates, named_configs=named_configs, force=options.get(ForceOption.get_flag(), False)) run.meta_info['command'] = command_name run.meta_info['options'] = options if meta_info: run.meta_info.update(meta_info) for option in gather_command_line_options(): option_value = options.get(option.get_flag(), False) if option_value: option.apply(option_value, run) self.current_run = run return run
def parse_args(argv, description="", commands=None, print_help=True): """ Parse the given commandline-arguments. :param argv: list of command-line arguments as in ``sys.argv`` :type argv: list[str] :param description: description of the experiment (docstring) to be used in the help text. :type description: str :param commands: list of commands that are supported by this experiment :type commands: dict[str, func] | None :param print_help: if True (default) this function will print the help-text and exit if that is required by the parsed arguments. :type print_help: bool :return: parsed values for all command-line options. See ``docopt`` for more details. :rtype: dict[str, str | bool | None] """ options = gather_command_line_options() usage = _format_usage(argv[0], description, commands, options) args = docopt(usage, [str(a) for a in argv[1:]], help=print_help) if not args['help'] or not print_help: return args if args['COMMAND'] is None: print(usage) sys.exit() else: print(help_for_command(commands[args['COMMAND']])) sys.exit()
def run_command(self, command_name, config_updates=None, named_configs=(), args=()): """Run the command with the given name. :param command_name: Name of the command to be run :type command_name: str :param config_updates: a dictionary of parameter values that should be updates (optional) :type config_updates: dict :param named_configs: list of names of named configurations to use (optional) :type named_configs: list[str] :param args: dictionary of command-line options :type args: dict :returns: the Run object corresponding to the finished run :rtype: sacred.run.Run """ force_flag = '--' + ForceOption.get_flag()[1] force = args[force_flag] if force_flag in args else False run = self._create_run_for_command(command_name, config_updates, named_configs, force=force) self.current_run = run for option in gather_command_line_options(): op_name = '--' + option.get_flag()[1] if op_name in args and args[op_name]: option.apply(args[op_name], run) self.current_run.run_logger.info("Running command '%s'", command_name) run() self.current_run = None return run
def _create_run(self, command_name=None, config_updates=None, named_configs=(), meta_info=None, options=None): command_name = command_name or self.default_command if command_name is None: raise RuntimeError('No command found to be run. Specify a command ' 'or define a main function.') default_options = self.get_default_options() if options: default_options.update(options) options = default_options # call option hooks for oh in self.option_hooks: oh(options=options) run = create_run(self, command_name, config_updates, named_configs=named_configs, force=options.get(ForceOption.get_flag(), False), log_level=options.get(LoglevelOption.get_flag(), None)) run.meta_info['command'] = command_name run.meta_info['options'] = options if meta_info: run.meta_info.update(meta_info) for option in gather_command_line_options(): option_value = options.get(option.get_flag(), False) if option_value: option.apply(option_value, run) self.current_run = run return run
def test_parse_individual_arguments(argv, expected): options = gather_command_line_options() usage = format_usage("test.py", "", {}, options) argv = shlex.split(argv) plain = docopt(usage, [], help=False) args = docopt(usage, argv, help=False) plain.update(expected) assert args == plain
def get_usage(self, program_name=None): """Get the commandline usage string for this experiment.""" program_name = program_name or sys.argv[0] commands = OrderedDict(self.gather_commands()) options = gather_command_line_options() long_usage = format_usage(program_name, self.doc, commands, options) short_usage = printable_usage(long_usage) return short_usage, long_usage
def get_usage(self, program_name=None): """Get the commandline usage string for this experiment.""" program_name = os.path.relpath(program_name or sys.argv[0] or 'Dummy', self.base_dir) commands = OrderedDict(self.gather_commands()) options = gather_command_line_options() long_usage = format_usage(program_name, self.doc, commands, options) # internal usage is a workaround because docopt cannot handle spaces # in program names. So for parsing we use 'dummy' as the program name. # for printing help etc. we want to use the actual program name. internal_usage = format_usage('dummy', self.doc, commands, options) short_usage = printable_usage(long_usage) return short_usage, long_usage, internal_usage
def _create_run( self, command_name=None, config_updates=None, named_configs=(), info=None, meta_info=None, options=None, ): command_name = command_name or self.default_command if command_name is None: raise RuntimeError("No command found to be run. Specify a command " "or define a main function.") default_options = self.get_default_options() if options: default_options.update(options) options = default_options # call option hooks for oh in self.option_hooks: oh(options=options) run = create_run( self, command_name, config_updates, named_configs=named_configs, force=options.get(ForceOption.get_flag(), False), log_level=options.get(loglevel_option.get_flag(), None), ) if info is not None: run.info.update(info) run.meta_info["command"] = command_name run.meta_info["options"] = options if meta_info: run.meta_info.update(meta_info) options_list = gather_command_line_options( ) + self.additional_cli_options for option in options_list: option_value = options.get(option.get_flag(), False) if option_value: option.apply(option_value, run) self.current_run = run return run