예제 #1
0
    def start(self, args=None):
        """Begin command line program

        By default will use the command line arguments passed when Python was
        initially started. New arguments can be passed through the args
        parameter.
        """
        commands = [[]]
        args = args if args is not None else sys.argv[1:]
        if len(args) > 0:
            commands = [
                list(group) for key, group in itertools.groupby(
                    args, lambda x: x == self._cmd_delim) if not key
            ]
        options = []
        for command in commands:
            opts = self._parser.parse_args(command)
            options.append(opts)
        context.clear()
        context.opts_next = tuple(options)
        for count, opts in enumerate(options):
            context.opts_next = context.opts_next[1:]
            context.opts_current = opts
            cmdline.apply_options(self.__wrapped__,
                                  opts,
                                  run_main=(count == 0),
                                  sub_group=self._group,
                                  collector=self._collector)
            context.opts_previous += (opts, )
        context.opts_current = None
        return context.last_return
예제 #2
0
    def test_missing_required(self, exit, stderr):
        def main(a):
            pass

        self.opts.a = cmdline.NODEFAULT
        cmdline.apply_options(main, self.opts)
        self.assertTrue(exit.called)
예제 #3
0
    def test_missing_required(self, exit, stderr):
        def main(a):
            pass

        self.opts.a = cmdline.NODEFAULT
        cmdline.apply_options(main, self.opts)
        self.assertTrue(exit.called)
예제 #4
0
파일: main.py 프로젝트: aliles/begins
    def start(self, args=None):
        """Begin command line program

        By default will use the command line arguments passed when Python was
        initially started. New arguments can be passed through the args
        parameter.
        """
        commands = [[]]
        args = args if args is not None else sys.argv[1:]
        if len(args) > 0:
            commands = [list(group) for key, group in itertools.groupby(args, lambda x: x == self._cmd_delim) if not key]
        options = []
        for command in commands:
            opts = self._parser.parse_args(command)
            options.append(opts)
        context.clear()
        context.opts_next = tuple(options)
        for count, opts in enumerate(options):
            context.opts_next = context.opts_next[1:]
            context.opts_current = opts
            cmdline.apply_options(self.__wrapped__, opts,
                    run_main=(count==0), sub_group=self._group,
                    collector=self._collector)
            context.opts_previous += (opts,)
        context.opts_current = None
        return context.last_return
예제 #5
0
 def test_tracebacks(self, enable):
     @extensions.tracebacks
     def main():
         return
     self.opts.tracebacks = True
     self.opts.tbdir = None
     cmdline.apply_options(main, self.opts)
     enable.assert_called_one(format='txt', logdir=None)
예제 #6
0
    def test_tracebacks(self, enable):
        @extensions.tracebacks
        def main():
            return

        self.opts.tracebacks = True
        self.opts.tbdir = None
        cmdline.apply_options(main, self.opts)
        enable.assert_called_one(format='txt', logdir=None)
예제 #7
0
    def test_variable_arguments(self):
        def main(*args):
            return tuple(args)

        self.opts.args = [1, 2, 3]
        value = cmdline.apply_options(main, self.opts)
        self.assertTupleEqual(value, (1, 2, 3))
예제 #8
0
    def test_variable_arguments(self):
        def main(*args):
            return tuple(args)

        self.opts.args = [1, 2, 3]
        value = cmdline.apply_options(main, self.opts)
        self.assertTupleEqual(value, (1, 2, 3))
예제 #9
0
 def test_positional_with_variable_arguments(self):
     def main(a, *b):
         return (a, b)
     self.opts.a = 1
     self.opts.b = [2]
     value = cmdline.apply_options(main, self.opts)
     self.assertTupleEqual(value, (1, (2,)))
예제 #10
0
 def test_positional_argument(self):
     def main(a, b,c):
         return (a, b, c)
     self.opts.a = 1
     self.opts.b = 2
     self.opts.c = 3
     value = cmdline.apply_options(main, self.opts)
     self.assertTupleEqual(value, (1, 2, 3))
예제 #11
0
    def test_positional_with_variable_arguments(self):
        def main(a, *b):
            return (a, b)

        self.opts.a = 1
        self.opts.b = [2]
        value = cmdline.apply_options(main, self.opts)
        self.assertTupleEqual(value, (1, (2, )))
예제 #12
0
 def test_keyword_argument(self):
     def main(a=None, b=None, c=None):
         return (a, b, c)
     self.opts.a = 1
     self.opts.b = 2
     self.opts.c = 3
     value = cmdline.apply_options(main, self.opts)
     self.assertEqual(value, (1, 2, 3))
예제 #13
0
 def test_subcommand_group(self):
     @subcommands.subcommand(group='named.collector')
     def subcmd():
         return 'blue'
     def main():
         return 'red'
     self.opts._subcommand = 'subcmd'
     value = cmdline.apply_options(main, self.opts, sub_group='named.collector')
     self.assertEqual('blue', value)
예제 #14
0
    def test_keyword_argument(self):
        def main(a=None, b=None, c=None):
            return (a, b, c)

        self.opts.a = 1
        self.opts.b = 2
        self.opts.c = 3
        value = cmdline.apply_options(main, self.opts)
        self.assertEqual(value, (1, 2, 3))
예제 #15
0
    def test_positional_argument(self):
        def main(a, b, c):
            return (a, b, c)

        self.opts.a = 1
        self.opts.b = 2
        self.opts.c = 3
        value = cmdline.apply_options(main, self.opts)
        self.assertTupleEqual(value, (1, 2, 3))
예제 #16
0
 def test_subcommand(self):
     @subcommands.subcommand
     def subcmd():
         return 'blue'
     def main():
         return 'red'
     self.opts._subcommand = 'subcmd'
     value = cmdline.apply_options(main, self.opts)
     self.assertEqual('blue', value)
예제 #17
0
    def test_subcommand(self):
        @subcommands.subcommand
        def subcmd():
            return 'blue'

        def main():
            return 'red'

        self.opts._subcommand = 'subcmd'
        value = cmdline.apply_options(main, self.opts)
        self.assertEqual('blue', value)
예제 #18
0
    def test_subcommand(self):
        @subcommands.subcommand
        def subcmd():
            return "blue"

        def main():
            return "red"

        self.opts._subcommand = "subcmd"
        value = cmdline.apply_options(main, self.opts)
        self.assertEqual("blue", value)
예제 #19
0
    def test_subcommand_group(self):
        @subcommands.subcommand(group="named.collector")
        def subcmd():
            return "blue"

        def main():
            return "red"

        self.opts._subcommand = "subcmd"
        value = cmdline.apply_options(main, self.opts, sub_group="named.collector")
        self.assertEqual("blue", value)
예제 #20
0
    def test_subcommand_group(self):
        @subcommands.subcommand(group='named.collector')
        def subcmd():
            return 'blue'

        def main():
            return 'red'

        self.opts._subcommand = 'subcmd'
        value = cmdline.apply_options(main,
                                      self.opts,
                                      sub_group='named.collector')
        self.assertEqual('blue', value)
예제 #21
0
파일: main.py 프로젝트: pombredanne/begins
    def start(self, args=None):
        """Begin command line program

        By default will use the command line arguments passed when Python was
        initially started. New arguments can be passed through the args
        parameter.
        """
        try:
            if self._parser is None:
                return self.__wrapped__()
            opts = self._parser.parse_args(args)
            return cmdline.apply_options(self.__wrapped__, opts,
                    sub_group=self._group, collector=self._collector)
        except KeyboardInterrupt:
            sys.exit(1)
예제 #22
0
    def test_missing_option(self):
        def main(a):
            return a

        with self.assertRaises(cmdline.CommandLineError):
            value = cmdline.apply_options(main, self.opts)
예제 #23
0
    def test_variable_keywords(self):
        def main(**kwargs):
            return dict(args)

        with self.assertRaises(cmdline.CommandLineError):
            value = cmdline.apply_options(main, self.opts)
예제 #24
0
    def test_void_function(self):
        def main():
            return Ellipsis

        value = cmdline.apply_options(main, self.opts)
        self.assertIs(value, Ellipsis)
예제 #25
0
    def test_void_function(self):
        def main():
            return Ellipsis

        value = cmdline.apply_options(main, self.opts)
        self.assertIs(value, Ellipsis)
예제 #26
0
    def test_variable_keywords(self):
        def main(**kwargs):
            return dict(args)

        with self.assertRaises(cmdline.CommandLineError):
            value = cmdline.apply_options(main, self.opts)
예제 #27
0
    def test_missing_option(self):
        def main(a):
            return a

        with self.assertRaises(cmdline.CommandLineError):
            value = cmdline.apply_options(main, self.opts)