Skip to content

Commit a6e90c7

Browse files
committed
Add subclass of click.Argument with an additional argument for storing a short description.
1 parent 9b369bf commit a6e90c7

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed

consolekit/options.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"flag_option",
8181
"auto_default_option",
8282
"auto_default_argument",
83+
"DescribedArgument",
8384
"_A",
8485
"_C",
8586
]
@@ -119,12 +120,12 @@ def verbose_option(help_text: str = "Show verbose output.") -> Callable[[_C], _C
119120
"""
120121

121122
return click.option( # type: ignore
122-
"-v",
123-
"--verbose",
124-
count=True,
125-
help=help_text,
126-
type=VerboseVersionCountType(),
127-
)
123+
"-v",
124+
"--verbose",
125+
count=True,
126+
help=help_text,
127+
type=VerboseVersionCountType(),
128+
)
128129

129130

130131
def version_option(callback: Callable[[click.Context, click.Option, int], Any]) -> Callable[[_C], _C]:
@@ -156,14 +157,14 @@ def version_callback(ctx: click.Context, param: click.Option, value: int):
156157
"""
157158

158159
return click.option( # type: ignore
159-
"--version",
160-
count=True,
161-
expose_value=False,
162-
is_eager=True,
163-
help="Show the version and exit.",
164-
type=VerboseVersionCountType(),
165-
callback=cast(Callback, callback),
166-
)
160+
"--version",
161+
count=True,
162+
expose_value=False,
163+
is_eager=True,
164+
help="Show the version and exit.",
165+
type=VerboseVersionCountType(),
166+
callback=cast(Callback, callback),
167+
)
167168

168169

169170
def colour_option(help_text="Whether to use coloured output.") -> Callable[[_C], _C]:
@@ -468,3 +469,30 @@ def prompt_for_value(self, ctx):
468469
confirmation_prompt=self.confirmation_prompt,
469470
value_proc=lambda x: self.process_value(ctx, x),
470471
)
472+
473+
474+
class DescribedArgument(click.Argument):
475+
r"""
476+
:class:`click.Argument` with an additional keyword argument and attribute giving a short description.
477+
478+
This is not shown in the help text,
479+
but may be useful for manpages or HTML documentation where additional information can be provided.
480+
481+
.. versionadded:: 1.2.0
482+
483+
:param description:
484+
485+
See :class:`click.Argument` and :class:`click.Parameter` for descriptions of the other keyword argu
486+
ments.
487+
488+
.. attribute:: description
489+
490+
**Type:** |nbsp| |nbsp| |nbsp| |nbsp| :py:obj:`~typing.Optional`\[:py:class:`str`\]
491+
492+
A short description of the argument.
493+
"""
494+
495+
def __init__(self, *args, description: Optional[str] = None, **kwargs):
496+
super().__init__(*args, **kwargs)
497+
498+
self.description: Optional[str] = description

tests/test_options.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# this package
1010
from consolekit import click_command
1111
from consolekit.options import (
12+
DescribedArgument,
1213
MultiValueOption,
1314
auto_default_argument,
1415
auto_default_option,
@@ -23,6 +24,35 @@
2324
from consolekit.testing import CliRunner, Result
2425

2526

27+
def test_described_argument(
28+
file_regression: FileRegressionFixture,
29+
cli_runner: CliRunner,
30+
):
31+
32+
@click.argument(
33+
"dest",
34+
cls=DescribedArgument,
35+
type=click.STRING,
36+
description="The destination directory.",
37+
)
38+
@click_command()
39+
def main(dest: str):
40+
print(dest)
41+
42+
result = cli_runner.invoke(main, args="--help")
43+
result.check_stdout(file_regression, extension=".md")
44+
assert result.exit_code == 0
45+
46+
result = cli_runner.invoke(main, catch_exceptions=False, args="./staging")
47+
assert result.stdout.rstrip() == "./staging"
48+
assert result.exit_code == 0
49+
50+
ctx = click.Context(main, info_name="main", parent=None)
51+
argument = ctx.command.params[0]
52+
assert isinstance(argument, DescribedArgument)
53+
assert argument.description == "The destination directory."
54+
55+
2656
def test_auto_default_option(
2757
file_regression: FileRegressionFixture,
2858
cli_runner: CliRunner,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Usage: main [OPTIONS] DEST
2+
3+
Options:
4+
-h, --help Show this message and exit.

0 commit comments

Comments
 (0)