Skip to content

Commit 356b121

Browse files
authored
Merge pull request #104 from Pennycook/help-format
Format help strings
2 parents 3e8eae2 + fcc9a08 commit 356b121

File tree

1 file changed

+66
-14
lines changed

1 file changed

+66
-14
lines changed

bin/codebasin

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,77 @@ 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+
# argparse.HelpFormatter indents by 24 characters.
44+
# We cannot override this directly, but can delete them with backspaces.
45+
lines = ["\b" * 20 + x for x in lines]
46+
47+
# The additional space is required for argparse to respect newlines.
48+
result += "\n".join(lines)
49+
50+
if not is_last:
51+
result += "\n "
52+
53+
return result
54+
55+
1956
def main():
2057
# Read command-line arguments
2158
parser = argparse.ArgumentParser(
2259
description="Code Base Investigator " + str(version),
60+
formatter_class=argparse.RawTextHelpFormatter,
2361
add_help=False,
2462
)
2563
parser.add_argument(
2664
"-h",
2765
"--help",
2866
action="help",
29-
help="Display help message and exit.",
67+
help=_help_string("Display help message and exit."),
3068
)
3169
parser.add_argument(
3270
"--version",
3371
action="version",
3472
version=f"Code Base Investigator {version}",
35-
help="Display version information and exit.",
73+
help=_help_string("Display version information and exit."),
3674
)
3775
parser.add_argument(
3876
"-v",
3977
"--verbose",
4078
dest="verbose",
4179
action="count",
4280
default=0,
43-
help="Increase verbosity level.",
81+
help=_help_string("Increase verbosity level."),
4482
)
4583
parser.add_argument(
4684
"-q",
4785
"--quiet",
4886
dest="quiet",
4987
action="count",
5088
default=0,
51-
help="Decrease verbosity level.",
89+
help=_help_string("Decrease verbosity level."),
5290
)
5391
parser.add_argument(
5492
"-R",
@@ -58,9 +96,12 @@ def main():
5896
action="append",
5997
default=[],
6098
choices=["all", "summary", "clustering"],
61-
help="Generate a report of the specified type. "
62-
+ "May be specified multiple times. "
63-
+ "If not specified, all reports will be generated.",
99+
help=_help_string(
100+
"Generate a report of the specified type.",
101+
"May be specified multiple times.",
102+
"If not specified, all reports will be generated.",
103+
is_long=True,
104+
),
64105
)
65106
parser.add_argument(
66107
"-x",
@@ -69,8 +110,11 @@ def main():
69110
metavar="<pattern>",
70111
action="append",
71112
default=[],
72-
help="Exclude files matching this pattern from the code base. "
73-
+ "May be specified multiple times.",
113+
help=_help_string(
114+
"Exclude files matching this pattern from the code base.",
115+
"May be specified multiple times.",
116+
is_long=True,
117+
),
74118
)
75119
parser.add_argument(
76120
"-p",
@@ -79,15 +123,23 @@ def main():
79123
metavar="<platform>",
80124
action="append",
81125
default=[],
82-
help="Include the specified platform in the analysis. "
83-
+ "May be specified multiple times. "
84-
+ "If not specified, all platforms will be included.",
126+
help=_help_string(
127+
"Include the specified platform in the analysis.",
128+
"May be specified multiple times.",
129+
"If not specified, all platforms will be included.",
130+
is_long=True,
131+
is_last=True,
132+
),
85133
)
134+
86135
parser.add_argument(
87136
"analysis_file",
88137
metavar="<analysis-file>",
89-
help="TOML file describing the analysis to be performed, "
90-
+ "including the codebase and platform descriptions.",
138+
help=_help_string(
139+
"TOML file describing the analysis to be performed, "
140+
+ "including the codebase and platform descriptions.",
141+
is_last=True,
142+
),
91143
)
92144

93145
args = parser.parse_args()

0 commit comments

Comments
 (0)