def test_options_with_same_initial_use_next_letter(self): def func1(one, version='', verbose=False): pass num_required_args, has_varargs, parser = optfn.func_to_optionparser(func1) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ['-h/--help', '-v/--version', '-e/--verbose']) def func2(one, host=''): pass num_required_args, has_varargs, parser = optfn.func_to_optionparser(func2) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ['-h/--help', '-o/--host'])
def test_varargs(self): def func(one, two, three, *varargs): return "foo", varargs # Should only have the -h help option num_required_args, has_varargs, parser = optfn.func_to_optionparser(func) self.assertEqual(len(parser.option_list), 1) self.assertEqual(str(parser.option_list[0]), '-h/--help') # Should have three required args self.assertEqual(num_required_args, 3) self.assertEqual(has_varargs, True) # Running it with the wrong number of arguments should cause an error for argv in (['one'], ['one', 'two']): e = StringIO() res = optfn.run(func, argv, stderr=e) self.assertTrue('Required 3 or more arguments' in e.getvalue(), e.getvalue()) self.assertEqual(res, optfn.ERROR_RETURN_CODE) # Running it with the right number of arguments should be fine - no varargs e = StringIO() res = optfn.run(func, ['one', 'two', 'three'], stderr=e) self.assertEqual(e.getvalue(), '') self.assertEqual(res, ("foo", ())) # Running it with the right number of arguments should be fine - with varargs e = StringIO() res = optfn.run(func, ['one', 'two', 'three', 'four', 'five'], stderr=e) self.assertEqual(e.getvalue(), '') self.assertEqual(res, ("foo", ("four", "five")))
def test_option_with_hyphens(self): def func2(option_with_hyphens=True): pass num_required_args, has_varargs, parser = optfn.func_to_optionparser(func2) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ['-h/--help', '-o/--option-with-hyphens'])
def test_options_are_correctly_named(self): def func1(one, option='', verbose=False): pass num_required_args, has_varargs, parser = optfn.func_to_optionparser(func1) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ['-h/--help', '-o/--option', '-v/--verbose'])
def test_arghelp(self): "@arghelp('foo', 'help about foo') sets help text for parameters" @optfn.arghelp('foo', 'help about foo') def foo(foo = False): pass num_required_args, has_varargs, parser = optfn.func_to_optionparser(foo) opt = parser.option_list[1] self.assertEqual(str(opt), '-f/--foo') self.assertEqual(opt.help, 'help about foo')
def test_short_option_can_be_named_explicitly(self): def func1(one, option='', q_verbose=False): pass num_required_args, has_varargs, parser = optfn.func_to_optionparser(func1) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ['-h/--help', '-o/--option', '-q/--verbose']) e = StringIO() optfn.run(func1, ['one', '-q'], stderr=e) self.assertEqual(e.getvalue().strip(), '')
def test_one_arg_one_option(self): def func(one, option=''): return (one, option) # Should have -o option as well as -h option num_required_args, has_varargs, parser = optfn.func_to_optionparser(func) self.assertEqual(len(parser.option_list), 2) strs = [str(o) for o in parser.option_list] self.assertTrue('-h/--help' in strs) self.assertTrue('-o/--option' in strs) # Should have one required arg self.assertEqual(num_required_args, 1) self.assertEqual(has_varargs, False) # Should execute res = optfn.run(func, ['the-required', '-o', 'the-option']) self.assertEqual(res, ("the-required", "the-option")) # Option should be optional res = optfn.run(func, ['required2']) self.assertEqual(res, ("required2", ""))