def test_stdout_special_argument(self): def upper(stdin=None, stdout=None): stdout.write(stdin.read().upper()) stdout = FakeStdout() optfn.run(upper, stdin=FakeStdin(), stdout=stdout) self.assertEqual(stdout.written, 'HELLO')
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_multiple_invalid_subcommand(self): "With multiple subcommands, invalid first arg should raise an error" def one(arg): pass def two(arg): pass def three(arg): pass # Invalid first argument should raise an error e = StringIO() res = optfn.run([one, two], ['three'], stderr=e) self.assertEqual(res, optfn.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one' or 'two'") e = StringIO() res = optfn.run([one, two, three], ['four'], stderr=e) self.assertEqual(res, optfn.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one', 'three' or 'two'") # No argument at all should raise an error e = StringIO() res = optfn.run([one, two, three], [], stderr=e) self.assertEqual(res, optfn.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one', 'three' or 'two'")
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_stderr_special_argument(self): def bad_upper(stderr): stderr.write('an error') def good_upper(stderr=None): stderr.write('an error') stderr = FakeStdout() optfn.run(bad_upper, stderr=stderr) self.assertEqual(stderr.written.strip(), 'Required 1 arguments, got 0') stderr = FakeStdout() optfn.run(good_upper, stderr=stderr) self.assertEqual(stderr.written, 'an error')
def test_notstrict(self): "@notstrict tells optfn to tolerate missing required arguments" def strict_func(one): pass e = StringIO() optfn.run(strict_func, [], stderr=e) self.assertEqual(e.getvalue().strip(), 'Required 1 arguments, got 0') @optfn.notstrict def notstrict_func(one): pass e = StringIO() optfn.run(notstrict_func, [], stderr=e) self.assertEqual(e.getvalue().strip(), '')
def test_return_arguments(self): "Checks that return values of functions are proxied back by run()" def one(): return "one called" def two(): return "two called" e = StringIO() res = optfn.run([one, two], ['one'], stderr=e) self.assertEqual(e.getvalue().strip(), "") self.assertEqual(res, "one called") e = StringIO() res = optfn.run([one, two], ['three'], stderr=e) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one' or 'two'") self.assertEqual(res, optfn.ERROR_RETURN_CODE)
def test_multiple_valid_subcommand_valid_argument(self): "Subcommands with valid arguments should execute as expected" def one(arg): return "one", arg def two(arg): return "two", arg e = StringIO() res = optfn.run([one, two], ['two', 'arg!'], stderr=e) self.assertEqual(e.getvalue().strip(), '') self.assertEqual(res, ('two', 'arg!'))
def test_multiple_valid_subcommand_invalid_argument(self): "Subcommands with invalid arguments should report as such" def one(arg): pass def two(arg): pass e = StringIO() res = optfn.run([one, two], ['one'], stderr=e) self.assertEqual(res, optfn.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), 'one: Required 1 arguments, got 0')
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", ""))
def main(): """Main entry-point for oz's cli""" # Hack to make user code available for import sys.path.append(".") # Run the specified action oz.initialize() retr = optfn.run(list(oz._actions.values())) if retr == optfn.ERROR_RETURN_CODE: sys.exit(-1) elif retr == None: sys.exit(0) elif isinstance(retr, int): sys.exit(retr) else: raise Exception("Unexpected return value from action: %s" % retr)
def test_stdin_special_argument(self): def func(stdin=None): return stdin.read() res = optfn.run(func, stdin=FakeStdin()) self.assertEqual(res, "hello")
> ./lc.py [options] --host=<host> - host running SooperLooper (default: localhost) --port=<port> - SooperLooper port (default: 9951) --loops=<n> - Number of loops to start (default: 2) --udp_host=<host> - UDP host address to connect/bind to (default in settings) --stero - Set if stereo is to be selected --nosetup - Do not re-configure the looper --config=<config> - Config set defined in settings (default: default) --show_config - Show configurations available --help -h - Show this help """ if show_config is True: show_configs() sys.exit(0) # ensure type is correct here port = int(port) loops = int(loops) if udp_host is not None: settings.UDP_HOST = udp_host setup = not nosetup set_config(config) run_controller(host, port, setup, loops, 2 if stereo else 1) if __name__ == '__main__': optfn.run(cli_handler)