From 9569588a9a5558c6a2b2d6a5b266ae46b9270cfd Mon Sep 17 00:00:00 2001 From: samedamci Date: Wed, 1 Sep 2021 01:23:16 +0200 Subject: [PATCH] Add parser for comments to generate help message --- guzzy/__init__.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/guzzy/__init__.py b/guzzy/__init__.py index 75d5289..243c967 100644 --- a/guzzy/__init__.py +++ b/guzzy/__init__.py @@ -2,9 +2,21 @@ from os import listdir, path from argparse import ArgumentParser from subprocess import call +dir_path = path.join(path.dirname(path.realpath(__file__)), "commands") + + +def parse_comments(command: str) -> dict: + with open(path.join(dir_path, f"{command}.sh"), "r") as f: + lines = f.read().splitlines() + comments = [ + l.replace("# ", "") + for l in lines + if l.startswith("#") and not l.startswith("#!") + ] + return {key: val.strip() for key, val in (line.split(":") for line in comments)} + def main(): - dir_path = path.join(path.dirname(path.realpath(__file__)), "commands") commands = [x.replace(".sh", "") for x in listdir(dir_path)] parser = ArgumentParser( @@ -12,7 +24,13 @@ def main(): description="Interactive CLI git client inside fzf.", allow_abbrev=False, ) - parser.add_argument("command", choices=commands) + subparsers = parser.add_subparsers(dest="command", required=True) + + subcommands = {} + for cmd in commands: + p = parse_comments(cmd) + subcommands[cmd] = subparsers.add_parser(cmd, help=p["help"]) + args = parser.parse_args() call(path.join(dir_path, f"{args.command}.sh"), shell=True)