alex-rudakov / sphinx-argparse

Sphinx extension that automatically document argparse commands and options
MIT License
49 stars 40 forks source link

Handling static methods as :func: targets #112

Open dwysocki opened 5 years ago

dwysocki commented 5 years ago

I have a project with a number of command line tools implemented as static methods and class methods. It seems, however, not possible to tell sphinx-argparse how to access these parsers.

Simplified example:

If we have a module cli.py

import argparse

class Foo(object):
    @staticmethod
    def make_parser():
        parser = argparse.ArgumentParser()
        parser.add_argument("test")
        return parser

    @classmethod
    def main(cls):
        parser = cls.make_parser()
        args = parser.parse_args()
        print(args.test)

None of the ways I think could generate documentation for this seem to work.

One way:

Test Command
============

.. argparse::
    :module: cli
    :func: Foo.make_parser
    :prog: command_name

produces the error:

/path/to/file.rst:4: WARNING: Module "cli" has no attribute "Foo.make_parser"
Incorrect argparse :module: or :func: values?

Another way:

Test Command
============

.. argparse::
    :ref: cli.Foo.make_parser
    :prog: command_name

produces the error

/path/to/file.rst:4: WARNING: Failed to import "make_parser" from "cli.Foo".
No module named 'cli.foo'; 'cli' is not a package

Would it be possible to make this work somehow? Perhaps an additional directive is needed?