Start using config provided in getopts module

This commit is contained in:
Duncan Ferguson 2014-06-20 15:16:41 +01:00
parent f39ffcb9e4
commit 97884dd03e
4 changed files with 52 additions and 46 deletions

View file

@ -56,10 +56,10 @@ sub new {
my $self = $class->SUPER::new(%args);
$self->{config} = App::ClusterSSH::Config->new();
$self->{helper} = App::ClusterSSH::Helper->new();
$self->{cluster} = App::ClusterSSH::Cluster->new();
$self->{options} = App::ClusterSSH::Getopt->new();
$self->{config} = App::ClusterSSH::Config->new(parent => $self, );
$self->{helper} = App::ClusterSSH::Helper->new(parent => $self, );
$self->{cluster} = App::ClusterSSH::Cluster->new(parent => $self, );
$self->{options} = App::ClusterSSH::Getopt->new(parent => $self, );
# catch and reap any zombies
$SIG{CHLD} = \&REAPER;
@ -995,7 +995,7 @@ sub retile_hosts {
);
}
$self->config->dump("noexit") if ( $options{debug} > 1 );
$self->config->dump("noexit") if ( $self->getopts->debug > 1 );
# now we have the info, plot first window position
my @hosts;
@ -1902,20 +1902,6 @@ sub run {
#die;
### main ###
# Note: getopts returns "" if it finds any options it doesn't recognise
# so use this to print out basic help
pod2usage( -verbose => 1 )
if ( !GetOptions( \%options, @options_spec ) );
pod2usage( -verbose => 1 ) if ( $options{'?'} || $options{help} );
pod2usage( -verbose => 2 ) if ( $options{H} || $options{man} );
if ( $options{version} ) {
print "Version: $VERSION\n";
exit 0;
}
$options{debug} ||= 0;
# only get xdisplay if we got past usage and help stuff
$xdisplay = X11::Protocol->new();
@ -1923,33 +1909,8 @@ sub run {
die("Failed to get X connection\n");
}
if ( $options{d} && $options{D} ) {
$options{debug} += 3;
logmsg( 0,
'NOTE: -d and -D are deprecated - use "--debug 3" instead' );
}
elsif ( $options{d} ) {
$options{debug} += 1;
logmsg( 0, 'NOTE: -d is deprecated - use "--debug 1" instead' );
}
elsif ( $options{D} ) {
$options{debug} += 2;
logmsg( 0, 'NOTE: -D is deprecated - use "--debug 2" instead' );
}
# restrict to max level
$options{debug} = 4 if ( $options{debug} && $options{debug} > 4 );
$self->set_debug_level( $options{debug} );
logmsg( 2, "VERSION: $VERSION" );
$self->config->load_configs( $options{'config-file'} );
if ( $options{title} ) {
$self->config->{title} = $options{title};
logmsg( 2, "Title: " . $self->config->{title} );
}
if ( $options{use_all_a_records} ) {
$self->config->{use_all_a_records}
= !$self->config->{use_all_a_records} || 0;

View file

@ -267,6 +267,11 @@ sub load_file {
return %results;
}
sub parent {
my ($self) = @_;
return $self->{parent};
}
1;
=pod

View file

@ -17,7 +17,7 @@ use App::ClusterSSH::Cluster;
my $clusters;
my %old_clusters;
my @app_specific = (qw/ command title comms method /);
my @app_specific = (qw/ command title comms method parent /);
# list of config items to not write out when writing the default config
my @ignore_default_config = (qw/ user /);

View file

@ -49,7 +49,7 @@ sub add_option {
$arg = defined $arg_type ? "'$arg_type'" : q{'<}.$self->loc('STRING').q{>'};
}
}
my ($desc, $long, $short);
my ($desc, $long, $short, $accessor);
foreach my $item ( split /\|/, $option) {
$desc .= ', ' if($desc);
@ -60,6 +60,7 @@ sub add_option {
} else {
$desc .= "--$item";
$long = "--$item";
$accessor=$item if(!$accessor);
}
$desc .= " $arg" if($arg);
$short .= " $arg" if($short && $arg);
@ -68,6 +69,7 @@ sub add_option {
$args{option_desc}=$desc;
$args{option_short}=$short;
$args{option_long}=$long;
$args{accessor}=$accessor if(!defined $args{no_accessor});
$self->{command_options}->{ $spec } = \%args;
return $self;
@ -80,18 +82,22 @@ sub add_common_options {
$self->add_option(
spec => 'help|h' ,
help => $self->loc("Show help text and exit"),
no_accessor => 1,
);
$self->add_option(
spec => 'usage|?' ,
help => $self->loc('Show basic usage and exit'),
no_accessor => 1,
);
$self->add_option(
spec => 'version|v' ,
help => $self->loc("Show version information and exit"),
no_accessor => 1,
);
$self->add_option(
spec => 'man|H' ,
help => $self->loc("Show full help text (the man page) and exit"),
no_accessor => 1,
);
$self->add_option(
spec => 'debug:+',
@ -246,6 +252,40 @@ sub getopts {
$self->exit;
}
$options->{debug} ||= 0;
$options->{debug} = 4 if ( $options->{debug} && $options->{debug} > 4 );
# Now all options are set to the correct values, generate accessor methods
foreach my $option ( sort keys(%{ $self->{command_options}}) ) {
my $accessor=$self->{command_options}->{$option}->{accessor};
if($accessor) {
$accessor =~ s/-/_/g;
no strict 'refs';
*$accessor = sub {
return $options->{$accessor};
};
}
}
$self->set_debug_level( $self->debug );
$self->parent->config->load_configs( $self->config_file );
if($self->{title}) {
$self->parent->config->{title} = $self->title;
$self->debug(2, "Title: " . $self->title );
}
if ( $options->{use_all_a_records} ) {
$self->parent->config->{use_all_a_records}
= !$self->parent->config->{use_all_a_records} || 0;
}
if ( $self->action ) {
$self->parent->config->{command} = $self->action;
}
return $self;
}