File: DbGetopt.pm

package info (click to toggle)
ns2 2.35%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,796 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (229 lines) | stat: -rwxr-xr-x 5,572 bytes parent folder | download | duplicates (8)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/home/johnh/BIN/perl5 -w

#
# DbGetopt.pm
# Copyright (C) 1995-1998 by John Heidemann <johnh@ficus.cs.ucla.edu>
# $Id: DbGetopt.pm,v 1.2 2005/09/16 04:41:55 tomh Exp $
#
# This program is distributed under terms of the GNU general
# public license, version 2.  See the file COPYING
# in $dblib for details.
# 
# The copyright of this module includes the following
# linking-with-specific-other-licenses addition:
# 
# In addition, as a special exception, the copyright holders of
# this module give you permission to combine (via static or
# dynamic linking) this module with free software programs or
# libraries that are released under the GNU LGPL and with code
# included in the standard release of ns-2 under the Apache 2.0
# license or under otherwise-compatible licenses with advertising
# requirements (or modified versions of such code, with unchanged
# license).  You may copy and distribute such a system following the
# terms of the GNU GPL for this module and the licenses of the
# other code concerned, provided that you include the source code of
# that other code when and as the GNU GPL requires distribution of
# source code.
# 
# Note that people who make modified versions of this module
# are not obligated to grant this special exception for their
# modified versions; it is their choice whether to do so.  The GNU
# General Public License gives permission to release a modified
# version without this exception; this exception also makes it
# possible to release a modified version which carries forward this
# exception.
#
#

package DbGetopt;

=head1 NAME

DbGetopt -- the currently preferred method of parsing args in jdb


=head1 SYNOPSIS

    use DbGetopt;
    $opts = new DbGetopts("ab:", \@ARGV);
    while ($opts->getopt()) {
	if ($opts->opt eq 'b') {
	    $b = $opts->optarg;
	}
    };
    @other_args = $opts->rest;

=head1 CREDITS

Taken from:

getoptk.pl -- getopt-like processing for Perl scripts, by
Brian Katzung  12 June 1993
<katzung@katsun.chi.il.us>,
and much hacked.

Perl5-ized by John Heidemann <johnh@isi.edu>.

=cut
#'


require 5.000;
require Exporter;
@EXPORT = qw();
@EXPORT_OK = qw();
($VERSION) = ('$Revision: 1.2 $' =~ / (\d+.d+) /);

use Carp qw(croak);


=head2 new("optionslist", \@ARGV)

Instantiate a new object.

=cut
#' font-lock hack
sub new {
    my($class) = shift @_;
    my($options, $optlistref) = @_;

    croak("DbGetopt::new: no options.\n") if (!defined($options));
    croak("DbGetopt::new: no option list or wrong type.\n") if (!defined($optlistref) || ref($optlistref) ne 'ARRAY');

    my $self = bless {
	opt => undef,
	opterr => 1,
	optarg => undef,
	_nextopt => '',
	_spec => $options,
	_optlistref => $optlistref,
    }, $class;
    return $self;
}

# from LWP::MemberMixin
sub _elem {
    my($self, $elem, $val) = @_;
    my $old = $self->{$elem};
    $self->{$elem} = $val if defined $val;
    return $old;
}
sub _elem_array {
    my($self) = shift @_;
    my($elem) = shift @_;
    return wantarray ? @{$self->{$elem}} : $self->{$elem}
        if ($#_ == -1);
    if (ref($_[0])) {
        $self->{$elem} = $_[0];
    } else {
	$self->{$elem} = ();
	push @{$self->{$elem}}, @_;
    };
    # always return array refrence
    return $self->{$elem};
}

=head2 opt, optarg, opterr, rest

Return the currently parsed option, that options's argument,
the error status, or any remaining options.

=cut
# '
sub opt { return shift->_elem('opt', @_); }
sub opterr { return shift->_elem('opterr', @_); }
sub optarg { return shift->_elem('optarg', @_); }
sub rest { return shift->_elem_array('_optlistref', @_); }

=head2 getopt

Get the next option, returning undef if out.

=cut
sub getopt {
    my($self) = shift;
    my($withArgs) = $self->{_spec};
    my($next);
    my($option, $i);
    my($argvref) = $self->{_optlistref};

    #
    # Fetch the next option string if necessary.
    #
    if (($next = $self->{'_nextopt'}) eq '') {
	#
	# Stop if there are no more arguments, if we see '--',
	# or if the next argument doesn't look like an option
	# string.
	#
	return undef
	    if (($#{$argvref} < 0) || (${$argvref}[0] eq '-') || (${$argvref}[0] !~ /^-/));
	if (${$argvref}[0] eq '--') {
	    shift(@{$argvref});
	    return undef;
	}
	
	#
	# Grab the next argument and remove it from @ARGV.
	#
	$next = shift @{$argvref};
	$next = substr($next, 1);
    };

    #
    # Peel off the next option.
    #
    $option = substr($next, 0, 1);
    $next = substr($next, 1);

    $i = index($withArgs, $option);
    if ($i == -1) {
	#
	# Unknown option.
	#
	croak("unknown option '$option'") if ($self->{'opterr'});
	# # put the argument back on ARGV
	# unshift (@ARGV, "-$option$next");
	$self->{'opt'} = '?';
	return 1;
    };
    if (substr($withArgs, $i+1, 1) eq ':') {
	#
	# The option takes an argument.  Take the argument
	# from the remainder of the option string, or use
	# the next argument if the option string is empty.
	#
	if ($next ne '') {
	    $self->{'optarg'} = $next;
	    $next = '';
	} else {
	    $self->{'optarg'} = shift(@{$argvref});
	};
    };

    #
    # Save the remainder of the option string and return
    # the current option.
    #
    $self->{'_nextopt'} = $next;
    $self->{'opt'} = $option;
    return 1;
}

=head2 ungetopt

Push the current option back on the options stream.
(May not exactly preserve original option parsing.)

=cut
sub ungetopt {
    my($self) = shift;
    my($opt) = $self->{'opt'};
    $opt .= $self->{_nextopt} if ($self->{_nextopt} ne '');
    unshift @{$self->{'_optlistref'}}, "-$opt";
}

# suppress warnings
my($bogus) = $VERSION;

1;