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 12import util 13from gallery_dl import option 14 15 16TEMPLATE = """_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 32complete -F _gallery_dl gallery-dl 33""" 34 35opts = [] 36diropts = [] 37fileopts = [] 38for 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 50PATH = util.path("data/completion/gallery-dl") 51with open(PATH, "w", encoding="utf-8") as file: 52 file.write(TEMPLATE % { 53 "opts" : " ".join(opts), 54 "diropts" : "|".join(diropts), 55 "fileopts": "|".join(fileopts), 56 }) 57