|
| 1 | +import argparse |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from importlib.metadata import version |
| 6 | + |
| 7 | + |
| 8 | +def main(): |
| 9 | + """Primary entry point for the Fibad CLI. This handles dispatching to the various |
| 10 | + Fibad actions. The actions are defined in the pyproject.toml project.scripts |
| 11 | + section. |
| 12 | + """ |
| 13 | + |
| 14 | + description = "Fibad CLI" |
| 15 | + epilog = "FIBAD is the Framework for Image-Based Anomaly Detection" |
| 16 | + |
| 17 | + #! We could potentially make this dynamic |
| 18 | + fibad_verbs = ["train", "predict"] |
| 19 | + |
| 20 | + parser = argparse.ArgumentParser(description=description, epilog=epilog) |
| 21 | + parser.add_argument("--version", dest="version", action="store_true", help="Show version") |
| 22 | + parser.add_argument("verb", nargs="?", choices=fibad_verbs, help="Verb to execute") |
| 23 | + parser.add_argument("args", nargs=argparse.REMAINDER, help="Arguments for the verb") |
| 24 | + |
| 25 | + args = parser.parse_args() |
| 26 | + |
| 27 | + if args.version: |
| 28 | + print(version("fibad")) |
| 29 | + return |
| 30 | + |
| 31 | + if not args.verb: |
| 32 | + parser.print_help() |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + fibad_action = f"fibad-{args.verb}" |
| 36 | + |
| 37 | + # Ensure the action is available |
| 38 | + if not shutil.which(fibad_action): |
| 39 | + print(f"Error: '{fibad_action}' is not available.") |
| 40 | + print("Is the action defined in the pyproject.toml project.scripts section?") |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | + # Execute the action with the remaining arguments |
| 44 | + try: |
| 45 | + result = subprocess.run([fibad_action] + args.args, check=True) |
| 46 | + sys.exit(result.returncode) |
| 47 | + except subprocess.CalledProcessError as e: |
| 48 | + print(f"Error: Command '{fibad_action}' failed with exit code {e.returncode}.") |
| 49 | + sys.exit(e.returncode) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments