|  | 
| 15 | 15 | # You should have received a copy of the GNU General Public License | 
| 16 | 16 | # along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
| 17 | 17 | 
 | 
|  | 18 | +from argparse import ArgumentTypeError | 
|  | 19 | + | 
|  | 20 | +from ._pep440 import PEP440VersioningScheme | 
| 18 | 21 | from ._scheme import VersioningScheme | 
| 19 | 22 | from ._semantic import SemanticVersioningScheme | 
| 20 | 23 | 
 | 
| 21 |  | -__all__ = ("VersioningScheme",) | 
|  | 24 | +__all__ = ( | 
|  | 25 | +    "VERSIONING_SCHEMES", | 
|  | 26 | +    "versioning_scheme_argument_type", | 
|  | 27 | +    "VersioningScheme", | 
|  | 28 | +    "PEP440VersioningScheme", | 
|  | 29 | +    "SemanticVersioningScheme", | 
|  | 30 | +) | 
|  | 31 | + | 
|  | 32 | +#: Dictionary with available versioning schemes | 
|  | 33 | +VERSIONING_SCHEMES = { | 
|  | 34 | +    "pep440": PEP440VersioningScheme, | 
|  | 35 | +    "semver": SemanticVersioningScheme, | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | + | 
|  | 39 | +def versioning_scheme_argument_type(value: str) -> VersioningScheme: | 
|  | 40 | +    """ | 
|  | 41 | +    Verifies if the passed value is a valid versioning scheme and returns | 
|  | 42 | +    the corresponding versioning scheme. | 
|  | 43 | +
 | 
|  | 44 | +    Intended to be used as in `ArgumentParser.add_argument` as the type. | 
|  | 45 | +
 | 
|  | 46 | +    Raises: | 
|  | 47 | +        ArgumentTypeError: If the passed value is not a valid versioning scheme | 
|  | 48 | +
 | 
|  | 49 | +    Example: | 
|  | 50 | +        .. code-block:: python | 
|  | 51 | +
 | 
|  | 52 | +            from argparse import ArgumentParser | 
|  | 53 | +            from pontos.version.scheme versioning_scheme_argument_type | 
|  | 54 | +
 | 
|  | 55 | +            parser = ArgumentParser() | 
|  | 56 | +            parser.add_argument( | 
|  | 57 | +                "--versioning-scheme", | 
|  | 58 | +                type=versioning_scheme_argument_type, | 
|  | 59 | +            ) | 
|  | 60 | +    """ | 
|  | 61 | +    try: | 
|  | 62 | +        return VERSIONING_SCHEMES[value] | 
|  | 63 | +    except KeyError: | 
|  | 64 | +        raise ArgumentTypeError( | 
|  | 65 | +            f"invalid value {value}. Expected one of " | 
|  | 66 | +            f"{', '.join(VERSIONING_SCHEMES.keys())}." | 
|  | 67 | +        ) from None | 
0 commit comments