Skip to content

Commit

Permalink
Separate subcommand parsing from subcommand main
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achilleas-k committed Aug 26, 2020
1 parent 34df252 commit dea9a8f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
9 changes: 6 additions & 3 deletions nixio/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 3 additions & 15 deletions nixio/cmd/nixexplore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
14 changes: 5 additions & 9 deletions nixio/cmd/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -297,7 +297,3 @@ def main():
for task in tasklist:
task()
print("done")


if __name__ == "__main__":
main()
17 changes: 6 additions & 11 deletions nixio/cmd/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,16 @@ 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:
if os.path.exists(nixfn):
validate(nixfn)
else:
print("error: No such file '{}'".format(nixfn))


if __name__ == "__main__":
main()

0 comments on commit dea9a8f

Please sign in to comment.