Generate an Argparse call from a function, then call this function. Notes: - for the arguments to have a description, the sphinx docstring format must be used. See https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html - the arguments must be typed in Pyt
(
func: DecoratorCallable,
_parseonly: bool = False,
)
| 3880 | |
| 3881 | |
| 3882 | def AutoArgparse( |
| 3883 | func: DecoratorCallable, |
| 3884 | _parseonly: bool = False, |
| 3885 | ) -> Optional[Tuple[List[str], List[str]]]: |
| 3886 | """ |
| 3887 | Generate an Argparse call from a function, then call this function. |
| 3888 | |
| 3889 | Notes: |
| 3890 | |
| 3891 | - for the arguments to have a description, the sphinx docstring format |
| 3892 | must be used. See |
| 3893 | https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html |
| 3894 | - the arguments must be typed in Python (we ignore Sphinx-specific types) |
| 3895 | untyped arguments are ignored. |
| 3896 | - only types that would be supported by argparse are supported. The others |
| 3897 | are omitted. |
| 3898 | """ |
| 3899 | argsdoc = {} |
| 3900 | if func.__doc__: |
| 3901 | # Sphinx doc format parser |
| 3902 | m = re.match( |
| 3903 | r"((?:.|\n)*?)(\n\s*:(?:param|type|raises|return|rtype)(?:.|\n)*)", |
| 3904 | func.__doc__.strip(), |
| 3905 | ) |
| 3906 | if not m: |
| 3907 | desc = func.__doc__.strip() |
| 3908 | else: |
| 3909 | desc = m.group(1) |
| 3910 | sphinxargs = re.findall( |
| 3911 | r"\s*:(param|type|raises|return|rtype)\s*([^:]*):(.*)", |
| 3912 | m.group(2), |
| 3913 | ) |
| 3914 | for argtype, argparam, argdesc in sphinxargs: |
| 3915 | argparam = argparam.strip() |
| 3916 | argdesc = argdesc.strip() |
| 3917 | if argtype == "param": |
| 3918 | if not argparam: |
| 3919 | raise ValueError(":param: without a name !") |
| 3920 | argsdoc[argparam] = argdesc |
| 3921 | else: |
| 3922 | desc = "" |
| 3923 | |
| 3924 | # Process the parameters |
| 3925 | positional = [] |
| 3926 | noargument = [] |
| 3927 | hexarguments = [] |
| 3928 | parameters = {} |
| 3929 | for param in inspect.signature(func).parameters.values(): |
| 3930 | if not param.annotation: |
| 3931 | continue |
| 3932 | noarg = False |
| 3933 | parname = param.name.replace("_", "-") |
| 3934 | paramkwargs: Dict[str, Any] = {} |
| 3935 | if param.annotation is bool: |
| 3936 | if param.default is True: |
| 3937 | parname = "no-" + parname |
| 3938 | paramkwargs["action"] = "store_false" |
| 3939 | else: |
no test coverage detected