示例#1
0
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()
示例#2
0
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()
示例#3
0
def test_help_for_command():
    def my_command():
        """This is my docstring"""
        pass

    help_text = help_for_command(my_command)
    assert "my_command" in help_text
    assert "This is my docstring" in help_text
示例#4
0
def test_help_for_command():
    def my_command():
        """This is my docstring"""
        pass

    help_text = help_for_command(my_command)
    assert help_text.find("my_command") > -1
    assert help_text.find("This is my docstring") > -1
示例#5
0
def test_help_for_command():
    def my_command():
        """This is my docstring"""
        pass

    help_text = help_for_command(my_command)
    assert help_text.find("my_command") > -1
    assert help_text.find("This is my docstring") > -1
示例#6
0
def test_help_for_command():
    def my_command():
        """This is my docstring"""
        pass

    help_text = help_for_command(my_command)
    assert "my_command" in help_text
    assert "This is my docstring" in help_text
示例#7
0
 def _handle_help(self, args, usage):
     if args['help'] or args['--help']:
         if args['COMMAND'] is None:
             print(usage)
             return True
         else:
             commands = dict(self.gather_commands())
             print(help_for_command(commands[args['COMMAND']]))
             return True
     return False
示例#8
0
 def _handle_help(self, args, usage):
     if args['help'] or args['--help']:
         if args['COMMAND'] is None:
             print(usage)
             return True
         else:
             commands = dict(self.gather_commands())
             print(help_for_command(commands[args['COMMAND']]))
             return True
     return False
示例#9
0
def parse_args(argv, description="", commands=None, print_help=True):
    usage = _format_usage(argv[0], description, commands)
    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()
示例#10
0
def parse_args(argv, description="", commands=None, print_help=True):
    usage = _format_usage(argv[0], description, commands)
    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()
示例#11
0
    def run_commandline(self, argv=None):
        """
        Run the command-line interface of this experiment.

        If ``argv`` is omitted it defaults to ``sys.argv``.

        Parameters
        ----------
        argv : list[str] or str, optional
            Command-line as string or list of strings like ``sys.argv``.

        Returns
        -------
        sacred.run.Run
            The Run object corresponding to the finished run.

        """
        if argv is None:
            argv = sys.argv
        elif isinstance(argv, basestring):
            argv = shlex.split(argv)
        else:
            if not isinstance(argv, (list, tuple)):
                raise ValueError("argv must be str or list, but was {}"
                                 .format(type(argv)))
            if not all([isinstance(a, basestring) for a in argv]):
                problems = [a for a in argv if not isinstance(a, basestring)]
                raise ValueError("argv must be list of str but contained the "
                                 "following elements: {}".format(problems))

        short_usage, usage = self.get_usage()
        args = docopt(usage, [str(a) for a in argv[1:]], help=False)
        commands = OrderedDict(self.gather_commands())
        cmd_name = args.get('COMMAND') or self.default_command
        config_updates, named_configs = get_config_updates(args['UPDATE'])

        if cmd_name is not None and cmd_name not in commands:
            print(short_usage)
            print('Error: Command "{}" not found. Available commands are: '
                  '{}'.format(cmd_name, ", ".join(commands.keys())))
            exit(1)

        if args['help'] or args['--help']:
            if args['COMMAND'] is None:
                print(usage)
            else:
                print(help_for_command(commands[args['COMMAND']]))
            exit()

        if cmd_name is None:
            print(short_usage)
            print('Error: No command found to be run. Specify a command'
                  ' or define main function. Available commands'
                  ' are: {}'.format(", ".join(commands.keys())))
            exit(1)

        try:
            return self.run(cmd_name, config_updates, named_configs, {}, args)
        except Exception:
            if not self.current_run or self.current_run.debug:
                raise
            elif self.current_run.pdb:
                import traceback
                import pdb
                traceback.print_exception(*sys.exc_info())
                pdb.post_mortem()
            else:
                print_filtered_stacktrace()
                exit(1)