The args and flags string for showing how to call a function. If positional arguments are accepted, the args will be shown as positional. E.g. "ARG1 ARG2 [--flag=FLAG]" If positional arguments are disallowed, the args will be shown with flags syntax. E.g. "--arg1=ARG1 [--flag=FLAG]" A
(spec, metadata)
| 312 | |
| 313 | |
| 314 | def _GetArgsAndFlagsString(spec, metadata): |
| 315 | """The args and flags string for showing how to call a function. |
| 316 | |
| 317 | If positional arguments are accepted, the args will be shown as positional. |
| 318 | E.g. "ARG1 ARG2 [--flag=FLAG]" |
| 319 | |
| 320 | If positional arguments are disallowed, the args will be shown with flags |
| 321 | syntax. |
| 322 | E.g. "--arg1=ARG1 [--flag=FLAG]" |
| 323 | |
| 324 | Args: |
| 325 | spec: The full arg spec for the component to construct the args and flags |
| 326 | string for. |
| 327 | metadata: Metadata for the component, including whether it accepts |
| 328 | positional arguments. |
| 329 | |
| 330 | Returns: |
| 331 | The constructed args and flags string. |
| 332 | """ |
| 333 | args_with_no_defaults = spec.args[:len(spec.args) - len(spec.defaults)] |
| 334 | args_with_defaults = spec.args[len(spec.args) - len(spec.defaults):] |
| 335 | |
| 336 | # Check if positional args are allowed. If not, require flag syntax for args. |
| 337 | accepts_positional_args = metadata.get(decorators.ACCEPTS_POSITIONAL_ARGS) |
| 338 | |
| 339 | arg_and_flag_strings = [] |
| 340 | if args_with_no_defaults: |
| 341 | if accepts_positional_args: |
| 342 | arg_strings = [formatting.Underline(arg.upper()) |
| 343 | for arg in args_with_no_defaults] |
| 344 | else: |
| 345 | arg_strings = [ |
| 346 | f'--{arg}={formatting.Underline(arg.upper())}' |
| 347 | for arg in args_with_no_defaults |
| 348 | ] |
| 349 | arg_and_flag_strings.extend(arg_strings) |
| 350 | |
| 351 | # If there are any arguments that are treated as flags: |
| 352 | if args_with_defaults or spec.kwonlyargs or spec.varkw: |
| 353 | arg_and_flag_strings.append('<flags>') |
| 354 | |
| 355 | if spec.varargs: |
| 356 | varargs_underlined = formatting.Underline(spec.varargs.upper()) |
| 357 | varargs_string = f'[{varargs_underlined}]...' |
| 358 | arg_and_flag_strings.append(varargs_string) |
| 359 | |
| 360 | return ' '.join(arg_and_flag_strings) |
| 361 | |
| 362 | |
| 363 | def _GetPossibleActions(actions_grouped_by_kind): |
no test coverage detected