From ac889907f329a14336d9bde446dc614f1e601ef1 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 25 May 2024 14:16:28 +0100 Subject: [PATCH] Add Command.add_args_iterable() and Command.add_args_mapping() --- python_on_whales/client_config.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/python_on_whales/client_config.py b/python_on_whales/client_config.py index ca425f92..6709f4ab 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))