|
7 | 7 | import re |
8 | 8 | import sys |
9 | 9 |
|
10 | | -from argparse import HelpFormatter |
11 | | -from operator import attrgetter |
12 | 10 | from pathlib import Path |
13 | 11 | from typing import TYPE_CHECKING, TypeAlias |
14 | 12 |
|
15 | 13 | from ansible_creator.output import Level, Msg |
16 | 14 |
|
17 | | - |
18 | | -if TYPE_CHECKING: |
19 | | - from collections.abc import Iterable |
20 | | - from typing import Any |
| 15 | +from ._arg_parser_custom import CustomArgumentParser |
21 | 16 |
|
22 | 17 |
|
23 | 18 | try: |
|
40 | 35 | SubParser: TypeAlias = argparse._SubParsersAction # noqa: SLF001 |
41 | 36 |
|
42 | 37 |
|
43 | | -class CustomHelpFormatter(HelpFormatter): # pragma: no cover py>=3.14 |
44 | | - """A custom help formatter.""" |
45 | | - |
46 | | - def __init__(self, prog: str) -> None: |
47 | | - """Initialize the help formatter. |
48 | | -
|
49 | | - Args: |
50 | | - prog: The program name |
51 | | - """ |
52 | | - long_string = "--abc --really_really_really_log" |
53 | | - # 3 here accounts for the spaces in the ljust(6) below |
54 | | - HelpFormatter.__init__( |
55 | | - self, |
56 | | - prog=prog, |
57 | | - indent_increment=1, |
58 | | - max_help_position=len(long_string) + 3, |
59 | | - ) |
60 | | - |
61 | | - def _format_action_invocation( |
62 | | - self, |
63 | | - action: argparse.Action, |
64 | | - ) -> str: |
65 | | - """Format the action invocation. |
66 | | -
|
67 | | - Args: |
68 | | - action: The action to format |
69 | | -
|
70 | | - Raises: |
71 | | - ValueError: If more than 2 options are given |
72 | | -
|
73 | | - Returns: |
74 | | - The formatted action invocation |
75 | | - """ |
76 | | - if not action.option_strings: # pragma: no branch |
77 | | - default = self._get_default_metavar_for_positional(action) |
78 | | - (metavar,) = self._metavar_formatter(action, default)(1) |
79 | | - return metavar |
80 | | - |
81 | | - if len(action.option_strings) == 1: |
82 | | - return action.option_strings[0] |
83 | | - |
84 | | - max_variations = 2 |
85 | | - if len(action.option_strings) == max_variations: |
86 | | - # Account for a --1234 --long-option-name |
87 | | - return f"{action.option_strings[0].ljust(6)} {action.option_strings[1]}" |
88 | | - msg = "Too many option strings" |
89 | | - raise ValueError(msg) |
90 | | - |
91 | | - def add_arguments(self, actions: Iterable[argparse.Action]) -> None: |
92 | | - """Add arguments sorted by option strings. |
93 | | -
|
94 | | - Args: |
95 | | - actions: The actions to add |
96 | | - """ |
97 | | - actions = sorted(actions, key=attrgetter("option_strings")) |
98 | | - super().add_arguments(actions) |
99 | | - |
100 | | - |
101 | 38 | class Parser: |
102 | 39 | """A parser for the command line arguments.""" |
103 | 40 |
|
@@ -835,6 +772,16 @@ def _init_ee_project(self, subparser: SubParser[argparse.ArgumentParser]) -> Non |
835 | 772 | "Must end with .yml or .yaml. Default: execution-environment.yml", |
836 | 773 | ) |
837 | 774 |
|
| 775 | + parser.add_argument( |
| 776 | + "--ee-build-arg-default", |
| 777 | + dest="ee_build_arg_defaults", |
| 778 | + action="append", |
| 779 | + default=[], |
| 780 | + metavar="KEY=VALUE", |
| 781 | + help="Default build ARG for the EE image (repeatable). " |
| 782 | + "Example: --ee-build-arg-default ANSIBLE_GALAXY_CLI_COLLECTION_OPTS=--pre", |
| 783 | + ) |
| 784 | + |
838 | 785 | parser.add_argument( |
839 | 786 | "--registry-tls-verify", |
840 | 787 | dest="registry_tls_verify", |
@@ -943,53 +890,4 @@ def handle_deprecations(self) -> bool: # noqa: C901 |
943 | 890 | return True |
944 | 891 |
|
945 | 892 |
|
946 | | -class CustomArgumentParser(argparse.ArgumentParser): |
947 | | - """A custom argument parser.""" |
948 | | - |
949 | | - def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: ANN401 |
950 | | - """Initialize the argument parser. |
951 | | -
|
952 | | - Args: |
953 | | - *args: The arguments |
954 | | - **kwargs: The keyword arguments |
955 | | - """ |
956 | | - super().__init__(*args, **kwargs) |
957 | | - if sys.version_info < (3, 14): # pragma: no cover py>=3.14 |
958 | | - self.formatter_class = CustomHelpFormatter |
959 | | - |
960 | | - def add_argument( # type: ignore[override] |
961 | | - self, |
962 | | - *args: Any, # noqa: ANN401 |
963 | | - **kwargs: Any, # noqa: ANN401 |
964 | | - ) -> None: |
965 | | - """Add an argument. |
966 | | -
|
967 | | - Args: |
968 | | - *args: The arguments |
969 | | - **kwargs: The keyword arguments |
970 | | - """ |
971 | | - if sys.version_info < (3, 14): # pragma: no cover py>=3.14 |
972 | | - if "choices" in kwargs: # pragma: no cover py>=3.14 |
973 | | - kwargs["help"] += f" (choices: {', '.join(kwargs['choices'])})" |
974 | | - if "default" in kwargs and kwargs["default"] != "==SUPPRESS==": |
975 | | - kwargs["help"] += f" (default: {kwargs['default']})" |
976 | | - super().add_argument(*args, **kwargs) |
977 | | - |
978 | | - def add_argument_group( |
979 | | - self, |
980 | | - *args: Any, # noqa: ANN401 |
981 | | - **kwargs: Any, # noqa: ANN401 |
982 | | - ) -> argparse._ArgumentGroup: |
983 | | - """Add an argument group. |
984 | | -
|
985 | | - Args: |
986 | | - *args: The arguments |
987 | | - **kwargs: The keyword arguments |
988 | | -
|
989 | | - Returns: |
990 | | - The argument group |
991 | | - """ |
992 | | - group = super().add_argument_group(*args, **kwargs) |
993 | | - if group.title: # pragma: no cover |
994 | | - group.title = group.title.capitalize() |
995 | | - return group |
| 893 | +__all__ = ["CustomArgumentParser", "Parser"] |
0 commit comments