From dea9a8fa1f6815bd98d726d43b6404787150535c Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Wed, 26 Aug 2020 18:29:43 +0200 Subject: [PATCH] Separate subcommand parsing from subcommand main Each subcommand implements a create_subcmd_parsers() function that receives the parent parser and adds its own parsing subcommands and options. The main() function of each subcommand receives the top level parsed 'args' object and uses it as it did previously. Old subcommand main() flow: 1. Define parser 2. Define subparsers 3. Parse args 4. Use args to call functions and pass args. New top-level command main() flow: 1. Define top level parser 2. Define subcommand names 3. Pass subcommand parser to each subcommand 'create_subcommand_parsers()' 3.1 Each subcommand defines its subparser 4. Parse args 5. Use args to call subcommand and pass args to subcommand main() 5.1 Subcommand main() uses args to call appropriate function and validate options. --- nixio/cmd/main.py | 9 ++++++--- nixio/cmd/nixexplore.py | 18 +++--------------- nixio/cmd/upgrade.py | 14 +++++--------- nixio/cmd/validate.py | 17 ++++++----------- 4 files changed, 20 insertions(+), 38 deletions(-) diff --git a/nixio/cmd/main.py b/nixio/cmd/main.py index 78aa312d..5a55d64c 100644 --- a/nixio/cmd/main.py +++ b/nixio/cmd/main.py @@ -20,13 +20,16 @@ def main(): dest="cmd") # nixio explore - subcmds.add_parser("explore", help=explore.__doc__) + explore_cmd = subcmds.add_parser("explore", help=explore.__doc__) + explore.create_subcmd_parsers(explore_cmd) # nixio validate - subcmds.add_parser("validate", help=validate.__doc__) + validate_cmd = subcmds.add_parser("validate", help=validate.__doc__) + validate.create_subcmd_parser(validate_cmd) # nixio upgrade - subcmds.add_parser("upgrade", help=upgrade.__doc__) + upgrade_cmd = subcmds.add_parser("upgrade", help=upgrade.__doc__) + upgrade.create_subcmd_parser(upgrade_cmd) cmdmap = { "explore": explore.main, diff --git a/nixio/cmd/nixexplore.py b/nixio/cmd/nixexplore.py index 62fb67f4..5001eae0 100644 --- a/nixio/cmd/nixexplore.py +++ b/nixio/cmd/nixexplore.py @@ -652,13 +652,10 @@ def create_file_parser(parent_parser): file_parser.set_defaults(func=file_worker) -def create_parser(): - parser = argparse.ArgumentParser(prog="nixio-explore", - description=general_help) +def create_subcmd_parsers(parser): subparsers = parser.add_subparsers(title="commands", help="Sub commands for working on data and metadata", - description=tool_description, dest="func") - + description=tool_description, dest="explore_cmd") create_file_parser(subparsers) create_data_parser(subparsers) create_dump_parser(subparsers) @@ -668,14 +665,5 @@ def create_parser(): return parser -def main(): - parser = create_parser() - args = parser.parse_args() - if not args.func: - parser.print_help() - sys.exit(1) +def main(args): args.func(args) - - -if __name__ == "__main__": - main() diff --git a/nixio/cmd/upgrade.py b/nixio/cmd/upgrade.py index 0a747bc1..7f72e23c 100644 --- a/nixio/cmd/upgrade.py +++ b/nixio/cmd/upgrade.py @@ -249,15 +249,15 @@ def collect_tasks(fname): return tasks -def main(): - parser = argparse.ArgumentParser( - description="Upgrade NIX files to newest version" - ) +def create_subcmd_parser(parser): parser.add_argument("-f", "--force", action="store_true", help="overwrite existing files without prompting") parser.add_argument("file", type=str, nargs="+", help="path to file to upgrade (at least one)") - args = parser.parse_args() + return parser + + +def main(args): filenames = args.file tasks = dict() @@ -297,7 +297,3 @@ def main(): for task in tasklist: task() print("done") - - -if __name__ == "__main__": - main() diff --git a/nixio/cmd/validate.py b/nixio/cmd/validate.py index e580a154..b9396dd2 100644 --- a/nixio/cmd/validate.py +++ b/nixio/cmd/validate.py @@ -40,13 +40,12 @@ def validate(filename): print() -def main(): - parser = argparse.ArgumentParser( - description="Validate NIX files" - ) - parser.add_argument("file", type=str, nargs="+", - help="path to file to validate (at least one)") - args = parser.parse_args() +def create_subcmd_parser(parser): + return parser.add_argument("file", type=str, nargs="+", + help="path to file to validate (at least one)") + + +def main(args): filenames = args.file for nixfn in filenames: @@ -54,7 +53,3 @@ def main(): validate(nixfn) else: print("error: No such file '{}'".format(nixfn)) - - -if __name__ == "__main__": - main()