From d474a4065bfbc70c26f1c42be7bdf9aee72e32b5 Mon Sep 17 00:00:00 2001 From: Jon Grace-Cox <30441316+jongracecox@users.noreply.github.com> Date: Sat, 11 Jan 2025 17:14:51 -0500 Subject: [PATCH] feat: Handle bad thresholds more elegantly --- anybadge/cli.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/anybadge/cli.py b/anybadge/cli.py index 97096a0..e787bc5 100644 --- a/anybadge/cli.py +++ b/anybadge/cli.py @@ -177,8 +177,12 @@ examples: return parser.parse_args(args) -def main(args=None): - """Generate a badge based on command line arguments.""" +def main(args=None) -> int: + """Generate a badge based on command line arguments. + + Returns: + int: 0 if successful, 1 otherwise. + """ # Args may be sent from command line of as args directly. if not args: @@ -186,7 +190,7 @@ def main(args=None): if args == ["--version"]: print(anybadge_version) - return + return 0 # Parse command line arguments args = parse_args(args) @@ -207,8 +211,13 @@ def main(args=None): suffix = style.suffix # Create threshold list from args - threshold_list = [x.split("=") for x in threshold_text] - threshold_dict = {x[0]: x[1] for x in threshold_list} + try: + threshold_list = [x.split("=") for x in threshold_text] + threshold_dict = {x[0]: x[1] for x in threshold_list} + except Exception as e: + print(f"ERROR: Failed to parse threshold values: '{' '.join(threshold_text)}'") + print(f"ERROR: Thresholds should be in the form '=color'") + return 1 # Create badge object badge = Badge( @@ -239,6 +248,9 @@ def main(args=None): else: print(badge.badge_svg_text) + return 0 + if __name__ == "__main__": - main() + retval = main() + sys.exit(retval)