diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2010-06-25 10:19:01 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2010-06-25 10:19:01 +0000 |
commit | 1bb75ca7999afca095e77980e67e57a069475a04 (patch) | |
tree | 8998ba5bfc8658b2a21ea25eb5375746f1eab4fd /usr.sbin/pkg_add | |
parent | a90e1f4b139ef90802ffec2d6956e7aa002b8a3d (diff) |
move common options (-D/-v) handling code upto State.
rework handle_options to be code in state proper, so that I can remove
a class that's actually useless.
reuse the export code from getopt so that legacy code still get opt_x
working.
Diffstat (limited to 'usr.sbin/pkg_add')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm | 42 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/State.pm | 47 |
2 files changed, 58 insertions, 31 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm b/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm index a83ca85cc0a..3f09866af3f 100644 --- a/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: AddCreateDelete.pm,v 1.6 2010/06/15 08:35:11 espie Exp $ +# $OpenBSD: AddCreateDelete.pm,v 1.7 2010/06/25 10:19:00 espie Exp $ # # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org> # @@ -24,7 +24,6 @@ use warnings; package OpenBSD::AddCreateDelete::State; our @ISA = qw(OpenBSD::State); -use OpenBSD::Subst; use OpenBSD::State; use OpenBSD::ProgressMeter; @@ -32,7 +31,6 @@ sub init { my $self = shift; - $self->{subst} = OpenBSD::Subst->new; $self->{progressmeter} = OpenBSD::ProgressMeter->new; $self->{bad} = 0; $self->SUPER::init; @@ -44,24 +42,12 @@ sub progress return $self->{progressmeter}; } -sub verbose -{ - my $self = shift; - return $self->{v}; -} - sub not { my $self = shift; return $self->{not}; } -sub opt -{ - my ($self, $k) = @_; - return $self->{opt}{$k}; -} - sub _print { my $self = shift; @@ -74,27 +60,23 @@ sub _errprint $self->progress->errprint(@_); } -package OpenBSD::AddCreateDelete; -use OpenBSD::Getopt; -use OpenBSD::Error; - sub handle_options { - my ($self, $opt_string, $state, @usage) = @_; + my ($state, $opt_string, @usage) = @_; + $state->SUPER::handle_options('mnx'.$opt_string, @usage); - $state->{opt}{v} = 0; - $state->{opt}{h} = sub { $state->usage; }; - $state->{opt}{D} = sub { - $state->{subst}->parse_option(shift); - }; - $state->usage_is(@usage); - $state->do_options(sub { - getopts('hmnvxD:'.$opt_string, $state->{opt}); - }); $state->progress->setup($state->opt('x'), $state->opt('m')); - $state->{v} = $state->opt('v'); $state->{not} = $state->opt('n'); } +package OpenBSD::AddCreateDelete; +use OpenBSD::Error; + +sub handle_options +{ + my ($self, $opt_string, $state, @usage) = @_; + $state->handle_options($opt_string, $self, @usage); +} + 1; diff --git a/usr.sbin/pkg_add/OpenBSD/State.pm b/usr.sbin/pkg_add/OpenBSD/State.pm index 6b969dab0a0..0e8c97f9000 100644 --- a/usr.sbin/pkg_add/OpenBSD/State.pm +++ b/usr.sbin/pkg_add/OpenBSD/State.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: State.pm,v 1.4 2010/06/15 08:29:22 espie Exp $ +# $OpenBSD: State.pm,v 1.5 2010/06/25 10:19:00 espie Exp $ # # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org> # @@ -23,6 +23,10 @@ use warnings; # in particular, provides "singleton-like" access to UI. package OpenBSD::State; use Carp; +use OpenBSD::Subst; +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = (); sub new { @@ -34,6 +38,8 @@ sub new sub init { + my $self = shift; + $self->{subst} = OpenBSD::Subst->new; } sub usage_is @@ -42,6 +48,18 @@ sub usage_is $self->{usage} = \@usage; } +sub verbose +{ + my $self = shift; + return $self->{v}; +} + +sub opt +{ + my ($self, $k) = @_; + return $self->{opt}{$k}; +} + sub usage { my $self = shift; @@ -130,6 +148,33 @@ sub do_options bless sub { $state->usage("#1", $_)}, "OpenBSD::Error::catchall"); } +sub handle_options +{ + my ($state, $opt_string, @usage) = @_; + require OpenBSD::Getopt; + + $state->{opt}{v} = 0; + $state->{opt}{h} = sub { $state->usage; }; + $state->{opt}{D} = sub { + $state->{subst}->parse_option(shift); + }; + $state->usage_is(@usage); + $state->do_options(sub { + OpenBSD::Getopt::getopts('hvD:'.$opt_string, $state->{opt}); + }); + $state->{v} = $state->opt('v'); + return if $state->{no_exports}; + # XXX + no strict "refs"; + no strict "vars"; + for my $k (keys %{$state->{opt}}) { + ${"opt_$k"} = $state->opt($k); + push(@EXPORT, "\$opt_$k"); + } + local $Exporter::ExportLevel = 1; + import OpenBSD::State; +} + my @signal_name = (); sub fillup_names { |