Skip to content

Commit e2f8ee2

Browse files
brettcannond3r3kk
authored andcommitted
Move from Click to docopt for announce.py (#1806)
1 parent 3d5f760 commit e2f8ee2

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ debug_coverage*/**
1818
analysis/**
1919
bin/**
2020
obj/**
21+
.pytest_cache

news/announce.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
"""Generate the changelog."""
1+
"""Generate the changelog.
2+
3+
Usage: announce [--dry_run | --interim | --final] [<directory>]
4+
5+
"""
26
import enum
37
import operator
48
import os
@@ -8,7 +12,7 @@
812
import sys
913
import types
1014

11-
import click
15+
import docopt
1216

1317

1418
FILENAME_RE = re.compile(r"(?P<issue>\d+)(?P<nonce>-\S+)?\.md")
@@ -117,15 +121,6 @@ class RunType(enum.Enum):
117121
final = 2
118122

119123

120-
@click.command()
121-
@click.option('--dry-run', 'run_type', flag_value=RunType.dry_run,
122-
help='validate input')
123-
@click.option('--interim', 'run_type', flag_value=RunType.interim, default=True,
124-
help='generate Markdown')
125-
@click.option('--final', 'run_type', flag_value=RunType.final,
126-
help='generate Markdown & `git rm` news files')
127-
@click.argument('directory', default=pathlib.Path(__file__).parent,
128-
type=click.Path(exists=True, file_okay=False))
129124
def main(run_type, directory):
130125
directory = pathlib.Path(directory)
131126
data = gather(directory)
@@ -137,4 +132,11 @@ def main(run_type, directory):
137132

138133

139134
if __name__ == '__main__':
140-
main()
135+
arguments = docopt.docopt(__doc__)
136+
run_type = RunType.interim
137+
for possible_run_type in RunType:
138+
if arguments[f"--{possible_run_type.name}"]:
139+
run_type = possible_run_type
140+
break
141+
directory = arguments["<directory>"] or pathlib.Path(__file__).parent
142+
main(run_type, directory)

news/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
click~=6.7.0
1+
docopt==0.6.2
22
pytest~=3.4.1

news/test_announce.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pathlib
22

3+
import docopt
34
import pytest
45

56
import announce as ann
@@ -48,8 +49,7 @@ def test_sections_sorting(directory):
4849

4950
def test_sections_naming(directory):
5051
(directory / 'Hello').mkdir()
51-
with pytest.raises(ValueError):
52-
list(ann.sections(directory))
52+
assert not ann.sections(directory)
5353

5454

5555
def test_gather(directory):
@@ -79,8 +79,6 @@ def test_gather(directory):
7979
assert entries[1].description == 'Fix 2'
8080

8181

82-
83-
8482
def test_entry_markdown():
8583
markdown = ann.entry_markdown(ann.NewsEntry(42, 'Hello, world!', None))
8684
assert '42' in markdown
@@ -126,3 +124,14 @@ def fake_git_rm(path):
126124
section, entries = results.pop()
127125
assert len(entries) == 1
128126
assert rm_path == entries[0].path
127+
128+
129+
def test_cli():
130+
for option in ("--"+opt for opt in ["dry_run", "interim", "final"]):
131+
args = docopt.docopt(ann.__doc__, [option])
132+
assert args[option]
133+
args = docopt.docopt(ann.__doc__, ["./news"])
134+
assert args["<directory>"] == "./news"
135+
args = docopt.docopt(ann.__doc__, ["--dry_run", "./news"])
136+
assert args["--dry_run"]
137+
assert args["<directory>"] == "./news"

0 commit comments

Comments
 (0)