Skip to content

Commit 7253591

Browse files
committed
Initial commit for CLI scaffolding.
1 parent cf6aade commit 7253591

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ requires-python = ">=3.9"
1818
dependencies = [
1919
]
2020

21+
[project.scripts]
22+
fibad = "fibad_cli.main:main"
23+
fibad-train = "fibad_cli.train:main"
24+
fibad-predict = "fibad_cli.predict:main"
25+
2126
[project.urls]
2227
"Source Code" = "https://github.com/lincc-frameworks/fibad"
2328

src/fibad/predict.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Scaffolding placeholder for prediction code."""
2+
3+
4+
def run(args):
5+
"""Placeholder for prediction code.
6+
7+
Parameters
8+
----------
9+
args : argparse.Namespace
10+
The parsed command line arguments.
11+
"""
12+
13+
print("Prending to run prediction...")
14+
print(f"Runtime config: {args.runtime_config}")

src/fibad/train.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Scaffolding placeholder for training code."""
2+
3+
4+
def run(args):
5+
"""Placeholder for training code.
6+
7+
Parameters
8+
----------
9+
args : argparse.Namespace
10+
The parsed command line arguments.
11+
"""
12+
13+
print("Prending to run training...")
14+
print(f"Runtime config: {args.runtime_config}")

src/fibad_cli/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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()

src/fibad_cli/predict.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import argparse
2+
3+
4+
def main():
5+
"""Argument parser to process command line arguments when predicting with Fibad."""
6+
7+
parser = argparse.ArgumentParser(description="Predicting with Fibad.")
8+
9+
parser.add_argument("-c", "--runtime-config", type=str, help="Full path to runtime config file")
10+
11+
args = parser.parse_args()
12+
13+
run(args)
14+
15+
16+
def run(args):
17+
"""Note: Don't import anything from Fibad outside of this run function.
18+
Keeping all the imports inside the run function ensures that the --help option
19+
returns quickly without loading all the dependencies.
20+
"""
21+
from fibad.predict import run
22+
23+
run(args)
24+
25+
26+
if __name__ == "__main__":
27+
main()

src/fibad_cli/train.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import argparse
2+
3+
4+
def main():
5+
"""Argument parser to process command line arguments when training with Fibad."""
6+
7+
parser = argparse.ArgumentParser(description="Training with Fibad.")
8+
9+
parser.add_argument("-c", "--runtime-config", type=str, help="Full path to runtime config file")
10+
11+
args = parser.parse_args()
12+
13+
run(args)
14+
15+
16+
def run(args):
17+
"""Note: Don't import anything from Fibad outside of this run function.
18+
Keeping all the imports inside the run function ensures that the --help option
19+
returns quickly without loading all the dependencies.
20+
"""
21+
from fibad.train import run
22+
23+
run(args)
24+
25+
26+
if __name__ == "__main__":
27+
main()

0 commit comments

Comments
 (0)