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
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)
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
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)
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))
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,)))
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))
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, )))
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))
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)
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))
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)
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)
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)
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)
def test_missing_option(self): def main(a): return a with self.assertRaises(cmdline.CommandLineError): value = cmdline.apply_options(main, self.opts)
def test_variable_keywords(self): def main(**kwargs): return dict(args) with self.assertRaises(cmdline.CommandLineError): value = cmdline.apply_options(main, self.opts)
def test_void_function(self): def main(): return Ellipsis value = cmdline.apply_options(main, self.opts) self.assertIs(value, Ellipsis)