Skip to content

Commit

Permalink
make cli backwards compatible (second try)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmelly committed Mar 25, 2024
1 parent f1f0b47 commit 11a0a57
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions graxpert/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,17 @@ def main():
available_bge_versions = collect_available_versions(bge_ai_models_dir, bge_bucket_name)
available_denoise_versions = collect_available_versions(denoise_ai_models_dir, denoise_bucket_name)

parser = argparse.ArgumentParser(description="GraXpert,the astronomical background extraction tool")
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-cli", "--cli", required=False, action="store_true", help="Has to be added when using the command line integration of GraXpert")
parser.add_argument(
"-c",
"--command",
required=False,
default="background-extraction",
choices=["background-extraction", "denoising"],
type=str,
help="Choose the image operation to execute: Background Extraction or Denoising",
)
parser.add_argument("filename", type=str, help="Path of the unprocessed image")
parser.add_argument("-output", "--output", nargs="?", required=False, type=str, help="Filename of the processed image")
parser.add_argument(
Expand All @@ -195,8 +204,7 @@ def main():
)
parser.add_argument("-v", "--version", action="version", version=f"GraXpert version: {graxpert_version} release: {graxpert_release}")

subparsers = parser.add_subparsers(dest="command")
bge_parser = subparsers.add_parser("background-extraction")
bge_parser = argparse.ArgumentParser("GraXpert Background Extraction", parents=[parser], description="GraXpert, the astronomical background extraction tool")
bge_parser.add_argument(
"-ai_version",
"--ai_version",
Expand All @@ -212,7 +220,7 @@ def main():
bge_parser.add_argument("-smoothing", "--smoothing", nargs="?", required=False, default=None, type=float, help="Strength of smoothing between 0 and 1")
bge_parser.add_argument("-bg", "--bg", required=False, action="store_true", help="Also save the background model")

denoise_parser = subparsers.add_parser("denoising")
denoise_parser = argparse.ArgumentParser("GraXpert Denoising", parents=[parser], description="GraXpert, the astronomical denoising tool")
denoise_parser.add_argument(
"-ai_version",
"--ai_version",
Expand All @@ -233,25 +241,31 @@ def main():
type=float,
help='Strength of the desired denoising effect, default: "1.0"',
)


if "-h" in sys.argv or "--help" in sys.argv:
if "denoising" in sys.argv:
denoise_parser.print_help()
else:
bge_parser.print_help()
sys.exit(0)

args, extras = parser.parse_known_args()

# assume "background-extraction" as default sub-command
if args.cli is not None and args.command is None:
args.command = "background-extraction"

if args.command == "background-extraction":
args = bge_parser.parse_args()
else:
args = denoise_parser.parse_args()

if args.cli and args.command == "background-extraction":
from graxpert.cmdline_tools import BGECmdlineTool

args = bge_parser.parse_args(extras, args)
logging.info(f"Starting GraXpert CLI, Background-Extraction, version: {graxpert_version} release: {graxpert_release}")
clt = BGECmdlineTool(args)
clt.execute()
logging.shutdown()
elif args.cli and args.command == "denoising":
from graxpert.cmdline_tools import DenoiseCmdlineTool

args = denoise_parser.parse_args(extras, args)
logging.info(f"Starting GraXpert CLI, Denoising, version: {graxpert_version} release: {graxpert_release}")
clt = DenoiseCmdlineTool(args)
clt.execute()
Expand Down

0 comments on commit 11a0a57

Please sign in to comment.