Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/scripts/completion_bash.py
5457 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
# Copyright 2019 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 bash completion script from gallery-dl's argument parser"""
11
12
import util
13
from gallery_dl import option
14
15
16
TEMPLATE = """_gallery_dl()
17
{
18
local cur prev
19
COMPREPLY=()
20
cur="${COMP_WORDS[COMP_CWORD]}"
21
prev="${COMP_WORDS[COMP_CWORD-1]}"
22
23
if [[ "${prev}" =~ ^(%(fileopts)s)$ ]]; then
24
COMPREPLY=( $(compgen -f -- "${cur}") )
25
elif [[ "${prev}" =~ ^(%(diropts)s)$ ]]; then
26
COMPREPLY=( $(compgen -d -- "${cur}") )
27
else
28
COMPREPLY=( $(compgen -W "%(opts)s" -- "${cur}") )
29
fi
30
}
31
32
complete -F _gallery_dl gallery-dl
33
"""
34
35
opts = []
36
diropts = []
37
fileopts = []
38
for action in option.build_parser()._actions:
39
40
if action.metavar in ("DEST",):
41
diropts.extend(action.option_strings)
42
43
elif action.metavar in ("FILE", "CFG"):
44
fileopts.extend(action.option_strings)
45
46
for opt in action.option_strings:
47
if opt.startswith("--"):
48
opts.append(opt)
49
50
PATH = util.path("data/completion/gallery-dl")
51
with util.lazy(PATH) as fp:
52
fp.write(TEMPLATE % {
53
"opts" : " ".join(opts),
54
"diropts" : "|".join(diropts),
55
"fileopts": "|".join(fileopts),
56
})
57
58