Add complete for shell by shtab

you-get --print-completion bash | sudo tee /usr/share/bash-completion/completions/you-get
you-get --print-completion zsh | sudo tee /usr/share/zsh/site-functions/_you-get
you-get --print-completion tcsh|sudo tee /etc/profile.d/you-get.completion.csh
This commit is contained in:
Wu Zhenyu 2022-08-28 14:08:28 +08:00
parent 4119a1493e
commit c7c2f19dc0
4 changed files with 29 additions and 69 deletions

View File

@ -1,29 +0,0 @@
#compdef you-get
# Zsh completion definition for soimort/you-get.
setopt localoptions noshwordsplit noksharrays
local -a args
args=(
'(- : *)'{-V,--version}'[print version and exit]'
'(- : *)'{-h,--help}'[print help and exit]'
'(-i --info)'{-i,--info}'[print extracted information]'
'(-u --url)'{-u,--url}'[print extracted information with URLs]'
'(--json)--json[print extracted URLs in JSON format]'
'(-n --no-merge)'{-n,--no-merge}'[do not merge video parts]'
'(--no-caption)--no-caption[do not download captions]'
'(-f --force)'{-f,--force}'[force overwrite existing files]'
'(-F --format)'{-F,--format}'[set video format to the specified stream id]:stream id'
'(-O --output-filename)'{-O,--output-filename}'[set output filename]:filename:_files'
'(-o --output-dir)'{-o,--output-dir}'[set output directory]:directory:_files -/'
'(-p --player)'{-p,--player}'[stream extracted URL to the specified player]:player and options'
'(-c --cookies)'{-c,--cookies}'[load cookies.txt or cookies.sqlite]:cookies file:_files'
'(-x --http-proxy)'{-x,--http-proxy}'[use the specified HTTP proxy for downloading]:host\:port:'
'(-y --extractor-proxy)'{-y,--extractor-proxy}'[use the specified HTTP proxy for extraction only]:host\:port'
'(--no-proxy)--no-proxy[do not use a proxy]'
'(-t --timeout)'{-t,--timeout}'[set socket timeout]:seconds'
'(-d --debug)'{-d,--debug}'[show traceback and other debug info]'
'*: :_guard "^-*" url'
)
_arguments -S -s $args

View File

@ -1,31 +0,0 @@
# Bash completion definition for you-get.
_you-get () {
COMPREPLY=()
local IFS=$' \n'
local cur=$2 prev=$3
local -a opts_without_arg opts_with_arg
opts_without_arg=(
-V --version -h --help -i --info -u --url --json -n --no-merge
--no-caption -f --force --no-proxy -d --debug
)
opts_with_arg=(
-F --format -O --output-filename -o --output-dir -p --player
-c --cookies -x --http-proxy -y --extractor-proxy -t --timeout
)
# Do not complete non option names
[[ $cur == -* ]] || return 1
# Do not complete when the previous arg is an option expecting an argument
for opt in "${opts_with_arg[@]}"; do
[[ $opt == $prev ]] && return 1
done
# Complete option names
COMPREPLY=( $(compgen -W "${opts_without_arg[*]} ${opts_with_arg[*]}" \
-- "$cur") )
return 0
}
complete -F _you-get you-get

View File

@ -43,6 +43,7 @@ setup(
entry_points = {'console_scripts': proj_info['console_scripts']},
install_requires=["shtab"],
extras_require={
'socks': ['PySocks'],
}

View File

@ -15,6 +15,7 @@ from http import cookiejar
from importlib import import_module
from urllib import request, parse, error
import shtab
from .version import __version__
from .util import log, term
from .util.git import get_version
@ -125,6 +126,23 @@ SITES = {
'zhibo' : 'zhibo',
'zhihu' : 'zhihu',
}
URL = {
"zsh": "_urls",
}
HOST = {
"zsh": "_hosts",
}
HOST_USER = {
"zsh": "_hosts_users",
}
# https://github.com/zsh-users/zsh/blob/master/Etc/completion-style-guide#L320-L323
PREAMBLE = {
"zsh": """\
_hosts_users() {
_alternative 'hosts: :_hosts' 'users: :_users'
}
""",
}
dry_run = False
json_output = False
@ -1525,6 +1543,7 @@ def script_main(download, download_playlist, **kwargs):
description='A tiny downloader that scrapes the web',
add_help=False,
)
shtab.add_argument_to(parser, preamble=PREAMBLE)
parser.add_argument(
'-V', '--version', action='store_true',
help='Print version and exit'
@ -1544,7 +1563,7 @@ def script_main(download, download_playlist, **kwargs):
dry_run_grp.add_argument(
'-u', '--url', action='store_true',
help='Print extracted information with URLs'
)
).complete = URL
dry_run_grp.add_argument(
'--json', action='store_true',
help='Print extracted URLs in JSON format'
@ -1577,11 +1596,11 @@ def script_main(download, download_playlist, **kwargs):
)
download_grp.add_argument(
'-O', '--output-filename', metavar='FILE', help='Set output filename'
)
).complete = shtab.FILE
download_grp.add_argument(
'-o', '--output-dir', metavar='DIR', default='.',
help='Set output directory'
)
).complete = shtab.DIR
download_grp.add_argument(
'-p', '--player', metavar='PLAYER',
help='Stream extracted URL to a PLAYER'
@ -1589,7 +1608,7 @@ def script_main(download, download_playlist, **kwargs):
download_grp.add_argument(
'-c', '--cookies', metavar='COOKIES_FILE',
help='Load cookies.txt or cookies.sqlite'
)
).complete = shtab.FILE
download_grp.add_argument(
'-t', '--timeout', metavar='SECONDS', type=int, default=600,
help='Set socket timeout'
@ -1601,7 +1620,7 @@ def script_main(download, download_playlist, **kwargs):
download_grp.add_argument(
'-I', '--input-file', metavar='FILE', type=argparse.FileType('r'),
help='Read non-playlist URLs from FILE'
)
).complete = shtab.FILE
download_grp.add_argument(
'-P', '--password', help='Set video visit password to PASSWORD'
)
@ -1639,18 +1658,18 @@ def script_main(download, download_playlist, **kwargs):
proxy_grp.add_argument(
'-x', '--http-proxy', metavar='HOST:PORT',
help='Use an HTTP proxy for downloading'
)
).complete = HOST
proxy_grp.add_argument(
'-y', '--extractor-proxy', metavar='HOST:PORT',
help='Use an HTTP proxy for extracting only'
)
).complete = HOST
proxy_grp.add_argument(
'--no-proxy', action='store_true', help='Never use a proxy'
)
proxy_grp.add_argument(
'-s', '--socks-proxy', metavar='HOST:PORT or USERNAME:PASSWORD@HOST:PORT',
help='Use an SOCKS5 proxy for downloading'
)
).complete = HOST_USER
download_grp.add_argument('--stream', help=argparse.SUPPRESS)
download_grp.add_argument('--itag', help=argparse.SUPPRESS)
@ -1659,7 +1678,7 @@ def script_main(download, download_playlist, **kwargs):
help = 'download video using an m3u8 url')
parser.add_argument('URL', nargs='*', help=argparse.SUPPRESS)
parser.add_argument('URL', nargs='*').complete = URL
args = parser.parse_args()