Skip to content

Commit 5354c1f

Browse files
committed
log control to arg group
1 parent e513993 commit 5354c1f

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

sphinx/_cli/__init__.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,13 @@ def format_help(self):
9494
)
9595
help_fragments.append('\n')
9696

97-
if options := [action for action in self._optionals._group_actions
98-
if action.help != argparse.SUPPRESS]:
99-
help_fragments += [
100-
'\n',
101-
bold(underline(__('Options:'))),
102-
'\n',
103-
]
104-
for action in options:
105-
opt = self._format_option_string(action.option_strings)
106-
if action.nargs != 0:
107-
opt += ' ' + self._format_metavar(
108-
action.nargs, action.metavar, action.choices, action.dest,
109-
)
110-
help_fragments.append(opt)
111-
help_fragments.append('\n')
112-
if action_help := (action.help or '').strip():
113-
help_fragments.extend(
114-
f' {line}\n' for line in action_help.splitlines()
115-
)
97+
# self._action_groups[1] is self._optionals
98+
# Uppercase the title of the Optionals group
99+
self._optionals.title = __('Options')
100+
for argument_group in self._action_groups[1:]:
101+
if arguments := [action for action in argument_group._group_actions
102+
if action.help != argparse.SUPPRESS]:
103+
help_fragments.extend(self._format_optional_arguments(arguments, argument_group.title))
116104

117105
help_fragments += [
118106
'\n',
@@ -121,6 +109,22 @@ def format_help(self):
121109
]
122110
return ''.join(help_fragments)
123111

112+
def _format_optional_arguments(self, options: list[argparse.Action], title: str) -> Iterator[str]:
113+
yield '\n'
114+
yield bold(underline(title + ':'))
115+
yield '\n'
116+
117+
for action in options:
118+
opt = self._format_option_string(action.option_strings)
119+
if action.nargs != 0:
120+
opt += ' ' + self._format_metavar(
121+
action.nargs, action.metavar, action.choices, action.dest,
122+
)
123+
yield opt
124+
yield '\n'
125+
if action_help := (action.help or '').strip():
126+
yield from (f' {line}\n' for line in action_help.splitlines())
127+
124128
@staticmethod
125129
def _format_option_string(option_strings: Sequence[str]) -> str:
126130
# hide 'colour'
@@ -186,21 +190,24 @@ def _create_parser() -> _RootArgumentParser:
186190
default=argparse.SUPPRESS,
187191
help=__('Show this message and exit.'),
188192
)
189-
parser.add_argument(
193+
194+
# logging control
195+
log_control = parser.add_argument_group(__('Logging'))
196+
log_control.add_argument(
190197
'-v', '--verbose',
191198
action='count',
192199
dest='verbosity',
193200
default=0,
194201
help=__('Increase verbosity (can be repeated)'),
195202
)
196-
parser.add_argument(
203+
log_control.add_argument(
197204
'-q', '--quiet',
198205
action='store_const',
199206
dest='verbosity',
200207
const=-1,
201208
help=__('Only print errors and warnings.'),
202209
)
203-
parser.add_argument(
210+
log_control.add_argument(
204211
'--silent',
205212
action='store_const',
206213
dest='verbosity',

0 commit comments

Comments
 (0)