Skip to content

Commit dafb144

Browse files
committed
cli: Expose knobs for date and version
Provide a way to override the date and version used in generated man pages. This can be useful where you want to generate man pages before actually releasing something and don't want to manually edit them. Signed-off-by: Stephen Finucane <[email protected]>
1 parent 41e0cd1 commit dafb144

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

click_man/core.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ def get_short_help_str(command, limit=45):
2626
return command.short_help or command.help and click.utils.make_default_short_help(command.help, limit) or ''
2727

2828

29-
def generate_man_page(ctx, version=None):
29+
def generate_man_page(ctx, version=None, date=None):
3030
"""
3131
Generate documentation for the given command.
3232
3333
:param click.Context ctx: the click context for the
34-
cli application.
34+
cli application.
35+
:param str version: The version information to include in the man page.
36+
:param str date: The date information to include in the man page.
3537
36-
:rtype: str
3738
:returns: the generate man page from the given click Context.
39+
:rtype: str
3840
"""
3941
# Create man page with the details from the given context
4042
man_page = ManPage(ctx.command_path)
@@ -43,6 +45,10 @@ def generate_man_page(ctx, version=None):
4345
man_page.description = ctx.command.help
4446
man_page.synopsis = ' '.join(ctx.command.collect_usage_pieces(ctx))
4547
man_page.options = [x.get_help_record(ctx) for x in ctx.command.params if isinstance(x, click.Option) and not getattr(x, 'hidden', False)]
48+
49+
if date:
50+
man_page.date = date
51+
4652
commands = getattr(ctx.command, 'commands', None)
4753
if commands:
4854
man_page.commands = [
@@ -52,7 +58,9 @@ def generate_man_page(ctx, version=None):
5258
return str(man_page)
5359

5460

55-
def write_man_pages(name, cli, parent_ctx=None, version=None, target_dir=None):
61+
def write_man_pages(
62+
name, cli, parent_ctx=None, version=None, target_dir=None, date=None,
63+
):
5664
"""
5765
Generate man page files recursively
5866
for the given click cli function.
@@ -62,6 +70,7 @@ def write_man_pages(name, cli, parent_ctx=None, version=None, target_dir=None):
6270
:param click.Context parent_ctx: the parent click context
6371
:param str target_dir: the directory where the generated
6472
man pages are stored.
73+
:param date: the date to include in the header
6574
"""
6675
ctx = click.Context(cli, info_name=name, parent=parent_ctx)
6776

@@ -80,4 +89,8 @@ def write_man_pages(name, cli, parent_ctx=None, version=None, target_dir=None):
8089
if command.hidden:
8190
# Do not write a man page for a hidden command
8291
continue
83-
write_man_pages(name, command, parent_ctx=ctx, version=version, target_dir=target_dir)
92+
93+
write_man_pages(
94+
name, command, parent_ctx=ctx, version=version,
95+
target_dir=target_dir, date=date,
96+
)

click_man/shell.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
:license: MIT, see LICENSE for more details.
1010
"""
1111

12+
from datetime import datetime
1213
import os
13-
import click
1414
from pkg_resources import iter_entry_points, get_distribution
1515

16+
import click
17+
1618
from click_man.core import write_man_pages
1719

1820

@@ -22,9 +24,11 @@
2224
type=click.Path(file_okay=False, dir_okay=True, resolve_path=True),
2325
help='Target location for the man pages'
2426
)
27+
@click.option('--man-version', help='Version to use in generated man page(s)')
28+
@click.option('--man-date', help='Date to use in generated man page(s)')
2529
@click.version_option(get_distribution('click-man').version, '-V', '--version')
2630
@click.argument('name')
27-
def cli(target, name):
31+
def cli(target, name, man_version, man_date):
2832
"""
2933
Generate man pages for the scripts defined in the ``console_scripts`` entry
3034
point.
@@ -50,6 +54,17 @@ def cli(target, name):
5054
except OSError:
5155
pass
5256

57+
if not man_version:
58+
man_version = entry_point.dist.version
59+
60+
if man_date:
61+
try:
62+
datetime.strptime(man_date, '%Y-%m-%d')
63+
except ValueError:
64+
raise click.ClickException(
65+
'"{0}" is not a valid date.'.format(man_date)
66+
)
67+
5368
click.echo('Load entry point {0}'.format(name))
5469
cli = entry_point.resolve()
5570

@@ -81,5 +96,5 @@ def cli(target, name):
8196

8297
click.echo('Generate man pages for {0} in {1}'.format(name, target))
8398
write_man_pages(
84-
name, cli, version=entry_point.dist.version, target_dir=target,
99+
name, cli, version=man_version, target_dir=target, date=man_date,
85100
)

tests/test_shell.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,37 @@ def test_is_click_command(mock_entry_points, mock_echo, mock_write):
5050
])
5151
mock_write.assert_called_once_with(
5252
'foo', fake_command, version=fake_version, target_dir=fake_target,
53+
date=None,
54+
)
55+
56+
57+
@mock.patch('os.makedirs', new=mock.Mock())
58+
@mock.patch.object(shell, 'write_man_pages')
59+
@mock.patch.object(click, 'echo')
60+
@mock.patch.object(shell, 'iter_entry_points')
61+
def test_man_date_version(mock_entry_points, mock_echo, mock_write):
62+
fake_target = os.path.join(os.getcwd(), 'man')
63+
fake_command = click.Command(name='foo')
64+
entry_point = mock.Mock()
65+
entry_point.resolve.return_value = fake_command
66+
67+
mock_entry_points.return_value = iter([entry_point])
68+
69+
runner = CLIRunner()
70+
result = runner.invoke(
71+
shell.cli,
72+
['foo', '--man-version', '3.2.1', '--man-date', '2020-01-01'],
73+
)
74+
75+
assert result.exit_code == 0, result.output
76+
77+
mock_entry_points.assert_called_once_with('console_scripts', name='foo')
78+
entry_point.dist.version.assert_not_called()
79+
mock_echo.assert_has_calls([
80+
mock.call('Load entry point foo'),
81+
mock.call('Generate man pages for foo in %s' % fake_target),
82+
])
83+
mock_write.assert_called_once_with(
84+
'foo', fake_command, version='3.2.1', target_dir=fake_target,
85+
date='2020-01-01',
5386
)

0 commit comments

Comments
 (0)