diff --git a/httpie/cli/utils.py b/httpie/cli/utils.py index ad27da37f7..dda2b875ac 100644 --- a/httpie/cli/utils.py +++ b/httpie/cli/utils.py @@ -1,4 +1,5 @@ import argparse +from argparse import ArgumentError from typing import Any, Callable, Generic, Iterator, Iterable, Optional, TypeVar T = TypeVar('T') @@ -44,7 +45,6 @@ def __init__( self._help: Optional[str] = None self._obj: Optional[Iterable[T]] = None super().__init__(*args, **kwargs) - self.choices = self def load(self) -> T: if self._obj is None or not self.cache: @@ -76,4 +76,9 @@ def __iter__(self) -> Iterator[T]: return iter(self.load()) def __call__(self, parser, namespace, values, option_string=None): + if values not in self.load(): + raise ArgumentError( + self, + f"invalid choice: {values!r} (choose from {self.load()!r})" + ) setattr(namespace, self.dest, values)