示例#1
0
def main(argv=sys.argv):
    # If no arguments have been passed be helpful and point out --help.
    snap_setup()

    if len(argv) == 1:
        sys.stderr.write("Error: no arguments given.\n"
                         "Run %s --help for usage details.\n" % argv[0])
        raise SystemExit(2)

    parser = prepare_parser(argv)

    try:
        options = parser.parse_args(argv[1:])
        if hasattr(options, "execute"):
            options.execute(options)
        else:
            # This mimics the error behaviour provided by argparse 1.1 from
            # PyPI (which differs from argparse 1.1 in the standard library).
            parser.error("too few arguments")
    except KeyboardInterrupt:
        raise SystemExit(1)
    except SystemExit:
        raise  # Pass-through.
    except Exception as error:
        show = getattr(error, "always_show", False)
        if options.debug or show:
            raise
        else:
            # Note: this will call sys.exit() when finished.
            parser.error("%s" % error)
示例#2
0
def main(argv=None):
    # Set up the process's locale; this helps bzrlib decode command-line
    # arguments in the next step.
    locale.setlocale(locale.LC_ALL, "")
    if argv is None:
        argv = sys.argv[:1] + osutils.get_unicode_argv()

    if len(argv) == 1:
        # No arguments passed.  Be helpful and point out the --help option.
        sys.stderr.write(
            "Error: no arguments given.\n"
            "Run %s --help for usage details.\n"
            % argv[0])
        raise SystemExit(2)

    parser = prepare_parser(argv)

    # Run, doing polite things with exceptions.
    try:
        options = parser.parse_args(argv[1:])
        options.execute(options)
    except KeyboardInterrupt:
        raise SystemExit(1)
    except StandardError as error:
        parser.error("%s" % error)
示例#3
0
 def test_bad_arguments_calls_sys_exit_2(self):
     argv = ["maas", factory.make_name(prefix="profile"), "nodes"]
     parser = prepare_parser(argv)
     self.patch(ArgumentParser, "print_help")
     mock_exit = self.patch(sys.exit)
     self.patch(ArgumentParser, "_print_error")
     # We need to catch this TypeError, because after our overridden error()
     # method is called, argparse expects the system to exit. Without
     # catching it, when we mock sys.exit() it will continue unexpectedly
     # and crash with the TypeError later.
     try:
         parser.parse_args(argv[1:])
     except TypeError:
         pass
     self.assertThat(mock_exit, MockCalledOnceWith(2))
示例#4
0
 def test_bad_arguments_prints_help_to_stderr(self):
     argv = ['maas', factory.make_name(prefix="profile"), 'nodes']
     parser = prepare_parser(argv)
     mock_print_help = self.patch(ArgumentParser, 'print_help')
     self.patch(sys.exit)
     self.patch(ArgumentParser, '_print_error')
     # We need to catch this TypeError, because after our overridden error()
     # method is called, argparse expects the system to exit. Without
     # catching it, when we mock sys.exit() it will continue unexpectedly
     # and crash with the TypeError later.
     try:
         parser.parse_args(argv[1:])
     except TypeError:
         pass
     self.assertThat(mock_print_help, MockCalledOnceWith(sys.stderr))
示例#5
0
def main(argv=sys.argv):
    # If no arguments have been passed be helpful and point out --help.
    snap_setup()

    parser = prepare_parser(argv)

    try:
        options = parser.parse_args(argv[1:])
        if hasattr(options, "execute"):
            options.execute(options)
        else:
            sub_parser = get_deepest_subparser(parser, argv[1:])
            # This mimics the error behaviour provided by argparse 1.1 from
            # PyPI (which differs from argparse 1.1 in the standard library).
            sub_parser.error("too few arguments")
    except KeyboardInterrupt:
        raise SystemExit(1)
    except Exception as error:
        show = getattr(error, "always_show", False)
        if options.debug or show:
            raise
        else:
            # Note: this will call sys.exit() when finished.
            parser.error("%s" % error)