Код: Выделить всё
from enum import auto, Enum
class MyEnum(Enum):
ONE = auto()
TWO = auto()
THREE = auto()
Код: Выделить всё
from argparse import ArgumentParser
from enum import auto, Enum
class MyEnum(Enum):
ONE = auto()
TWO = auto()
THREE = auto()
enum_map = {e.name.lower(): e for e in MyEnum}
parser = ArgumentParser()
parser.add_argument(
"--enum",
default="two",
choices=tuple(enum_map.keys()),
help="An enum value (Default: %(default)s)",
)
args = parser.parse_args()
args.enum = enum_map[args.enum]
print(args.enum)
Код: Выделить всё
from argparse import Action, ArgumentParser, FileType, Namespace
from collections.abc import Callable, Sequence
from enum import auto, Enum
from typing import Any
class EnumAction[T](Action):
_enum: type[T]
_enum_map: dict[str, T]
def __init__(
self,
option_strings: Sequence[str],
dest: str,
nargs: int | str | None = None,
const: Any = None,
default: str | T = None,
type: Callable[[str], T] | FileType | None = None,
choices: Sequence[T] | None = None,
required: bool = False,
help: str | None = None,
metavar: str | tuple[str, ...] | None = None,
) -> None:
if type is None:
raise ValueError("type must be assigned an Enum when using EnumAction")
if not issubclass(type, Enum):
raise TypeError("type must be an Enum when using EnumAction")
if choices is not None:
raise ValueError("Can't specify choices when using EnumAction")
self._enum = type
type = None
self._enum_map = {e.name.lower(): e for e in self._enum}
choices = tuple(self._enum_map.keys())
super().__init__(
option_strings,
dest,
nargs,
const,
default,
type,
choices,
required,
help,
metavar,
)
def __call__(
self,
parser: ArgumentParser,
namespace: Namespace,
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
setattr(namespace, self.dest, self._enum_map[values])
class MyEnum(Enum):
ONE = auto()
TWO = auto()
THREE = auto()
parser = ArgumentParser()
parser.add_argument(
"--enum",
action=EnumAction,
type=MyEnum,
default="two",
help="An enum value (Default: %(default)s)",
)
print(parser.parse_args())
Код: Выделить всё
$ python demo.py --enum one
Namespace(enum=)
$ python demo.py
Namespace(enum='two')
Код: Выделить всё
$ python demo.py -h
usage: demo.py [-h] [--enum {one,two,three}]
options:
-h, --help show this help message and exit
--enum {one,two,three}
An enum value (Default: MyEnum.TWO)
Подробнее здесь: https://stackoverflow.com/questions/787 ... ult-values
Мобильная версия