def test_custom_param_help(self): class CustParam(parser.OptionParameter): def show_help(self, desc, after, f, cols): cols.append('I am', 'a custom parameter!') f.append("There is stuff after me.") f.append(desc) f.extend(after) func = f("*, param: a", locals={'a': parser.use_class(named=CustParam)}) func.__doc__ = """ param: Param desc After param _:_ """ r = runner.Clize(func) self._do_test( r, ['func --param=STR', USAGE_HELP], """ Usage: func [OPTIONS] Options: I am a custom parameter! There is stuff after me. Param desc After param Other actions: -h, --help Show the help """)
def _test_func(self, sig, doc, help_str): func = f(sig, pre="from clize import Parameter as P") func.__doc__ = doc r = runner.Clize(func) h = help.ClizeHelp(r, None) h.prepare() p_help_str = str(h.show('func')) self.assertEqual(help_str, p_help_str)
def _test_func(self, sig, wrapper_sigs, doc, wrapper_docs, help_str): ifunc = f(sig, pre="from clize import Parameter") ifunc.__doc__ = doc func = ifunc for sig, doc in zip(wrapper_sigs, wrapper_docs): wfunc = f('func, ' + sig, pre="from clize import Parameter") wfunc.__doc__ = doc func = wrapper_decorator(wfunc)(func) r = runner.Clize(func) h = help.ClizeHelp(r, None) h.prepare() p_help_str = str(h.show('func')) self.assertEqual(help_str.split(), p_help_str.split())
def test_nonclize_alt(self): @runner.Clize.as_is def alt(script): raise NotImplementedError def func(): raise NotImplementedError r = runner.Clize(func, alt=alt) self._do_test(r, ['func', USAGE_HELP, 'func --alt [args...]'], """ Usage: func Other actions: --alt -h, --help Show the help """)
def _test_func(self, sig, doc, help_str): try: backw = util.get_terminal_width except AttributeError: backw = None try: util.get_terminal_width = lambda: 80 func = f(sig, pre="from clize import Parameter as P") func.__doc__ = doc r = runner.Clize(func) h = help.ClizeHelp(r, None) h.prepare() p_help_str = str(h.show('func')) self.assertEqual(help_str, p_help_str) finally: if backw is not None: util.get_terminal_width = backw
def _clize(fn, alias={}, force_positional=(), coerce={}, require_excess=False, extra=(), use_kwoargs=None): sig = specifiers.signature(fn) has_kwoargs = False annotations = defaultdict(list) ann_positional = [] for param in sig.parameters.values(): coerce_set = False if param.kind == param.KEYWORD_ONLY: has_kwoargs = True elif param.kind == param.VAR_KEYWORD: annotations[param.name].append(parser.Parameter.IGNORE) elif require_excess and param.kind == param.VAR_POSITIONAL: annotations[param.name].append(parser.Parameter.REQUIRED) if param.annotation != param.empty: for thing in util.maybe_iter(param.annotation): if thing == clize.POSITIONAL: ann_positional.append(param.name) continue elif callable(thing): coerce_set = True thing = _convert_coerce(thing) annotations[param.name].append(thing) try: func = coerce[param.name] except KeyError: pass else: annotations[param.name].append(_convert_coerce(func)) coerce_set = True annotations[param.name].extend(alias.get(param.name, ())) if not coerce_set and param.default != param.empty: annotations[param.name].append(_convert_coerce(type( param.default))) fn = modifiers.annotate(**annotations)(fn) use_kwoargs = has_kwoargs if use_kwoargs is None else use_kwoargs if not use_kwoargs: fn = modifiers.autokwoargs( exceptions=chain(ann_positional, force_positional))(fn) return runner.Clize(fn, extra=extra)
class Cls(object): method = runner.Clize(NoGet())
class Cls(object): method = runner.Clize(SelfGet())
def _test_func(self, sig, doc, usage, help_str): func = f(sig, pre="from clize import Parameter as P") func.__doc__ = doc r = runner.Clize(func) self._do_test(r, usage, help_str)