Skip to content

Commit f5fcd01

Browse files
committed
Add helper function for help string formatting
Hopefully more readable and maintainable than manually inserting seemingly magic newlines and spaces. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 5fd622a commit f5fcd01

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

bin/codebasin

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@ from codebasin.walkers.platform_mapper import PlatformMapper
1616
version = "1.2.0"
1717

1818

19+
def _help_string(*lines: str, is_long=False, is_last=False):
20+
"""
21+
Parameters
22+
----------
23+
*lines: str
24+
Each line in the help string.
25+
26+
is_long: bool
27+
A flag indicating whether the option is long enough to generate an
28+
initial newline by default.
29+
30+
is_last: bool
31+
A flag indicating whether the option is the last in the list.
32+
33+
Returns
34+
-------
35+
An argparse help string formatted as a paragraph.
36+
"""
37+
result = ""
38+
39+
# A long option like --exclude will force a newline.
40+
if not is_long:
41+
result = "\n"
42+
43+
# The additional space is required for argparse to respect newlines.
44+
result += "\n".join(lines)
45+
46+
if not is_last:
47+
result += "\n "
48+
49+
return result
50+
51+
1952
def main():
2053
# Read command-line arguments
2154
parser = argparse.ArgumentParser(
@@ -27,29 +60,29 @@ def main():
2760
"-h",
2861
"--help",
2962
action="help",
30-
help="\nDisplay help message and exit.\n ",
63+
help=_help_string("Display help message and exit."),
3164
)
3265
parser.add_argument(
3366
"--version",
3467
action="version",
3568
version=f"Code Base Investigator {version}",
36-
help="\nDisplay version information and exit.\n ",
69+
help=_help_string("Display version information and exit."),
3770
)
3871
parser.add_argument(
3972
"-v",
4073
"--verbose",
4174
dest="verbose",
4275
action="count",
4376
default=0,
44-
help="\nIncrease verbosity level.\n ",
77+
help=_help_string("Increase verbosity level."),
4578
)
4679
parser.add_argument(
4780
"-q",
4881
"--quiet",
4982
dest="quiet",
5083
action="count",
5184
default=0,
52-
help="\nDecrease verbosity level.\n ",
85+
help=_help_string("Decrease verbosity level."),
5386
)
5487
parser.add_argument(
5588
"-R",
@@ -59,9 +92,12 @@ def main():
5992
action="append",
6093
default=[],
6194
choices=["all", "summary", "clustering"],
62-
help="Generate a report of the specified type.\n"
63-
+ "May be specified multiple times.\n"
64-
+ "If not specified, all reports will be generated.\n ",
95+
help=_help_string(
96+
"Generate a report of the specified type.",
97+
"May be specified multiple times.",
98+
"If not specified, all reports will be generated.",
99+
is_long=True,
100+
),
65101
)
66102
parser.add_argument(
67103
"-x",
@@ -70,8 +106,11 @@ def main():
70106
metavar="<pattern>",
71107
action="append",
72108
default=[],
73-
help="Exclude files matching this pattern from the code base.\n"
74-
+ "May be specified multiple times.\n ",
109+
help=_help_string(
110+
"Exclude files matching this pattern from the code base.",
111+
"May be specified multiple times.",
112+
is_long=True,
113+
),
75114
)
76115
parser.add_argument(
77116
"-p",
@@ -80,15 +119,23 @@ def main():
80119
metavar="<platform>",
81120
action="append",
82121
default=[],
83-
help="Include the specified platform in the analysis.\n"
84-
+ "May be specified multiple times.\n"
85-
+ "If not specified, all platforms will be included.\n ",
122+
help=_help_string(
123+
"Include the specified platform in the analysis.",
124+
"May be specified multiple times.",
125+
"If not specified, all platforms will be included.",
126+
is_long=True,
127+
is_last=True,
128+
),
86129
)
130+
87131
parser.add_argument(
88132
"analysis_file",
89133
metavar="<analysis-file>",
90-
help="\nTOML file describing the analysis to be performed, "
91-
+ "including the codebase and platform descriptions.",
134+
help=_help_string(
135+
"TOML file describing the analysis to be performed, "
136+
+ "including the codebase and platform descriptions.",
137+
is_last=True,
138+
),
92139
)
93140

94141
args = parser.parse_args()

0 commit comments

Comments
 (0)