#!/usr/bin/env python3 # Copyright (C) 2020-2024 Soleta Networks # # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by the # Free Software Foundation; either version 3 of the License, or # (at your option) any later version. from inspect import ismethod, getmembers from cli.cli import OgCLI import signal import argparse import json import sys def sigint_handler(signum, frame): print("User has pressed ctrl-C, interrupting...") sys.exit(1) class CLI(): def __init__(self): signal.signal(signal.SIGPIPE, signal.SIG_DFL) signal.signal(signal.SIGINT, sigint_handler) self.ogcli = OgCLI() parser = argparse.ArgumentParser(prog='ogcli') parser.add_argument('command', help='Subcommand to run', nargs='?', choices=[attr for attr, _ in getmembers(self.ogcli, lambda x: ismethod(x)) if not attr.startswith('_')]) args = parser.parse_args(sys.argv[1:2]) if args.command is None: print('Missing subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) sys.exit(1) if not hasattr(self.ogcli, args.command): print('Invalid subcommand', file=sys.stderr) parser.print_help(file=sys.stderr) sys.exit(1) # Call the command with the same name. res = getattr(self.ogcli, args.command)(sys.argv[2:]) sys.exit(res) if __name__ == "__main__": CLI()