1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 10:42:34 +01:00
gallery-dl/scripts/completion_zsh.py

58 lines
1.3 KiB
Python
Raw Normal View History

2020-07-04 18:05:31 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Generate zsh completion script from gallery-dl's argument parser"""
2020-07-04 18:05:31 +02:00
import util
import argparse
2020-07-04 18:05:31 +02:00
from gallery_dl import option
TEMPLATE = """#compdef gallery-dl
local curcontext="$curcontext"
typeset -A opt_args
local rc=1
_arguments -s -S \\
%(opts)s && rc=0
2020-07-04 18:05:31 +02:00
return rc
"""
TR = str.maketrans({
"'": "'\\''",
"[": "\\[",
"]": "\\]",
})
2020-07-04 18:05:31 +02:00
opts = []
for action in option.build_parser()._actions:
if not action.option_strings or action.help == argparse.SUPPRESS:
2020-07-04 18:05:31 +02:00
continue
elif len(action.option_strings) == 1:
opt = action.option_strings[0]
else:
opt = "{" + ",".join(action.option_strings) + "}"
opt += "'[" + action.help.translate(TR) + "]'"
2020-07-04 18:05:31 +02:00
if action.metavar:
opt += ":'<" + action.metavar.lower() + ">'"
if action.metavar in ("FILE", "CFG", "DEST"):
opt += ":_files"
opts.append(opt)
PATH = util.path("data/completion/_gallery-dl")
with util.lazy(PATH) as fp:
fp.write(TEMPLATE % {"opts": " \\\n".join(opts)})