diff --git a/python_on_whales/client_config.py b/python_on_whales/client_config.py index 37a1f522..da1d69f6 100644 --- a/python_on_whales/client_config.py +++ b/python_on_whales/client_config.py @@ -5,7 +5,7 @@ from dataclasses import dataclass, field from datetime import datetime, timedelta from pathlib import Path -from typing import Any, Dict, Iterable, List, Literal, Optional, Union +from typing import Any, Dict, Iterable, List, Literal, Mapping, Optional, Union import pydantic @@ -13,9 +13,9 @@ download_docker_cli, get_docker_binary_path_in_cache, ) -from python_on_whales.utils import to_list -from .utils import ValidPath, run +from . import utils +from .utils import ValidPath, run, to_list CACHE_VALIDITY_PERIOD = 0.01 @@ -37,12 +37,23 @@ def add_flag(self, name: str, value: bool): if value: self.append(name) + def add_args_iterable(self, arg_name: str, values: Iterable[Any]): + for value in values: + self.extend([arg_name, value]) + def add_args_iterable_or_single( self, arg_name: str, iterable_or_single: Union[Iterable[Any] | Any] ): for value in to_list(iterable_or_single): self.extend([arg_name, value]) + def add_args_mapping( + self, arg_name: str, mapping: Mapping[Any, Any], *, separator="=" + ): + self.add_args_iterable( + arg_name, utils.format_mapping_for_cli(mapping, separator) + ) + def __add__(self, other) -> "Command": return Command(super().__add__(other))