diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2004-09-15 18:51:45 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2004-09-15 18:51:45 +0000 |
commit | 05cf49729c11ef8539b8da0025366f485f0dc743 (patch) | |
tree | cdfb4aa1a95577d2eef043e9d75ec7b88c8c61cc | |
parent | 05f4c957e914e44927c477abcd42150e5fade81f (diff) |
new getopt module, that allows for option-specific processing, so that
for instance, pkg_create -Dvar=value -Dvar2=value2 will work.
-rw-r--r-- | usr.sbin/pkg_add/Makefile | 3 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Getopt.pm | 58 |
2 files changed, 60 insertions, 1 deletions
diff --git a/usr.sbin/pkg_add/Makefile b/usr.sbin/pkg_add/Makefile index c676260fc74..636b9898aa9 100644 --- a/usr.sbin/pkg_add/Makefile +++ b/usr.sbin/pkg_add/Makefile @@ -1,9 +1,10 @@ -# $OpenBSD: Makefile,v 1.9 2004/09/14 22:43:09 espie Exp $ +# $OpenBSD: Makefile,v 1.10 2004/09/15 18:51:43 espie Exp $ MAN=pkg_add.1 pkg_info.1 pkg_create.1 pkg_delete.1 pkg.1 PACKAGES= \ OpenBSD/Error.pm \ + OpenBSD/Getopt.pm \ OpenBSD/IdCache.pm \ OpenBSD/Logger.pm \ OpenBSD/Mtree.pm \ diff --git a/usr.sbin/pkg_add/OpenBSD/Getopt.pm b/usr.sbin/pkg_add/OpenBSD/Getopt.pm new file mode 100644 index 00000000000..143c9aaf8e0 --- /dev/null +++ b/usr.sbin/pkg_add/OpenBSD/Getopt.pm @@ -0,0 +1,58 @@ +package OpenBSD::Getopt; +require Exporter; + +@ISA = qw(Exporter); +@EXPORT = qw(getopts); + +sub handle_option +{ + my ($opt, $hash, $params) = @_; + + $params = 1 unless defined $params; + if (defined $hash->{$opt} and ref($hash->{$opt}) eq 'CODE') { + &{$hash->{$opt}}($params); + } else { + ${"opt_$opt"} = $params; + push(@EXPORT, "\$opt_$opt"); + $hash->{$opt} = $params; + } +} + +sub getopts($;$) +{ + my ($args, $hash) = @_; + + $hash = {} unless defined $hash; + local @EXPORT; + + while ($_ = shift @ARGV) { + last if /^--$/; + unless (m/^-(.)(.*)/s) { + unshift @ARGV, $_; + last; + } + my ($opt, $other) = ($1, $2); + if ($args =~ m/\Q$opt\E(\:)?/) { + if ($1 eq ':') { + if ($other eq '') { + die "no argument for option $opt" unless @ARGV; + $other = shift @ARGV; + } + handle_option($opt, $hash, $other); + } else { + handle_option($opt, $hash); + if ($other ne '') { + $_ = "-$other"; + redo; + } + } + } else { + die "Unknown option $opt"; + } + } + local $Exporter::ExportLevel = 1; + import OpenBSD::Getopt; + return $hash; +} + +1; |