Skip to content

Commit

Permalink
Distinguish between click and asyncclick
Browse files Browse the repository at this point in the history
Otherwise 'isinstance(param, click.Option)' fails because
'asyncclick.Option' != 'click.Option'.

Signed-off-by: Stephen Finucane <stephen@that.guru>
  • Loading branch information
stephenfin committed May 14, 2024
1 parent ac21950 commit 05b69cf
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import asyncclick as click
except ImportError:
import click
import click.core
from docutils import nodes
from docutils.parsers import rst
from docutils.parsers.rst import directives
Expand Down Expand Up @@ -63,7 +64,7 @@ def _get_usage(ctx: click.Context) -> str:
return formatter.getvalue().rstrip('\n') # type: ignore


def _get_help_record(ctx: click.Context, opt: click.Option) -> ty.Tuple[str, str]:
def _get_help_record(ctx: click.Context, opt: click.core.Option) -> ty.Tuple[str, str]:
"""Re-implementation of click.Opt.get_help_record.
The variant of 'get_help_record' found in Click makes uses of slashes to
Expand Down Expand Up @@ -171,9 +172,9 @@ def _format_usage(ctx: click.Context) -> ty.Generator[str, None, None]:


def _format_option(
ctx: click.Context, opt: click.Option
ctx: click.Context, opt: click.core.Option
) -> ty.Generator[str, None, None]:
"""Format the output for a `click.Option`."""
"""Format the output for a `click.core.Option`."""
opt_help = _get_help_record(ctx, opt)

yield '.. option:: {}'.format(opt_help[0])
Expand All @@ -196,10 +197,14 @@ def _format_option(
def _format_options(ctx: click.Context) -> ty.Generator[str, None, None]:
"""Format all `click.Option` for a `click.Command`."""
# the hidden attribute is part of click 7.x only hence use of getattr
print(ctx.command.params)
for param in ctx.command.params:
print(type(param))
print(isinstance(param, click.Option))
params = [
param
for param in ctx.command.params
if isinstance(param, click.Option) and not getattr(param, 'hidden', False)
if isinstance(param, click.core.Option) and not getattr(param, 'hidden', False)
]

for param in params:
Expand Down Expand Up @@ -238,7 +243,7 @@ def _format_arguments(ctx: click.Context) -> ty.Generator[str, None, None]:


def _format_envvar(
param: ty.Union[click.Option, click.Argument]
param: ty.Union[click.core.Option, click.Argument]
) -> ty.Generator[str, None, None]:
"""Format the envvars of a `click.Option` or `click.Argument`."""
yield '.. envvar:: {}'.format(param.envvar)
Expand Down

0 comments on commit 05b69cf

Please sign in to comment.