scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Thu, 02 Jan 2014 20:12:45 +0100
branchv_0
changeset 115 0f237fbb08a5
parent 110 ff6bbd0223c3
child 159 9632b23df30c
permissions -rwxr-xr-x
basic XSD

#!/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)
		if [ -f '.$databasesFile.' ]; then
			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
			return 0
		fi
		;;
	--formatter)
		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
';