in chz/blueprint/_blueprint.py [0:0]
def get_help(self, *, color: bool = False) -> str:
"""Get help text for this Blueprint.
Note that applied arguments may affect output here, e.g. in case of polymorphically
constructed fields.
"""
r = self._make_lazy()
f = io.StringIO()
output = functools.partial(print, file=f)
try:
self._arg_map.check_extraneous(
r.used_args, r.all_params.keys(), entrypoint_repr=self.entrypoint_repr
)
except ExtraneousBlueprintArg as e:
output(f"WARNING: {e}\n")
try:
check_reference_targets(r.value_mapping, r.all_params.keys())
except InvalidBlueprintArg as e:
output(f"WARNING: {e}\n")
if r.missing_params:
output(
f"WARNING: Missing required arguments for parameter(s): {', '.join(r.missing_params)}\n"
)
output(f"Entry point: {self.entrypoint_repr}")
output()
if self.param.doc:
output(textwrap.indent(self.param.doc, " "))
output()
param_output = []
for param_path, param in r.all_params.items():
# TODO: cast or meta_factory info, not just type
found_arg = self._arg_map.get_kv(param_path)
if (
not isinstance(self.target, chz.factories.MetaFactory)
and param_path == ""
and found_arg is None
):
# If we're not using root polymorphism, skip this param
continue
if found_arg is None:
if param_path in r.meta_factory_value:
found_arg_str = type_repr(r.meta_factory_value[param_path])
if color:
found_arg_str += " \033[90m(meta_factory)\033[0m"
else:
found_arg_str += " (meta_factory)"
elif param.default is not None:
found_arg_str = param.default.to_help_str()
if color:
found_arg_str += " \033[90m(default)\033[0m"
else:
found_arg_str += " (default)"
elif (
param.meta_factory is not None
and (factory := param.meta_factory.unspecified_factory()) is not None
and factory is not param.type
):
if getattr(factory, "__name__", None) == "<lambda>":
found_arg_str = _lambda_repr(factory) or type_repr(factory)
else:
found_arg_str = type_repr(factory)
if color:
found_arg_str += " \033[90m(blueprint_unspecified)\033[0m"
else:
found_arg_str += " (blueprint_unspecified)"
else:
found_arg_str = "-"
else:
if isinstance(found_arg.value, Castable):
found_arg_str = repr(found_arg.value.value)[1:-1]
elif isinstance(found_arg.value, Reference):
found_arg_str = f"@={found_arg.value.ref}"
else:
found_arg_str = type_repr(found_arg.value)
if found_arg.layer_name:
if color:
found_arg_str += (
f" \033[90m(from \033[94m{found_arg.layer_name}\033[90m)\033[0m"
)
else:
found_arg_str += f" (from {found_arg.layer_name})"
param_output.append(
(param_path or "<entrypoint>", type_repr(param.type), found_arg_str, param.doc)
)
clip = 40
lens = tuple(min(clip, max(map(len, column))) for column in zip(*param_output))
output("Arguments:")
for p, typ, arg, doc in param_output:
col = 0
target_col = 0
line = io.StringIO()
add = functools.partial(print, file=line, end="")
raw_string = p
add(" ")
if color:
add(f"\033[1m{raw_string}\033[0m")
else:
add(raw_string)
col += 2 + len(raw_string)
target_col += 2 + lens[0]
pad = (target_col - col) if col <= target_col else (-len(raw_string)) % 20
add(" " * pad)
col += pad
raw_string = typ
add(" ")
add(raw_string)
col += 2 + len(raw_string)
target_col += 2 + lens[1]
pad = (target_col - col) if col <= target_col else (-len(raw_string)) % 20
add(" " * pad)
col += pad
raw_string = arg
add(" ")
add(raw_string)
col += 2 + len(raw_string)
target_col += 2 + lens[2]
pad = (target_col - col) if col <= target_col else (-len(raw_string)) % 20
add(" " * pad)
col += pad
raw_string = doc
add(" ")
if color:
add(f"\033[90m{raw_string}\033[0m")
else:
add(raw_string)
output(line.getvalue().rstrip())
return f.getvalue()