summaryrefslogtreecommitdiff
path: root/test/manual/etags/perl-src/yagrip.pl
blob: be9f09c02d4835e027bc1100c5be7c12467f35f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#Yet Another Getopt Routine In Perl
# jgreely@cis.ohio-state.edu, 89/11/1
#usage:
#&getopt("f:bar") ||
#	die &usage("script","f:bar","oo","[files ...]");
#
sub getopt {
	local($_,$flag,$opt,$f,$r,@temp) = @_;
	@temp = split(/(.):/);
	while ($#temp >= $[) {
		$flag .= shift(@temp);
		$opt .= shift(@temp);
	}
	while ($_ = $ARGV[0], /^-(.)(.*)/ && shift(@ARGV)) {
		($f,$r) = ($1,$2);
		last if $f eq '-';
		if (index($flag,$f) >= $[) {
			eval "\$opt_$f++;";
			$r =~ /^(.)(.*)/,redo if $r ne '';
		}elsif (index($opt,$f) >= $[) {
			$r = $r eq '' ? shift(@ARGV) : $r;
			eval "\$opt_$f = \$r;";
		}else{
			print STDERR "Unrecognized switch \"-$f\".\n";
			return 0;
		}
	}
	return 1;
}

#usage: usage:
# &usage(progname,arglist,@names,@last);
#ex:
# &usage("script","f:bar","oo","[file ...]");
#would return
# "usage: script [-f oo] [-bar] [file ...]"
#
sub usage {
	local($prog,$_,@list) = @_;
	local($string,$flag,@string,@temp,@last) = ();
	@temp = split(/(.):/);
	push(@string,"usage:",$prog);
	while ($#temp >= $[) {
		if (($flag = shift(@temp)) ne '') {
			push(@string,"[-$flag]");
		}
		if (($flag = shift(@temp)) ne '') {
			push(@string,sprintf("[-%s %s]",$flag,shift(@list)));
		}
	}
	push(@string,@list) if $#list >= $[;
	return join(' ',@string) . "\n";
}
1;