MCPcopy
hub / github.com/google/python-fire / _CreateFlagItem

Function _CreateFlagItem

fire/helptext.py:452–511  ·  view source on GitHub ↗

Returns a string describing a flag using docstring and FullArgSpec info. Args: flag: The name of the flag. docstring_info: A docstrings.DocstringInfo namedtuple with information about the containing function's docstring. spec: An instance of fire.inspectutils.FullArgSpec, contai

(flag, docstring_info, spec, required=False,
                    flag_string=None, short_arg=False)

Source from the content-addressed store, hash-verified

450
451
452def _CreateFlagItem(flag, docstring_info, spec, required=False,
453 flag_string=None, short_arg=False):
454 """Returns a string describing a flag using docstring and FullArgSpec info.
455
456 Args:
457 flag: The name of the flag.
458 docstring_info: A docstrings.DocstringInfo namedtuple with information about
459 the containing function's docstring.
460 spec: An instance of fire.inspectutils.FullArgSpec, containing type and
461 default information about the arguments to a callable.
462 required: Whether the flag is required.
463 flag_string: If provided, use this string for the flag, rather than
464 constructing one from the flag name.
465 short_arg: Whether the flag has a short variation or not.
466 Returns:
467 A string to be used in constructing the help screen for the function.
468 """
469 # pylint: disable=g-bad-todo
470 # TODO(MichaelCG8): Get type and default information from docstrings if it is
471 # not available in FullArgSpec. This will require updating
472 # fire.docstrings.parser().
473
474 # The help string is indented, so calculate the maximum permitted length
475 # before indentation to avoid exceeding the maximum line length.
476 max_str_length = LINE_LENGTH - SECTION_INDENTATION - SUBSECTION_INDENTATION
477
478 description = _GetArgDescription(flag, docstring_info)
479
480 if not flag_string:
481 flag_name_upper = formatting.Underline(flag.upper())
482 flag_string = f'--{flag}={flag_name_upper}'
483 if required:
484 flag_string += ' (required)'
485 if short_arg:
486 short_flag = flag[0]
487 flag_string = f'-{short_flag}, {flag_string}'
488
489 arg_type = _GetArgType(flag, spec)
490 arg_default = _GetArgDefault(flag, spec)
491
492 # We need to handle the case where there is a default of None, but otherwise
493 # the argument has another type.
494 if arg_default == 'None':
495 arg_type = f'Optional[{arg_type}]'
496
497 arg_type = f'Type: {arg_type}' if arg_type else ''
498 available_space = max_str_length - len(arg_type)
499 arg_type = (
500 formatting.EllipsisTruncate(arg_type, available_space, max_str_length))
501
502 arg_default = f'Default: {arg_default}' if arg_default else ''
503 available_space = max_str_length - len(arg_default)
504 arg_default = (
505 formatting.EllipsisTruncate(arg_default, available_space, max_str_length))
506
507 description = '\n'.join(
508 part for part in (arg_type, arg_default, description) if part
509 )

Callers 2

_ArgsAndFlagsSectionsFunction · 0.85

Calls 5

_GetArgDescriptionFunction · 0.85
_GetArgTypeFunction · 0.85
_GetArgDefaultFunction · 0.85
_CreateItemFunction · 0.85
upperMethod · 0.80

Tested by

no test coverage detected