Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/scripts/completion_zsh.py
5457 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
# Copyright 2020 Mike Fährmann
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License version 2 as
8
# published by the Free Software Foundation.
9
10
"""Generate zsh completion script from gallery-dl's argument parser"""
11
12
import util
13
import argparse
14
from gallery_dl import option
15
16
TEMPLATE = """#compdef gallery-dl
17
18
local curcontext="$curcontext"
19
typeset -A opt_args
20
21
local rc=1
22
_arguments -s -S \\
23
%(opts)s && rc=0
24
25
return rc
26
"""
27
28
TR = str.maketrans({
29
"'": "'\\''",
30
"[": "\\[",
31
"]": "\\]",
32
})
33
34
35
opts = []
36
for action in option.build_parser()._actions:
37
38
if not action.option_strings or action.help == argparse.SUPPRESS:
39
continue
40
elif len(action.option_strings) == 1:
41
opt = action.option_strings[0]
42
else:
43
opt = "{" + ",".join(action.option_strings) + "}"
44
45
opt += "'[" + action.help.translate(TR) + "]'"
46
47
if action.metavar:
48
opt += ":'<" + action.metavar.lower() + ">'"
49
if action.metavar in ("FILE", "CFG", "DEST"):
50
opt += ":_files"
51
52
opts.append(opt)
53
54
55
PATH = util.path("data/completion/_gallery-dl")
56
with util.lazy(PATH) as fp:
57
fp.write(TEMPLATE % {"opts": " \\\n".join(opts)})
58
59