Created
January 3, 2024 16:09
-
-
Save Pyrrha/29680dca59d13d364260f1ceb2e6d2d6 to your computer and use it in GitHub Desktop.
Version updater in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import re | |
pattern = "^(.*)(\d+).(\d+).(\d+)(.*)$" | |
def isVersion(arg, pattern=pattern): | |
if not re.match(pattern, arg): | |
raise argparse.ArgumentTypeError("invalid version pattern. Should match: [prefix]x.y.z[suffix]") | |
return arg | |
parser = argparse.ArgumentParser( | |
prog='version_updater.py', | |
description='Update a version number with given update type.') | |
parser.add_argument('version', help='version to update, should match [prefix]x.y.z[suffix]', type=isVersion) | |
parser.add_argument('update', help='number to update. Default is patch', choices=['major', 'minor', 'patch'], default='patch', nargs='?') | |
args = parser.parse_args() | |
splitted = re.match(pattern, args.version) | |
prefix, major, minor, patch, suffix = splitted.groups() | |
if args.update == 'major': | |
major, minor, patch = int(major) + 1, 0, 0 | |
elif args.update == 'minor': | |
minor, patch = int(minor) + 1, 0 | |
elif args.update == 'patch': | |
patch = int(patch) + 1 | |
print(f'{prefix}{major}.{minor}.{patch}{suffix}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment