def _generate_command(self, func, name=None, *args, **kwargs): '''Generate argparse's subparser. :param func: The function to analyze. :param name: If given, a different name for the command. The default one is ``func.__name__``.''' func_name = func.__name__ name = func_name.replace("_", "-") if name is None else name argspec = inspect.getargspec(func) self.argspecs[func_name] = argspec argz = izip_longest(reversed(argspec.args), reversed(argspec.defaults or []), fillvalue=_POSITIONAL()) argz = reversed(list(argz)) doc = (inspect.getdoc(func) or '').strip() + '\n' cmd_help, cmd_desc = split_doc(purify_doc(doc)) subparser = self.subparsers.add_parser(name, help=cmd_help or None, description=cmd_desc or None, **kwargs) for a, kw in self.analyze_func(func, doc, argz, argspec.varargs): a = tuple(option.replace("_", "-") for option in a) a = tuple( option.replace("--", "--no-" ) if kw.get('default') is True else option for option in a) subparser.add_argument(*a, **purify_kwargs(kw)) subparser.set_defaults(**{_DISPATCH_TO: func}) return func
def _generate_command(self, func, name=None, doctype='rest', *args, **kwargs): '''Generate argparse's subparser. :param func: The function to analyze. :param name: If given, a different name for the command. The default one is ``func.__name__``.''' func_name = func.__name__ name = func_name if name is None else name argspec = getfullargspec(func) self._argspecs[func_name] = argspec argz = izip_longest(reversed(argspec.args), reversed(argspec.defaults or []), fillvalue=_POSITIONAL()) argz = reversed(list(argz)) doc = (inspect.getdoc(func) or '').strip() + '\n' if doctype == 'numpy': config = Config(napoleon_google_docstring=False, napoleon_use_rtype=False) doc = str(NumpyDocstring(doc, config)) elif doctype == 'google': config = Config(napoleon_numpy_docstring=False, napoleon_use_rtype=False) doc = str(GoogleDocstring(doc, config)) elif doctype == 'rest': pass else: raise ValueError('doctype must be one of "numpy", "google", ' 'or "rest"') cmd_help, cmd_desc = split_doc(purify_doc(doc)) subparser = self._subparsers.add_parser(name, help=cmd_help or None, description=cmd_desc or None, **kwargs) params = find_param_docs(doc) for a, kw in self._analyze_func(func, params, argz, argspec.varargs): completer = kw.pop('completer', None) arg = subparser.add_argument(*a, **purify_kwargs(kw)) if completer is not None: arg.completer = completer subparser.set_defaults(**{_DISPATCH_TO: func}) return func
def _generate_command(self, func, name=None, doctype='rest', *args, **kwargs): '''Generate argparse's subparser. :param func: The function to analyze. :param name: If given, a different name for the command. The default one is ``func.__name__``.''' func_name = func.__name__ name = func_name if name is None else name argspec = inspect.getargspec(func) self._argspecs[func_name] = argspec argz = izip_longest(reversed(argspec.args), reversed(argspec.defaults or []), fillvalue=_POSITIONAL()) argz = reversed(list(argz)) doc = (inspect.getdoc(func) or '').strip() + '\n' if doctype == 'numpy': config = Config(napoleon_google_docstring=False, napoleon_use_rtype=False) doc = str(NumpyDocstring(doc, config)) elif doctype == 'google': config = Config(napoleon_numpy_docstring=False, napoleon_use_rtype=False) doc = str(GoogleDocstring(doc, config)) elif doctype == 'rest': pass else: raise ValueError('doctype must be one of "numpy", "google", ' 'or "rest"') cmd_help, cmd_desc = split_doc(purify_doc(doc)) subparser = self._subparsers.add_parser(name, help=cmd_help or None, description=cmd_desc or None, **kwargs) params = find_param_docs(doc) for a, kw in self._analyze_func(func, params, argz, argspec.varargs): completer = kw.pop('completer', None) arg = subparser.add_argument(*a, **purify_kwargs(kw)) if completer is not None: arg.completer = completer subparser.set_defaults(**{_DISPATCH_TO: func}) return func
def _generate_command(self, func, name=None, *args, **kwargs): """Generate argparse's subparser. :param func: The function to analyze. :param name: If given, a different name for the command. The default one is ``func.__name__``.""" func_name = func.__name__ name = func_name if name is None else name argspec = inspect.getargspec(func) self.argspecs[func_name] = argspec argz = izip_longest(reversed(argspec.args), reversed(argspec.defaults or []), fillvalue=_POSITIONAL()) argz = reversed(list(argz)) doc = (inspect.getdoc(func) or "").strip() + "\n" cmd_help, cmd_desc = split_doc(purify_doc(doc)) subparser = self.subparsers.add_parser(name, help=cmd_help or None, description=cmd_desc or None, **kwargs) for a, kw in self.analyze_func(func, doc, argz, argspec.varargs): subparser.add_argument(*a, **purify_kwargs(kw)) subparser.set_defaults(**{_DISPATCH_TO: func}) return func
def _generate_command(self, func, name=None, doctype='rest', *args, **kwargs): '''Generate argparse's subparser. :param func: The function to analyze. :param name: If given, a different name for the command. The default one is ``func.__name__``.''' name = name or func.__name__ doc = (inspect.getdoc(func) or '').strip() + '\n' if doctype == 'numpy': config = Config(napoleon_google_docstring=False, napoleon_use_rtype=False) doc = str(NumpyDocstring(doc, config)) elif doctype == 'google': config = Config(napoleon_numpy_docstring=False, napoleon_use_rtype=False) doc = str(GoogleDocstring(doc, config)) elif doctype == 'rest': pass else: raise ValueError('doctype must be one of "numpy", "google", ' 'or "rest"') cmd_help, cmd_desc = split_doc(purify_doc(doc)) subparser = self._subparsers.add_parser(name, help=cmd_help or None, description=cmd_desc or None, **kwargs) doc_params = find_param_docs(doc) self._signatures[func.__name__] = signature(func) for a, kw in self._analyze_func(func, doc_params): completer = kw.pop('completer', None) arg = subparser.add_argument(*a, **purify_kwargs(kw)) if completer is not None: arg.completer = completer subparser.set_defaults(**{_DISPATCH_TO: func}) return func