Skip to content

Commit

Permalink
scoffolding subcommand args with argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
kefeimo committed Dec 15, 2022
1 parent 8f4ce41 commit 70e4bc3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
55 changes: 40 additions & 15 deletions src/dnp3demo/__main__2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
control_workflow_demo_master
import argparse

from dnp3demo import run_master


def main():

Expand All @@ -15,30 +17,53 @@ 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.)",
type=int,
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__":
Expand Down
29 changes: 14 additions & 15 deletions src/dnp3demo/run_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -41,21 +35,26 @@ def arg_config() -> argparse.ArgumentParser:
parser.add_argument("-oid", "--outstation-id", action="store", default=2, type=int,
metavar="<ID>")

# # 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,
Expand Down

0 comments on commit 70e4bc3

Please sign in to comment.