From 8f39257fd53a068ce6c4e6c2efeffea0bd27fb90 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 3 Dec 2010 23:03:59 +0900 Subject: [PATCH] Added aria2c bash_completion. make_bash_completion.py generates aria2c bash_completion using `aria2c --help=#all'. --- doc/Makefile.am | 3 + doc/bash_completion/README.txt | 11 ++ doc/bash_completion/aria2c | 74 +++++++++ doc/bash_completion/make_bash_completion.py | 162 ++++++++++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 doc/bash_completion/README.txt create mode 100644 doc/bash_completion/aria2c create mode 100755 doc/bash_completion/make_bash_completion.py diff --git a/doc/Makefile.am b/doc/Makefile.am index f330bd3e..bdc0dffe 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -5,6 +5,9 @@ doc_xmlrpcdir = $(docdir)/xmlrpc dist_doc_xmlrpc_DATA = xmlrpc/aria2mon \ xmlrpc/aria2rpc \ xmlrpc/README.txt +doc_bashcompletiondir = $(docdir)/bash_completion +dist_doc_bashcompletion_DATA = bash_completion/README.txt \ + bash_completion/aria2c if HAVE_A2X aria2c.1: aria2c.1.asciidoc diff --git a/doc/bash_completion/README.txt b/doc/bash_completion/README.txt new file mode 100644 index 00000000..785029c1 --- /dev/null +++ b/doc/bash_completion/README.txt @@ -0,0 +1,11 @@ +Bash_completion for aria2c +========================== + +Install +------- + +Copy 'aria2c' to the directory where bash_completion searches +completion files. For Debian and Ubuntu, copy 'aria2c' to +'/etc/bash_completion.d/' and run + +. /etc/bash_completion diff --git a/doc/bash_completion/aria2c b/doc/bash_completion/aria2c new file mode 100644 index 00000000..1fd4fdbd --- /dev/null +++ b/doc/bash_completion/aria2c @@ -0,0 +1,74 @@ +_aria2c() +{ + local cur prev split=false + COMPREPLY=() + COMP_WORDBREAKS=${COMP_WORDBREAKS//=} + + cmd=${COMP_WORDS[0]} + _get_comp_words_by_ref cur prev + case $prev in + --ftp-type) + COMPREPLY=( $( compgen -W 'binary ascii' -- "$cur" ) ) + return 0 + ;; + --proxy-method) + COMPREPLY=( $( compgen -W 'get tunnel' -- "$cur" ) ) + return 0 + ;; + --metalink-preferred-protocol) + COMPREPLY=( $( compgen -W 'http https ftp none' -- "$cur" ) ) + return 0 + ;; + --bt-min-crypto-level) + COMPREPLY=( $( compgen -W 'plain arc4' -- "$cur" ) ) + return 0 + ;; + --follow-metalink) + COMPREPLY=( $( compgen -W 'true mem false' -- "$cur" ) ) + return 0 + ;; + --file-allocation) + COMPREPLY=( $( compgen -W 'none prealloc falloc' -- "$cur" ) ) + return 0 + ;; + --log-level) + COMPREPLY=( $( compgen -W 'debug info notice warn error' -- "$cur" ) ) + return 0 + ;; + --uri-selector) + COMPREPLY=( $( compgen -W 'inorder feedback adaptive' -- "$cur" ) ) + return 0 + ;; + --event-poll) + COMPREPLY=( $( compgen -W 'epoll poll select' -- "$cur" ) ) + return 0 + ;; + --follow-torrent) + COMPREPLY=( $( compgen -W 'true mem false' -- "$cur" ) ) + return 0 + ;; + --dir) + _filedir -d + return 0 + ;; + --torrent-file) + _filedir '@(torrent)' + return 0 + ;; + --metalink-file) + _filedir '@(meta4|metalink)' + return 0 + ;; + esac + case $cur in + -*) + COMPREPLY=( $( compgen -W '--on-download-start --metalink-language --torrent-file --enable-peer-exchange --enable-peer-exchange=false --http-proxy-passwd --bt-tracker-timeout --ftp-type --seed-time --bt-tracker-connect-timeout --no-netrc --no-netrc=false --force-sequential --force-sequential=false --private-key --xml-rpc-passwd --ftp-passwd --allow-overwrite --allow-overwrite=false --summary-interval --lowest-speed-limit --proxy-method --metalink-preferred-protocol --metalink-version --listen-port --bt-tracker-interval --server-stat-of --server-stat-timeout --https-proxy-user --http-no-cache --http-no-cache=false --dry-run --dry-run=false --bt-max-peers --max-overall-upload-limit --xml-rpc-max-request-size --dht-entry-point6 --dir --split --on-download-pause --auto-file-renaming --auto-file-renaming=false --http-proxy --daemon --daemon=false --https-proxy --save-cookies --out --max-file-not-found --on-download-stop --enable-xml-rpc --enable-xml-rpc=false --certificate --bt-min-crypto-level --remove-control-file --remove-control-file=false --enable-dht --enable-dht=false --timeout --follow-metalink --on-bt-download-complete --xml-rpc-user --ftp-proxy --show-files --show-files=false --metalink-servers --file-allocation --bt-hash-check-seed --bt-hash-check-seed=false --ftp-pasv --ftp-pasv=false --check-certificate --check-certificate=false --always-resume --always-resume=false --load-cookies --bt-stop-timeout --max-concurrent-downloads --quiet --quiet=false --xml-rpc-listen-all --xml-rpc-listen-all=false --http-auth-challenge --http-auth-challenge=false --max-resume-failure-tries --help --help=#basic --help=#advanced --help=#http --help=#https --help=#ftp --help=#metalink --help=#bittorrent --help=#cookie --help=#hook --help=#file --help=#xml-rpc --help=#experimental --help=#help --help=#all --auto-save-interval --all-proxy-user --server-stat-if --dht-file-path6 --save-session --bt-external-ip --max-tries --conditional-get --conditional-get=false --ftp-reuse-connection --ftp-reuse-connection=false --bt-max-open-files --async-dns --async-dns=false --bt-prioritize-piece --check-integrity --check-integrity=false --log-level --remote-time --remote-time=false --uri-selector --index-out --bt-tracker --referer --connect-timeout --dht-message-timeout --select-file --disable-ipv6 --disable-ipv6=false --enable-http-keep-alive --enable-http-keep-alive=false --https-proxy-passwd --continue --continue=false --no-file-allocation-limit --ftp-proxy-user --metalink-location --allow-piece-length-change --allow-piece-length-change=false --max-connection-per-server --no-conf --no-conf=false --metalink-os --enable-http-pipelining --enable-http-pipelining=false --http-passwd --enable-dht6 --enable-dht6=false --dht-file-path --max-download-result --peer-id-prefix --use-head --use-head=false --bt-lpd-interface --conf-path --log --no-proxy --dht-entry-point --dht-listen-port --http-user --on-download-complete --header --max-overall-download-limit --event-poll --http-accept-gzip --http-accept-gzip=false --metalink-file --all-proxy --dht-listen-addr6 --human-readable --human-readable=false --ftp-user --all-proxy-passwd --bt-exclude-tracker --bt-require-crypto --bt-require-crypto=false --http-proxy-user --metalink-enable-unique-protocol --metalink-enable-unique-protocol=false --on-download-error --max-upload-limit --realtime-chunk-checksum --realtime-chunk-checksum=false --ca-certificate --bt-save-metadata --bt-save-metadata=false --seed-ratio --follow-torrent --xml-rpc-listen-port --version --user-agent --max-download-limit --bt-enable-lpd --bt-enable-lpd=false --enable-direct-io --enable-direct-io=false --parameterized-uri --parameterized-uri=false --ftp-proxy-passwd --min-split-size --bt-seed-unverified --bt-seed-unverified=false --input-file --interface --enable-async-dns6 --enable-async-dns6=false --reuse-uri --reuse-uri=false --bt-request-peer-speed-limit --stop --bt-metadata-only --bt-metadata-only=false ' -- "$cur" ) ) + ;; + *) + _filedir '@(torrent|meta4|metalink|text|txt|list|lst)' + [ ${#COMPREPLY[@]} -eq 0 ] && _filedir + return 0 + esac + return 0 +} +complete -F _aria2c aria2c diff --git a/doc/bash_completion/make_bash_completion.py b/doc/bash_completion/make_bash_completion.py new file mode 100755 index 00000000..2fb12eaa --- /dev/null +++ b/doc/bash_completion/make_bash_completion.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python + +import subprocess +from StringIO import StringIO +import re +import sys + +class Option: + def __init__(self, long_opt, short_opt, optional): + self.long_opt = long_opt + self.short_opt = short_opt + self.optional = optional + self.values = [] + +def get_all_options(cmd): + opt_pattern = re.compile(r' (?:(-.), )?(--[^\s\[=]+)(\[)?') + values_pattern = re.compile(r'\s+Possible Values: (.+)') + proc = subprocess.Popen([cmd, "--help=#all"], stdout=subprocess.PIPE) + stdoutdata, stderrdata = proc.communicate() + cur_option = None + opts = {} + for line in StringIO(stdoutdata): + match = opt_pattern.match(line) + if match: + long_opt = match.group(2) + short_opt = match.group(1) + optional = match.group(3) == '[' + if cur_option: + opts[cur_option.long_opt] = cur_option + cur_option = Option(long_opt, short_opt, optional) + else: + match = values_pattern.match(line) + if match: + cur_option.values = match.group(1).split(', ') + if cur_option: + opts[cur_option.long_opt] = cur_option + + # for opt in opts.itervalues(): + # print opt.short_opt, opt.long_opt, opt.optional, opt.values + + return opts + +def output_value_case(out, key, values): + out.write("""\ + {0}) + COMPREPLY=( $( compgen -W '{1}' -- "$cur" ) ) + return 0 + ;; +""".format(key, " ".join(values))) + +def output_value_case_file_comp(out, key, exts): + out.write("""\ + {0}) + _filedir '@({1})' + return 0 + ;; +""".format(key, '|'.join(exts))) + +def output_value_case_dir_comp(out, key): + out.write("""\ + {0}) + _filedir -d + return 0 + ;; +""".format(key)) + +def output_case(out, opts): + out.write("""\ +_aria2c() +{ + local cur prev split=false + COMPREPLY=() + COMP_WORDBREAKS=${COMP_WORDBREAKS//=} + + cmd=${COMP_WORDS[0]} + _get_comp_words_by_ref cur prev +""") + bool_opts = [] + nonbool_opts = [] + for opt in opts.itervalues(): + if opt.values == ['true', 'false']: + bool_opts.append(opt) + else: + nonbool_opts.append(opt) + + out.write("""\ + case $prev in +""") + # Complete pre-defined option arguments + for long_opt in ['--ftp-type', + '--proxy-method', + '--metalink-preferred-protocol', + '--bt-min-crypto-level', + '--follow-metalink', + '--file-allocation', + '--log-level', + '--uri-selector', + '--event-poll', + '--follow-torrent']: + opt = opts[long_opt] + output_value_case(out, opt.long_opt, opt.values) + # Complete directory + dir_opts = [] + for opt in opts.itervalues(): + if opt.values and opt.values[0] == '/path/to/directory': + dir_opts.append(opt) + # Complete file + output_value_case_dir_comp(out,'|'.join([opt.long_opt for opt in dir_opts])) + # Complete specific file type + output_value_case_file_comp(out, '--torrent-file', ['torrent']) + output_value_case_file_comp(out, '--metalink-file', ['meta4', 'metalink']) + out.write("""\ + esac +""") + # Complete option name. + out.write("""\ + case $cur in + -*) + COMPREPLY=( $( compgen -W '\ +""") + bool_values = [ 'true', 'false' ] + for opt in opts.itervalues(): + out.write(opt.long_opt) + out.write(' ') + # Options which takes optional argument needs "=" between + # option name and value, so we complete them including "=" and + # value here. + if opt.optional: + if bool_values == opt.values: + # Because boolean option takes true when argument is + # omitted, we just complete additional 'false' option + # only. + out.write('='.join([opt.long_opt, 'false'])) + out.write(' ') + else: + for value in opt.values: + out.write('='.join([opt.long_opt, value])) + out.write(' ') + + out.write("""\ +' -- "$cur" ) ) + ;; +""") + # If no option found for completion then complete with files. + out.write("""\ + *) + _filedir '@(torrent|meta4|metalink|text|txt|list|lst)' + [ ${#COMPREPLY[@]} -eq 0 ] && _filedir + return 0 + esac + return 0 +} +complete -F _aria2c aria2c +""") + +if __name__ == '__main__': + if len(sys.argv) < 2: + print "Generates aria2c(1) bash_completion using `aria2c --help=#all'" + print "Usage: make_bash_completion.py /path/to/aria2c" + exit(1) + opts = get_all_options(sys.argv[1]) + output_case(sys.stdout, opts)