Add parser for comments to generate help message
This commit is contained in:
parent
8efa042937
commit
9569588a9a
@ -2,9 +2,21 @@ from os import listdir, path
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from subprocess import call
|
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():
|
def main():
|
||||||
dir_path = path.join(path.dirname(path.realpath(__file__)), "commands")
|
|
||||||
commands = [x.replace(".sh", "") for x in listdir(dir_path)]
|
commands = [x.replace(".sh", "") for x in listdir(dir_path)]
|
||||||
|
|
||||||
parser = ArgumentParser(
|
parser = ArgumentParser(
|
||||||
@ -12,7 +24,13 @@ def main():
|
|||||||
description="Interactive CLI git client inside fzf.",
|
description="Interactive CLI git client inside fzf.",
|
||||||
allow_abbrev=False,
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
call(path.join(dir_path, f"{args.command}.sh"), shell=True)
|
call(path.join(dir_path, f"{args.command}.sh"), shell=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user