MCPcopy Index your code
hub / github.com/python/cpython / _format_usage

Method _format_usage

Lib/argparse.py:320–408  ·  view source on GitHub ↗
(self, usage, actions, groups, prefix)

Source from the content-addressed store, hash-verified

318 if part and part is not SUPPRESS])
319
320 def _format_usage(self, usage, actions, groups, prefix):
321 t = self._theme
322
323 if prefix is None:
324 prefix = _('usage: ')
325
326 # if usage is specified, use that
327 if usage is not None:
328 usage = (
329 t.prog_extra
330 + usage
331 % {"prog": f"{t.prog}{self._prog}{t.reset}{t.prog_extra}"}
332 + t.reset
333 )
334
335 # if no optionals or positionals are available, usage is just prog
336 elif usage is None and not actions:
337 usage = f"{t.prog}{self._prog}{t.reset}"
338
339 # if optionals and positionals are available, calculate usage
340 elif usage is None:
341 prog = '%(prog)s' % dict(prog=self._prog)
342
343 parts, pos_start = self._get_actions_usage_parts(actions, groups)
344 # build full usage string
345 usage = ' '.join(filter(None, [prog, *parts]))
346
347 # wrap the usage parts if it's too long
348 text_width = self._width - self._current_indent
349 if len(prefix) + len(self._decolor(usage)) > text_width:
350
351 # break usage into wrappable parts
352 opt_parts = parts[:pos_start]
353 pos_parts = parts[pos_start:]
354
355 # helper for wrapping lines
356 def get_lines(parts, indent, prefix=None):
357 lines = []
358 line = []
359 indent_length = len(indent)
360 if prefix is not None:
361 line_len = len(prefix) - 1
362 else:
363 line_len = indent_length - 1
364 for part in parts:
365 part_len = len(self._decolor(part))
366 if line_len + 1 + part_len > text_width and line:
367 lines.append(indent + ' '.join(line))
368 line = []
369 line_len = indent_length - 1
370 line.append(part)
371 line_len += part_len + 1
372 if line:
373 lines.append(indent + ' '.join(line))
374 if prefix is not None:
375 lines[0] = lines[0][indent_length:]
376 return lines
377

Callers

nothing calls this directly

Calls 7

get_linesFunction · 0.85
filterFunction · 0.70
_Function · 0.50
joinMethod · 0.45
extendMethod · 0.45
removeprefixMethod · 0.45

Tested by

no test coverage detected