scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Sun, 16 Aug 2015 01:25:01 +0200
branchv_0
changeset 220 0bc544b38cfa
parent 209 8dfe037b3274
child 221 e38910065d55
permissions -rwxr-xr-x
bash-completion: --formatter-property names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/usr/bin/perl
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
83
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
     3
# SQL-DK
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
     4
# Copyright © 2013 František Kučera (frantovo.cz)
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
     5
# 
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
     7
# it under the terms of the GNU General Public License as published by
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
     8
# the Free Software Foundation, either version 3 of the License, or
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
     9
# (at your option) any later version.
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    10
# 
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    11
# This program is distributed in the hope that it will be useful,
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    14
# GNU General Public License for more details.
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    15
# 
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    16
# You should have received a copy of the GNU General Public License
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    18
9563232ea0b7 bash completion: license
František Kučera <franta-hg@frantovo.cz>
parents: 82
diff changeset
    19
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
# Parses Java source code from STDIN and generates script for BASH completion
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
# Input (in this order):
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#	info/globalcode/sql/dk/Constants.java
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#	info/globalcode/sql/dk/formatting/*
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#	info/globalcode/sql/dk/CLIParser.java
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
82
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    26
# TODO: support database/formatter names with spaces
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    27
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
use strict;
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
use warnings;
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
my $configDir = "~";
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
while (<>) {
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		$configDir .= "/$1";
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		last;
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
82
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    40
my $databasesFile  = "$configDir/bash-completion/databases";
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    41
my $formattersFile = "$configDir/bash-completion/formatters";
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    42
my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";
82
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    43
110
ff6bbd0223c3 Bash completion: no 'have sql-dk' test
František Kučera <franta-hg@frantovo.cz>
parents: 84
diff changeset
    44
print '#have sql-dk &&
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    45
_sql_dk_bash_completion_find_formatter() {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    46
	local previous
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    47
	for token in "$@"; do
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    48
		if [ "x$previous" == "x--formatter" ]; then
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    49
			echo -n "$token";
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    50
		fi
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    51
		previous="$token";
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    52
	done
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    53
}
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    54
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    55
_sql_dk_bash_completion_formatter_property() {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    56
	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    57
		local formatter_dir='.$formatterPropertiesDir.'/$formatter
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    58
		if [ -d  $formatter_dir ]; then
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    59
			ls -1 $formatter_dir;
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    60
		fi
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    61
	fi
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    62
}
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    63
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    64
_sql_dk_bash_completion_formatter_property_choice() {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    65
	local property="${COMP_WORDS[COMP_CWORD-1]}";
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    66
	echo true;
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    67
	echo false;
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    68
	echo "TODO_$property";
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    69
	# TODO: find choices for the property  
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    70
}
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    71
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    72
_sql_dk_bash_completion() {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
    73
	local cur prev formatter
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	COMPREPLY=()
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	cur=${COMP_WORDS[COMP_CWORD]}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	prev=${COMP_WORDS[COMP_CWORD-1]}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	case "$prev" in
159
9632b23df30c InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
František Kučera <franta-hg@frantovo.cz>
parents: 110
diff changeset
    80
	--db | --test-connection | --list-jdbc-properties)
82
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    81
		if [ -f '.$databasesFile.' ]; then
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    82
			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    83
			return 0
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    84
		fi
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		;;
209
8dfe037b3274 property annotations: grounds for --list-formatter-properties
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    86
	--formatter | --list-formatter-properties)
82
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    87
		if [ -f '.$formattersFile.' ]; then
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    88
			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    89
		else
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    90
			COMPREPLY=( $( compgen -W "
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
';
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
while (<>) {
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
82
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
    95
		print "				$1\n";
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	last if (/\/\/\s*bash-completion:options/);
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
82
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
   101
print '				" -- $cur ) );
adea6d8d3708 bash completion: use dynamic completion derived from configuration if available
František Kučera <franta-hg@frantovo.cz>
parents: 81
diff changeset
   102
		fi
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		return 0
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
		;;
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   105
	--formatter-property)
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   106
		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   107
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property )" -- $cur ) ); 
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   108
		return 0;
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   109
		;;
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
	esac;
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   111
	
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   112
	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   113
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   114
		return 0;
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   115
	fi
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
	COMPREPLY=( $( compgen -W "
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
';
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
while (<>) {
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
		print "			$1\n";
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
	}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
print '		" -- $cur ) )
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
	return 0
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
}
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   131
complete -F _sql_dk_bash_completion sql-dk
81
847c83288d01 bash completion: perl + bash + ant for generating completion script
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
';