Debian Codegraph @

test/lib/ansible_test/_internal/cli/argparsing/actions.py

raw
1 """Actions for argparse."""
2
3 from __future__ import annotations
4
5 import argparse
6 import enum
7 import typing as t
8
9
10 class EnumAction(argparse.Action):
11 """Parse an enum using the lowercase enum names."""
12
13 def __init__(self, **kwargs: t.Any) -> None:
14 self.enum_type: t.Type[enum.Enum] = kwargs.pop('type', None)
15 kwargs.setdefault('choices', tuple(e.name.lower() for e in self.enum_type))
16 super().__init__(**kwargs)
17
18 def __call__(self, parser, namespace, values, option_string=None):
19 value = self.enum_type[values.upper()]
20 setattr(namespace, self.dest, value)