Skip to content

Commit d4b41af

Browse files
Merge pull request #1134 from Backblaze/console-tool-parser
Avoid eager help message evaluation during parser construction
2 parents 6b99ad1 + 4c3ac4c commit d4b41af

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

b2/_internal/arg_parser.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,10 @@ def __init__(
9797
:param for_docs: is this parser used for generating docs
9898
:param custom_deprecated: is this command deprecated?
9999
"""
100-
self._raw_description = None
101-
self._description = None
100+
self._raw_description: str | None = None
101+
self._description: str | None = None
102102
self._for_docs = for_docs
103103
self.deprecated = custom_deprecated
104-
self._short_description = self._make_short_description(
105-
kwargs.get('usage', ''), kwargs.get('description', '')
106-
)
107104
kwargs.setdefault('formatter_class', B2RawTextHelpFormatter)
108105
super().__init__(*args, **kwargs)
109106

@@ -139,17 +136,13 @@ def _encode_description(self, value: str):
139136
# TODO-REMOVE-BY: When rst2ansi is updated or replaced
140137
return textwrap.dedent(value)
141138

142-
def _make_short_description(self, usage: str, raw_description: str) -> str:
143-
if usage:
144-
return usage
145-
146-
if not raw_description:
139+
def _get_short_description(self) -> str:
140+
if not self._raw_description:
147141
return ''
148-
149-
for line in raw_description.splitlines():
150-
if line.strip():
151-
return self._encode_description(line.strip())
152-
142+
for line in str(self._raw_description).splitlines():
143+
cleaned_line = line.strip()
144+
if cleaned_line:
145+
return self._encode_description(cleaned_line)
153146
return ''
154147

155148
def error(self, message):
@@ -217,11 +210,15 @@ def _hide_duplicated_action_choices(self, action):
217210
action.choices = original_choices
218211

219212
def format_usage(self, use_short_description: bool = False, col_length: int = 16):
220-
if not use_short_description or not self._short_description:
213+
if not use_short_description:
214+
return super().format_usage()
215+
216+
short_description = self._get_short_description()
217+
if not short_description:
221218
return super().format_usage()
222219

223220
formatter = self._get_formatter()
224-
formatter.add_text(f'{self.prog:{col_length + 2}} {self._short_description}')
221+
formatter.add_text(f'{self.prog:{col_length + 2}} {short_description}')
225222
return formatter.format_help()
226223

227224

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid eager calculation of command help during parser construction.

0 commit comments

Comments
 (0)