click-contrib / click-man

Automate generation of man pages for python click applications :star:
MIT License
163 stars 35 forks source link

AttributeError: 'function' object has no attribute 'allow_extra_args' #20

Closed ElieDeloumeau closed 6 years ago

ElieDeloumeau commented 6 years ago

AttributeError: 'function' object has no attribute 'allow_extra_args'

Python version: 3.6 Command: python3 setup.py --command-packages=click_man.commands man_pages

stephenfin commented 6 years ago

We saw a similar issue with https://github.com/click-contrib/sphinx-click. tl;dr: you're using the wrong configuration. See https://github.com/click-contrib/sphinx-click/issues/13

ElieDeloumeau commented 6 years ago

Which configuration?

stephenfin commented 6 years ago

Which configuration?

The commands listed in your console_scripts setting in conf.py. Have you a minimal test case you can share?

ElieDeloumeau commented 6 years ago

Sure, here is the test case:

mycli.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import click

@click.group()
def mycli():
    """
    Test CLI
    """
    pass

@click.command()
def test_command():
    """
    This is a test command
    """
    click.echo("Hello world")

def main():
    mycli.add_command(test_command)
    mycli()

if __name__ == '__main__':
    main()

setup.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages

setup(
    name='MyCLI',
    py_modules=['mycli'],
    packages=find_packages(),
    install_requires=['click'],
    entry_points='''
        [console_scripts]
        my=mycli:main
    ''',
    classifiers=[
        'Natural Language :: English',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: POSIX :: Linux',
        'Programming Language :: Python'
    ]
)

Executed command:

$ PATH=env/bin/ click-man my 
Load entry point my
Generate man pages for my in /home/elie/Documents/git/mycli/man
Traceback (most recent call last):
  File "env/bin//click-man", line 11, in <module>
    sys.exit(cli())
  File "/home/elie/Documents/git/mycli/env/lib/python2.7/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/elie/Documents/git/mycli/env/lib/python2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/elie/Documents/git/mycli/env/lib/python2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/elie/Documents/git/mycli/env/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/elie/Documents/git/mycli/env/lib/python2.7/site-packages/click_man/__main__.py", line 49, in cli
    write_man_pages(name, cli, version=entry_point.dist.version, target_dir=target)
  File "/home/elie/Documents/git/mycli/env/lib/python2.7/site-packages/click_man/core.py", line 54, in write_man_pages
    ctx = click.Context(cli, info_name=name, parent=parent_ctx)
  File "/home/elie/Documents/git/mycli/env/lib/python2.7/site-packages/click/core.py", line 254, in __init__
    allow_extra_args = command.allow_extra_args
AttributeError: 'function' object has no attribute 'allow_extra_args'
stephenfin commented 6 years ago

This issue is main does not return a click object. You need to modify it like so.

diff --git a/mycli.py b/mycli.py
index 0b87781..fb3afb4 100644
--- a/mycli.py
+++ b/mycli.py
@@ -16,9 +16,7 @@ def test_command():
     """
     click.echo("Hello world")

-def main():
-    mycli.add_command(test_command)
-    mycli()
+mycli.add_command(test_command)

 if __name__ == '__main__':
-    main()
+    mycli()
diff --git a/setup.py b/setup.py
index 2afc564..20a85e7 100644
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,7 @@ setup(
     install_requires=['click'],
     entry_points='''
         [console_scripts]
-        my=mycli:main
+        my=mycli:mycli
     ''',
     classifiers=[
         'Natural Language :: English',

This ensures that (a) the add_command function is always called and the command registered and (b) you're trying to document a click object and not a random function.

ElieDeloumeau commented 6 years ago

It works! Many thanks :smiley: