Skip to content

Commit a1cffaa

Browse files
bjoernricksy0urself
authored andcommitted
Add: Add all versioning schemes to pontos.version.scheme
Implement additional function and constant variable for the CLI usage.
1 parent 2edf32d commit a1cffaa

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

pontos/version/scheme/__init__.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,53 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
from argparse import ArgumentTypeError
19+
20+
from ._pep440 import PEP440VersioningScheme
1821
from ._scheme import VersioningScheme
1922
from ._semantic import SemanticVersioningScheme
2023

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

Comments
 (0)