scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 10:47:56 +0200
branchv_0
changeset 209 8dfe037b3274
parent 159 9632b23df30c
child 220 0bc544b38cfa
permissions -rwxr-xr-x
property annotations: grounds for --list-formatter-properties

#!/usr/bin/perl

# SQL-DK
# Copyright © 2013 František Kučera (frantovo.cz)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# Parses Java source code from STDIN and generates script for BASH completion
# Input (in this order):
#	info/globalcode/sql/dk/Constants.java
#	info/globalcode/sql/dk/formatting/*
#	info/globalcode/sql/dk/CLIParser.java

# TODO: support database/formatter names with spaces

use strict;
use warnings;

my $configDir = "~";

while (<>) {
	if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
		$configDir .= "/$1";
		last;
	}
}

my $databasesFile  = "$configDir/bash-completion/databases";
my $formattersFile = "$configDir/bash-completion/formatters";

print '#have sql-dk &&
_sql_dk()
{
	local cur prev

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
	--db | --test-connection | --list-jdbc-properties)
		if [ -f '.$databasesFile.' ]; then
			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
			return 0
		fi
		;;
	--formatter | --list-formatter-properties)
		if [ -f '.$formattersFile.' ]; then
			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
		else
			COMPREPLY=( $( compgen -W "
';

while (<>) {
	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
		print "				$1\n";
	}
	last if (/\/\/\s*bash-completion:options/);
}


print '				" -- $cur ) );
		fi
		return 0
		;;
	esac;

	COMPREPLY=( $( compgen -W "
';

while (<>) {
	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
		print "			$1\n";
	}
}

print '		" -- $cur ) )
	return 0

}

complete -F _sql_dk sql-dk
';