Skip to content

Commit 8639a91

Browse files
committed
refactor: replace repetitive CLI parser setup with config-driven location registry
_add_subparsers_source() repeated ~13 lines for each of the 4 location types (local, git, maven, pypi) — 52 lines total. Replace with a module-level _LOCATION_DEFS list and a loop, reducing the method body to 16 lines. Pure behavioral refactor: no argument flags, help text, or required/optional status changed. Signed-off-by: Jimisola Laursen <jimisola@jimisola.com>
1 parent 195eaf5 commit 8639a91

1 file changed

Lines changed: 64 additions & 53 deletions

File tree

src/reqstool/command.py

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,55 @@
3535
from reqstool.locations.pypi_location import PypiLocation
3636

3737

38+
_LOCATION_DEFS = [
39+
{
40+
"name": "local",
41+
"help": "local source",
42+
"exclusive_group": True,
43+
"args": [
44+
{"flags": ["-p", "--path"], "kwargs": {"help": "path to a local directory"}},
45+
{"flags": ["--maven"], "kwargs": {"metavar": "PATH", "help": "path to a local Maven ZIP artifact (.zip)"}},
46+
{
47+
"flags": ["--pypi"],
48+
"kwargs": {"metavar": "PATH", "help": "path to a local PyPI sdist tarball (.tar.gz)"},
49+
},
50+
],
51+
},
52+
{
53+
"name": "git",
54+
"help": "git source",
55+
"args": [
56+
{"flags": ["-u", "--url"], "kwargs": {"help": "url description", "required": True}},
57+
{"flags": ["-p", "--path"], "kwargs": {"help": "path description", "required": True}},
58+
{"flags": ["-b", "--branch"], "kwargs": {"help": "branch description"}},
59+
{"flags": ["-t", "--env_token"], "kwargs": {"help": "env_token description"}},
60+
],
61+
},
62+
{
63+
"name": "maven",
64+
"help": "maven source",
65+
"args": [
66+
{"flags": ["-u", "--url"], "kwargs": {"help": "url description", "required": False}},
67+
{"flags": ["-t", "--env_token"], "kwargs": {"help": "env_token description"}},
68+
{"flags": ["--group_id"], "kwargs": {"help": "group_id description", "required": True}},
69+
{"flags": ["--artifact_id"], "kwargs": {"help": "artifact_id description", "required": True}},
70+
{"flags": ["--version"], "kwargs": {"help": "version description", "required": True}},
71+
{"flags": ["--classifier"], "kwargs": {"help": "classifier description"}},
72+
],
73+
},
74+
{
75+
"name": "pypi",
76+
"help": "pypi source",
77+
"args": [
78+
{"flags": ["-u", "--url"], "kwargs": {"help": "url description", "required": False}},
79+
{"flags": ["-t", "--env_token"], "kwargs": {"help": "env_token description"}},
80+
{"flags": ["--package"], "kwargs": {"help": "package", "required": True}},
81+
{"flags": ["--version"], "kwargs": {"help": "version description", "required": True}},
82+
],
83+
},
84+
]
85+
86+
3887
class Command:
3988
__parser: argparse.Namespace
4089

@@ -111,59 +160,21 @@ def _add_filter_options(self, parser: argparse.ArgumentParser):
111160
)
112161

113162
def _add_subparsers_source(self, parser, include_report_options=True, include_filter_options=False):
114-
# Subparser for local report
115-
local_report_parser = parser.add_parser("local", help="local source")
116-
local_group = local_report_parser.add_mutually_exclusive_group(required=True)
117-
local_group.add_argument("-p", "--path", help="path to a local directory")
118-
local_group.add_argument("--maven", metavar="PATH", help="path to a local Maven ZIP artifact (.zip)")
119-
local_group.add_argument("--pypi", metavar="PATH", help="path to a local PyPI sdist tarball (.tar.gz)")
120-
self._add_argument_output(local_report_parser)
121-
if include_report_options:
122-
self._add_group_by(local_report_parser)
123-
self._add_sort_by(local_report_parser)
124-
if include_filter_options:
125-
self._add_filter_options(local_report_parser)
126-
127-
# Subparser for git report
128-
git_report_parser = parser.add_parser("git", help="git source")
129-
git_report_parser.add_argument("-u", "--url", help="url description", required=True)
130-
git_report_parser.add_argument("-p", "--path", help="path description", required=True)
131-
git_report_parser.add_argument("-b", "--branch", help="branch description")
132-
git_report_parser.add_argument("-t", "--env_token", help="env_token description")
133-
self._add_argument_output(git_report_parser)
134-
if include_report_options:
135-
self._add_group_by(git_report_parser)
136-
self._add_sort_by(git_report_parser)
137-
if include_filter_options:
138-
self._add_filter_options(git_report_parser)
139-
140-
# Subparser for maven report
141-
maven_report_parser = parser.add_parser("maven", help="maven source")
142-
maven_report_parser.add_argument("-u", "--url", help="url description", required=False)
143-
maven_report_parser.add_argument("-t", "--env_token", help="env_token description")
144-
maven_report_parser.add_argument("--group_id", help="group_id description", required=True)
145-
maven_report_parser.add_argument("--artifact_id", help="artifact_id description", required=True)
146-
maven_report_parser.add_argument("--version", help="version description", required=True)
147-
maven_report_parser.add_argument("--classifier", help="classifier description")
148-
self._add_argument_output(maven_report_parser)
149-
if include_report_options:
150-
self._add_group_by(maven_report_parser)
151-
self._add_sort_by(maven_report_parser)
152-
if include_filter_options:
153-
self._add_filter_options(maven_report_parser)
154-
155-
# Subparser for pypi report
156-
pypi_report_parser = parser.add_parser("pypi", help="pypi source")
157-
pypi_report_parser.add_argument("-u", "--url", help="url description", required=False)
158-
pypi_report_parser.add_argument("-t", "--env_token", help="env_token description")
159-
pypi_report_parser.add_argument("--package", help="package", required=True)
160-
pypi_report_parser.add_argument("--version", help="version description", required=True)
161-
self._add_argument_output(pypi_report_parser)
162-
if include_report_options:
163-
self._add_group_by(pypi_report_parser)
164-
self._add_sort_by(pypi_report_parser)
165-
if include_filter_options:
166-
self._add_filter_options(pypi_report_parser)
163+
for loc in _LOCATION_DEFS:
164+
sub = parser.add_parser(loc["name"], help=loc["help"])
165+
if loc.get("exclusive_group"):
166+
grp = sub.add_mutually_exclusive_group(required=True)
167+
for arg in loc["args"]:
168+
grp.add_argument(*arg["flags"], **arg["kwargs"])
169+
else:
170+
for arg in loc["args"]:
171+
sub.add_argument(*arg["flags"], **arg["kwargs"])
172+
self._add_argument_output(sub)
173+
if include_report_options:
174+
self._add_group_by(sub)
175+
self._add_sort_by(sub)
176+
if include_filter_options:
177+
self._add_filter_options(sub)
167178

168179
def _add_argument_version(self, argument_parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
169180
ver = Utils.get_version()

0 commit comments

Comments
 (0)