Skip to content

Commit 03138e6

Browse files
committed
fixup refactoring of CLI args
1 parent f2e3a6b commit 03138e6

File tree

6 files changed

+204
-421
lines changed

6 files changed

+204
-421
lines changed

gh2slack.py

Lines changed: 12 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import rss2irc
2020
import rss2slack
2121
from lib import CachedData
22+
from lib import cli_args
2223
from lib import config_options
2324
from lib import utils
2425
from lib.exceptions import CacheReadError
@@ -232,105 +233,38 @@ def main():
232233
def parse_args() -> argparse.Namespace:
233234
"""Return parsed CLI args."""
234235
parser = argparse.ArgumentParser()
235-
parser.add_argument(
236-
"--cache",
237-
dest="cache_file",
238-
type=str,
239-
default=None,
240-
help="Path to cache file.",
241-
)
242-
parser.add_argument(
243-
"--cache-expiration",
244-
dest="cache_expiration",
245-
type=int,
246-
default=config_options.CACHE_EXPIRATION,
247-
help=(
248-
"How long to keep items in cache. "
249-
"Defaults to %(default)s seconds."
250-
),
251-
)
252-
parser.add_argument(
253-
"--cache-init",
254-
dest="cache_init",
255-
action="store_true",
256-
default=False,
257-
help=(
258-
"Prevents posting news to IRC. This is useful "
259-
"when bootstrapping new RSS feed."
260-
),
261-
)
262-
parser.add_argument(
236+
cli_args.add_generic_args(parser)
237+
cli_args.add_cache_file_arg_group(parser)
238+
239+
github_group = parser.add_argument_group("GitHub options")
240+
github_group.add_argument(
263241
"--gh-owner",
264242
dest="gh_owner",
265243
required=True,
266244
type=str,
267245
help="Owner/org of the repository to track.",
268246
)
269-
parser.add_argument(
247+
github_group.add_argument(
270248
"--gh-repo",
271249
dest="gh_repo",
272250
required=True,
273251
type=str,
274252
help="Repository of owner/org to track.",
275253
)
276-
parser.add_argument(
254+
github_group.add_argument(
277255
"--gh-section",
278256
dest="gh_section",
279257
required=True,
280258
choices=["issues", "pulls"],
281259
help='GH "section" to track.',
282260
)
283-
parser.add_argument(
284-
"--return-error",
285-
dest="mask_errors",
286-
action="store_false",
287-
default=True,
288-
help=(
289-
"Return RC > 0 should error occur. "
290-
"Majority of errors are masked because of cron."
291-
),
292-
)
293-
parser.add_argument(
294-
"--slack-base-url",
295-
dest="slack_base_url",
296-
type=str,
297-
default=rss2slack.SLACK_BASE_URL,
298-
help="Base URL for Slack client.",
299-
)
300-
parser.add_argument(
301-
"--slack-channel",
302-
dest="slack_channel",
303-
type=str,
304-
required=True,
305-
help="Name of Slack channel to send formatted news to.",
306-
)
307-
parser.add_argument(
308-
"--slack-timeout",
309-
dest="slack_timeout",
310-
type=int,
311-
default=config_options.HTTP_TIMEOUT,
312-
help="Slack API Timeout. Defaults to %(default)s seconds.",
313-
)
314-
parser.add_argument(
315-
"--sleep",
316-
dest="sleep",
317-
type=int,
318-
default=2,
319-
help=(
320-
"Sleep between messages in order to avoid "
321-
"possible excess flood/API call rate limit."
322-
),
323-
)
324-
parser.add_argument(
325-
"-v",
326-
"--verbose",
327-
action="count",
328-
default=0,
329-
help="Increase log level verbosity. Can be passed multiple times.",
330-
)
261+
262+
cli_args.add_slack_arg_group(parser, rss2slack.SLACK_BASE_URL)
331263
args = parser.parse_args()
332264
args.log_level = utils.calc_log_level(args.verbose)
333265

266+
cli_args.check_cache_expiration_arg(parser, args)
267+
cli_args.check_sleep_arg(parser, args)
334268
return args
335269

336270

git_commits2slack.py

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from typing import List
1717

1818
import rss2slack
19-
from lib import config_options
19+
from lib import cli_args
2020
from lib import utils
2121
from lib.exceptions import SlackTokenError
2222

@@ -217,75 +217,32 @@ def main():
217217
def parse_args() -> argparse.Namespace:
218218
"""Return parsed CLI args."""
219219
parser = argparse.ArgumentParser()
220-
parser.add_argument(
220+
cli_args.add_generic_args(parser)
221+
222+
git_group = parser.add_argument_group("git options")
223+
git_group.add_argument(
221224
"--git-clone-dir",
222225
dest="git_clone_dir",
223226
required=True,
224227
type=str,
225228
help="Directory where git repository will be cloned into.",
226229
)
227-
parser.add_argument(
230+
git_group.add_argument(
228231
"--git-repository",
229232
dest="git_repo",
230233
required=True,
231234
type=str,
232235
help="git repository to track.",
233236
)
234-
parser.add_argument(
237+
git_group.add_argument(
235238
"--git-web",
236239
dest="git_web",
237240
type=str,
238241
default="http://localhost",
239242
help="git web interface, resp. base URL, for given repository.",
240243
)
241-
parser.add_argument(
242-
"--return-error",
243-
dest="mask_errors",
244-
action="store_false",
245-
default=True,
246-
help=(
247-
"Return RC > 0 should error occur. "
248-
"Majority of errors are masked because of cron."
249-
),
250-
)
251-
parser.add_argument(
252-
"--slack-base-url",
253-
dest="slack_base_url",
254-
type=str,
255-
default=rss2slack.SLACK_BASE_URL,
256-
help="Base URL for Slack client.",
257-
)
258-
parser.add_argument(
259-
"--slack-channel",
260-
dest="slack_channel",
261-
type=str,
262-
required=True,
263-
help="Name of Slack channel to send formatted news to.",
264-
)
265-
parser.add_argument(
266-
"--slack-timeout",
267-
dest="slack_timeout",
268-
type=int,
269-
default=config_options.HTTP_TIMEOUT,
270-
help="Slack API Timeout. Defaults to %(default)s seconds.",
271-
)
272-
parser.add_argument(
273-
"--sleep",
274-
dest="sleep",
275-
type=int,
276-
default=2,
277-
help=(
278-
"Sleep between messages in order to avoid "
279-
"possible excess flood/API call rate limit."
280-
),
281-
)
282-
parser.add_argument(
283-
"-v",
284-
"--verbose",
285-
action="count",
286-
default=0,
287-
help="Increase log level verbosity. Can be passed multiple times.",
288-
)
244+
245+
cli_args.add_slack_arg_group(parser, rss2slack.SLACK_BASE_URL)
289246
args = parser.parse_args()
290247
args.log_level = utils.calc_log_level(args.verbose)
291248

lib/cli_args.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#!/usr/bin/env python3
2+
"""Helper functions related to CLI args.
3+
4+
2026/Jan/06 @ Zdenek Styblik
5+
"""
6+
import argparse
7+
from dataclasses import dataclass
8+
9+
from . import config_options
10+
11+
12+
@dataclass
13+
class GenericArgsCfg:
14+
"""Class represents configuration of generic CLI args."""
15+
16+
handle: bool = False
17+
output: bool = False
18+
19+
20+
def add_cache_file_arg_group(parser: argparse.ArgumentParser):
21+
"""Add cache related CLI args."""
22+
cache_file_group = parser.add_argument_group("caching options")
23+
cache_file_group.add_argument(
24+
"--cache",
25+
dest="cache_file",
26+
type=str,
27+
default=None,
28+
help="File which contains cache.",
29+
)
30+
cache_file_group.add_argument(
31+
"--cache-expiration",
32+
dest="cache_expiration",
33+
type=int,
34+
default=config_options.CACHE_EXPIRATION,
35+
help=(
36+
"How long to keep items in cache. "
37+
"Defaults to %(default)s seconds."
38+
),
39+
)
40+
cache_file_group.add_argument(
41+
"--cache-init",
42+
dest="cache_init",
43+
action="store_true",
44+
default=False,
45+
help=(
46+
"Prevents posting news to IRC. This is useful "
47+
"when bootstrapping new RSS feed."
48+
),
49+
)
50+
51+
52+
def add_generic_args(
53+
parser: argparse.ArgumentParser, args_cfg: GenericArgsCfg = None
54+
):
55+
"""Add generic CLI args."""
56+
if args_cfg and args_cfg.handle:
57+
parser.add_argument(
58+
"--handle",
59+
dest="handle",
60+
type=str,
61+
default=None,
62+
help="Handle/callsign of this feed.",
63+
)
64+
65+
if args_cfg and args_cfg.output:
66+
parser.add_argument(
67+
"--output",
68+
dest="output",
69+
type=str,
70+
required=True,
71+
help="Where to output formatted news.",
72+
)
73+
74+
parser.add_argument(
75+
"--return-error",
76+
dest="mask_errors",
77+
action="store_false",
78+
default=True,
79+
help=(
80+
"Return RC > 0 should error occur. "
81+
"Majority of errors are masked because of cron."
82+
),
83+
)
84+
parser.add_argument(
85+
"--sleep",
86+
dest="sleep",
87+
type=int,
88+
default=2,
89+
help="Sleep between messages in order to avoid Excess Flood at IRC.",
90+
)
91+
parser.add_argument(
92+
"-v",
93+
"--verbose",
94+
action="count",
95+
default=0,
96+
help="Increase log level verbosity. Can be passed multiple times.",
97+
)
98+
99+
100+
def add_rss_arg_group(parser: argparse.ArgumentParser):
101+
"""Add RSS related CLI args."""
102+
rss_group = parser.add_argument_group("RSS options")
103+
rss_group.add_argument(
104+
"--rss-url",
105+
dest="rss_url",
106+
type=str,
107+
required=True,
108+
help="URL of RSS Feed.",
109+
)
110+
rss_group.add_argument(
111+
"--rss-http-timeout",
112+
dest="rss_http_timeout",
113+
type=int,
114+
default=config_options.HTTP_TIMEOUT,
115+
help="HTTP Timeout. Defaults to %(default)s seconds.",
116+
)
117+
118+
119+
def add_slack_arg_group(parser: argparse.ArgumentParser, slack_base_url: str):
120+
"""Add Slack related CLI args."""
121+
slack_group = parser.add_argument_group("Slack options")
122+
slack_group.add_argument(
123+
"--slack-base-url",
124+
dest="slack_base_url",
125+
type=str,
126+
default=slack_base_url,
127+
help="Base URL for Slack client.",
128+
)
129+
slack_group.add_argument(
130+
"--slack-channel",
131+
dest="slack_channel",
132+
type=str,
133+
required=True,
134+
help="Name of Slack channel to send formatted news to.",
135+
)
136+
slack_group.add_argument(
137+
"--slack-timeout",
138+
dest="slack_timeout",
139+
type=int,
140+
default=config_options.HTTP_TIMEOUT,
141+
help="Slack API Timeout. Defaults to %(default)s seconds.",
142+
)
143+
144+
145+
def check_cache_expiration_arg(
146+
parser: argparse.ArgumentParser, args: argparse.Namespace
147+
):
148+
"""Check that cache_expiration CLI arg is within range."""
149+
if args.cache_expiration < 0:
150+
parser.error("Cache expiration cannot be less than 0.")
151+
152+
153+
def check_sleep_arg(parser: argparse.ArgumentParser, args: argparse.Namespace):
154+
"""Check that sleep CLI arg is within range."""
155+
if args.sleep < 0:
156+
parser.error("Sleep interval cannot be less than 0.")

0 commit comments

Comments
 (0)