diff options
author | 2004-09-15 18:51:43 +0000 | |
---|---|---|
committer | 2004-09-15 18:51:43 +0000 | |
commit | e6f69fe29d4262dcaa342736787efea723aee245 (patch) | |
tree | c996c0718f85260786298cb8be5d0564b6e6957d | |
parent | fix proto for sysarch() (diff) | |
download | wireguard-openbsd-e6f69fe29d4262dcaa342736787efea723aee245.tar.xz wireguard-openbsd-e6f69fe29d4262dcaa342736787efea723aee245.zip |
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; |