Add parser for comments to generate help message

This commit is contained in:
samedamci 2021-09-01 01:23:16 +02:00
parent 8efa042937
commit 9569588a9a
No known key found for this signature in database
GPG Key ID: FCB4A9A20D00E894

View File

@ -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)