Get usage parts with split index for optionals/positionals. Returns (parts, pos_start) where pos_start is the index in parts where positionals begin. This preserves mutually exclusive group formatting across the optionals/positionals boundary (gh-75949).
(self, actions, groups)
| 411 | return len(string) > 2 |
| 412 | |
| 413 | def _get_actions_usage_parts(self, actions, groups): |
| 414 | """Get usage parts with split index for optionals/positionals. |
| 415 | |
| 416 | Returns (parts, pos_start) where pos_start is the index in parts |
| 417 | where positionals begin. |
| 418 | This preserves mutually exclusive group formatting across the |
| 419 | optionals/positionals boundary (gh-75949). |
| 420 | """ |
| 421 | actions = [action for action in actions if action.help is not SUPPRESS] |
| 422 | # group actions by mutually exclusive groups |
| 423 | action_groups = dict.fromkeys(actions) |
| 424 | for group in groups: |
| 425 | for action in group._group_actions: |
| 426 | if action in action_groups: |
| 427 | action_groups[action] = group |
| 428 | # positional arguments keep their position |
| 429 | positionals = [] |
| 430 | for action in actions: |
| 431 | if not action.option_strings: |
| 432 | group = action_groups.pop(action) |
| 433 | if group: |
| 434 | group_actions = [ |
| 435 | action2 for action2 in group._group_actions |
| 436 | if action2.option_strings and |
| 437 | action_groups.pop(action2, None) |
| 438 | ] + [action] |
| 439 | positionals.append((group.required, group_actions)) |
| 440 | else: |
| 441 | positionals.append((None, [action])) |
| 442 | # the remaining optional arguments are sorted by the position of |
| 443 | # the first option in the group |
| 444 | optionals = [] |
| 445 | for action in actions: |
| 446 | if action.option_strings and action in action_groups: |
| 447 | group = action_groups.pop(action) |
| 448 | if group: |
| 449 | group_actions = [action] + [ |
| 450 | action2 for action2 in group._group_actions |
| 451 | if action2.option_strings and |
| 452 | action_groups.pop(action2, None) |
| 453 | ] |
| 454 | optionals.append((group.required, group_actions)) |
| 455 | else: |
| 456 | optionals.append((None, [action])) |
| 457 | |
| 458 | # collect all actions format strings |
| 459 | parts = [] |
| 460 | t = self._theme |
| 461 | pos_start = None |
| 462 | for i, (required, group) in enumerate(optionals + positionals): |
| 463 | start = len(parts) |
| 464 | if i == len(optionals): |
| 465 | pos_start = start |
| 466 | in_group = len(group) > 1 |
| 467 | for action in group: |
| 468 | # produce all arg strings |
| 469 | if not action.option_strings: |
| 470 | default = self._get_default_metavar_for_positional(action) |
no test coverage detected