From 0a4edf18531a76e8e14bb7b72531f710e1970f77 Mon Sep 17 00:00:00 2001
From: YaoZengzeng <yaozengzeng@zju.edu.cn>
Date: Tue, 6 Dec 2016 14:24:24 +0800
Subject: [PATCH] bash completion for global options and commands

Signed-off-by: Yao Zengzeng <yaozengzeng@zju.edu.cn>
---
 contrib/completion/bash/runv | 98 ++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 contrib/completion/bash/runv

diff --git a/contrib/completion/bash/runv b/contrib/completion/bash/runv
new file mode 100644
index 00000000..28b0d710
--- /dev/null
+++ b/contrib/completion/bash/runv
@@ -0,0 +1,98 @@
+#!/bin/bash
+#
+# bash completion file for core runv commands
+#
+# This script provides completion of:
+#  - global options
+#  - commands and their options
+# To enalbe the completions:
+# - place this file in /etc/bash_completion.d
+#
+
+
+# Transforms a multiline list of strings into a single line string
+# with the words separated by "|".
+__runv_to_alternatives() {
+	local parts=( $1 )
+	local IFS='|'
+	echo "${parts[*]}"
+}
+
+# Transforms a multiline list of options into an extglob pattern
+# suitable for use in case statements
+__runv_to_extglob() {
+	local extglob=$( __runv_to_alternatives "$1" )
+	echo "@($extglob)"
+}
+
+# global options that may appear after the runv command
+_runv_runv() {
+	local boolean_options="
+		$global_boolean_options
+		--help -h
+		--version -v
+	"
+
+	case "$prev" in
+		$(__runv_to_extglob "$global_options_with_args") )
+			return
+		;;
+	esac
+
+	case "$cur" in
+		-*)
+			COMPREPLY=( $( compgen -W "$boolean_options $global_options_with_args" -- "$cur") )
+			;;
+		*)
+			COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
+			;;
+	esac
+}
+
+_runv() {
+	local previous_extglob_setting=$(shopt -p extglob)
+	shopt -s extglob
+
+	local commands=(
+		start
+		spec
+		exec
+		kill
+		list
+		state
+		manage
+		supervisord
+	)
+
+	local global_boolean_options="
+		--debug
+	"
+
+	local global_options_with_args="
+		--log_dir
+		--log
+		--log-format
+		--root
+		--driver
+		--default_cpus
+		--default_memory
+		--kernel
+		--initrd
+		--template
+		--vbox
+	"
+
+	COMPREPLY=()
+	local cur prev words cword
+	_get_comp_words_by_ref -n : cur prev words cword
+
+	local command='runv'
+
+	local completions_func=_runv_${command}
+	declare -F $completions_func >/dev/null && $completions_func
+
+	eval "$previous_extglob_setting"
+	return 0
+}
+
+complete -F _runv runv