diff --git a/src/dnp3demo/__main__2.py b/src/dnp3demo/__main__2.py index 26905f5..cb9dba8 100644 --- a/src/dnp3demo/__main__2.py +++ b/src/dnp3demo/__main__2.py @@ -5,6 +5,8 @@ control_workflow_demo_master import argparse +from dnp3demo import run_master + def main(): @@ -15,18 +17,6 @@ def main(): # epilog="Thanks for using %(prog)s! :)", ) - # borrow children program arg parser - from dnp3demo import run_master - # parser = run_master.arg_config() - - # p = argparse.ArgumentParser() - s = parser.add_subparsers() - ss = s.add_parser('script', parents=[parser], add_help=False) - - print(f"====parser, {vars(parser.parse_args())}") - # print(f"====s, {vars(s.parse_args())}") - print(f"====ss, {vars(ss.parse_args())}") - # Adding optional argument parser.add_argument("-d", "--duration", action="store", help="Configure demo duration (in seconds.)", @@ -34,11 +24,46 @@ def main(): default=60, metavar="sec") + # use exclusive group, choose among scripts to run, + # by default run --demo-get-point + + group = parser.add_mutually_exclusive_group(required=False) + group.description = "some description" + group.add_argument("-rm", "--run-master-station", action="store_true", + help="Run a standalone master station.", ) + group.add_argument("-ro", "--run-outstation", action="store_true", + help="Run a standalone master station.") + group.add_argument("-dg", "--demo-get-point", action="store_true", + help="Demo get point workflow.") + group.add_argument("-ds", "--demo-set-point", action="store_true", + help="Demo set point workflow.") + group.add_argument("-rim", "--run-interactive-master", action="store_true", + help="Run an interactive master station. (For set point demo)") + + # subcommand + # Note: by using `dest="command"`, we create namespace, such that args.command + # to access subcommand + subparsers = parser.add_subparsers(title="Run Station Sub-command", + help='run-station sub-command help', + dest="command") + parser_master = subparsers.add_parser('master', help='run an interactive master') + # parser_master = subparsers + parser_master = run_master.setup_args(parser_master) + # hacky: added subcommands + # parser_master.add_argument("") + args = parser.parse_args() - print(f"1st d_args, {vars(args)}") + # args_master = parser_master.parse_args() + print(f"1st d_args args, {vars(args)}") + # print(f"1st d_args args_master, {vars(args_master)}") - from dnp3demo import run_master - run_master.main(sys.argv) + # parser.set_defaults(func=run_master.main, which="master") + # run_master.main() + cmd = args.command + if cmd == "master": + run_master.main(parser=parser) + pass + # print(f"===========cmd {cmd}") if __name__ == "__main__": diff --git a/src/dnp3demo/run_master.py b/src/dnp3demo/run_master.py index cad55ff..d74c7cd 100644 --- a/src/dnp3demo/run_master.py +++ b/src/dnp3demo/run_master.py @@ -21,13 +21,7 @@ _log.setLevel(logging.DEBUG) -def arg_config() -> argparse.ArgumentParser: - # Initialize parser - parser = argparse.ArgumentParser( - prog="dnp3-master", - description="Run a dnp3 master", - # epilog="Thanks for using %(prog)s! :)", - ) +def setup_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: # Adding optional argument parser.add_argument("-mip", "--master-ip", action="store", default="0.0.0.0", type=str, @@ -41,21 +35,26 @@ def arg_config() -> argparse.ArgumentParser: parser.add_argument("-oid", "--outstation-id", action="store", default=2, type=int, metavar="") - # # Read arguments from command line - # args = parser.parse_args() - - # choose among the following scripts to run return parser -def main(*args, **kwargs): +def main(parser=None, *args, **kwargs): + + if parser is None: + # Initialize parser + parser = argparse.ArgumentParser( + prog="dnp3-master", + description="Run a dnp3 master", + # epilog="Thanks for using %(prog)s! :)", + ) + parser = setup_args(parser) + # Read arguments from command line - parser = arg_config() args = parser.parse_args() - # or use dict + # dict to store args.Namespace d_args = vars(args) - print(d_args) + print(__name__, d_args) master_application = MyMasterNew( masterstation_ip_str=args.master_ip,