| 1 | package MYDan::Util::OptConf; | ||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | MYDan::Util::OptConf - Get command line options. | ||
| 5 | |||
| 6 | =cut | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Cwd; | ||
| 11 | use Carp; | ||
| 12 | use YAML::XS; | ||
| 13 | use File::Spec; | ||
| 14 | use File::Basename; | ||
| 15 | use Getopt::Long; | ||
| 16 | use Pod::Usage; | ||
| 17 | use FindBin qw( $RealScript $RealBin ); | ||
| 18 | use MYDan qw( $PATH ); | ||
| 19 | |||
| 20 | local $| = 1; | ||
| 21 | |||
| 22 | our ( $ARGC, $THIS, $CONF, $PRIVATE, $ROOT, @CONF ) = ( 0, $RealScript, '.config', '.config.private' ); | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use MYDan::Util::OptConf; | ||
| 27 | |||
| 28 | $MYDan::Util::OptConf::ARGC = -1; | ||
| 29 | @MYDan::Util::OptConf::CONF = qw( pass_through ); | ||
| 30 | |||
| 31 | my $option = MYDan::Util::OptConf->load( conf => '/conf/file' ); | ||
| 32 | |||
| 33 | my $option = MYDan::Util::OptConf->load( base => '/conf/path' ); | ||
| 34 | |||
| 35 | my %foo = $option->dump( 'foo' ); | ||
| 36 | |||
| 37 | my %opt = $option->set( bar => 'baz' )->get( 'timeout=i', 'verbose' )->dump; | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =head3 load( $conf ) | ||
| 42 | |||
| 43 | Load options from a YAML file $conf, which if unspecified, defaults to | ||
| 44 | $RealBin/.config, or $RealBin/../.config, if either exists. Returns object. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | sub load | ||
| 48 | { | ||
| 49 | my ( $class, %load ) = @_; | ||
| 50 | my $self = {}; | ||
| 51 | my $base = $load{base} || $RealBin; | ||
| 52 | my @conf = map { | ||
| 53 | my $p = $_; | ||
| 54 | map{ File::Spec->join( $base, $p, $_ ) }( $PRIVATE, $CONF ) | ||
| 55 | } qw( . .. ../.. ../../.. ); | ||
| 56 | my ( $conf ) = $load{conf} ? $load{conf} : grep { -l $_ || -f $_ } @conf; | ||
| 57 | |||
| 58 | if ( $conf ) | ||
| 59 | { | ||
| 60 | my $error = "invalid config $conf"; | ||
| 61 | $conf = readlink $conf if -l $conf; | ||
| 62 | confess "$error: not a regular file" unless -f $conf; | ||
| 63 | |||
| 64 | $self = eval { YAML::XS::LoadFile( $conf ) }; | ||
| 65 | confess "$error: $@" if $@; | ||
| 66 | confess "$error: not HASH" if ref $self ne 'HASH'; | ||
| 67 | |||
| 68 | unless( $load{raw} ) | ||
| 69 | { | ||
| 70 | $ROOT ||= dirname( Cwd::abs_path( $conf ) ); | ||
| 71 | for my $conf ( values %$self ) | ||
| 72 | { | ||
| 73 | while ( my ( $opt, $value ) = each %$conf ) | ||
| 74 | { | ||
| 75 | unless ( my $ref = ref $value ) | ||
| 76 | { | ||
| 77 | $conf->{$opt} = $class->macro( $conf->{$opt} ); | ||
| 78 | } | ||
| 79 | elsif ( $ref eq 'ARRAY' ) | ||
| 80 | { | ||
| 81 | $value = [ map { $class->macro( $_ ) } @$value ]; | ||
| 82 | } | ||
| 83 | elsif ( $ref eq 'HASH' ) | ||
| 84 | { | ||
| 85 | map { $value->{$_} = $class->macro( $value->{$_} ) } | ||
| 86 | keys %$value; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | $self->{$THIS} ||= {}; | ||
| 94 | bless $self, ref $class || $class; | ||
| 95 | } | ||
| 96 | |||
| 97 | =head3 dump( $name ) | ||
| 98 | |||
| 99 | Dump options by $name, or that of $0 if $name is unspecified. | ||
| 100 | Returns HASH in scalar context or flattened HASH in list context. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | sub dump | ||
| 104 | { | ||
| 105 | my $self = shift; | ||
| 106 | my %opt = %{ $self->{ @_ ? shift : $THIS } || {} }; | ||
| 107 | return wantarray ? %opt : \%opt; | ||
| 108 | } | ||
| 109 | |||
| 110 | =head3 set( %opt ) | ||
| 111 | |||
| 112 | Set options specified by %opt for $0. Returns object. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | sub set | ||
| 116 | { | ||
| 117 | my ( $self, %opt ) = splice @_; | ||
| 118 | map { $self->{$THIS}{$_} = $opt{$_} } keys %opt; | ||
| 119 | return $self; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head3 get( @option ) | ||
| 123 | |||
| 124 | Invoke Getopt::Long to get @option, if any specified. Returns object. | ||
| 125 | |||
| 126 | Getopt::Long is configured through @CONF. | ||
| 127 | |||
| 128 | The leftover @ARGV size is asserted through $ARGC. @ARGV cannot be empty | ||
| 129 | if $ARGC is negative, otherwise size of @ARGV needs to equal $ARGC. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | sub get | ||
| 133 | { | ||
| 134 | my $self = shift; | ||
| 135 | push @CONF, 'auto_help' unless grep /auto_help/, @CONF; | ||
| 136 | Getopt::Long::Configure( @CONF ); | ||
| 137 | $self->assert() if ! Getopt::Long::GetOptions( $self->{$THIS}, @_ ) | ||
| 138 | || $ARGC < 0 && ! @ARGV || $ARGC > 0 && @ARGV != $ARGC; | ||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head3 assert( @option ) | ||
| 143 | |||
| 144 | print help and exit, if any of @option is not defined. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | sub assert | ||
| 148 | { | ||
| 149 | my $self = shift; | ||
| 150 | Pod::Usage::pod2usage( -input => $0, -output => \*STDERR, -verbose => 2 ) | ||
| 151 | if ! @_ || grep { ! defined $self->{$THIS}{$_} } @_; | ||
| 152 | return $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | =head3 macro( $path ) | ||
| 156 | |||
| 157 | Replace $ROOT in $path if defined. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | sub macro | ||
| 161 | { | ||
| 162 | my ( $self, $path ) = splice @_; | ||
| 163 | |||
| 164 | if ( $path && defined $ROOT ) | ||
| 165 | { | ||
| 166 | $path =~ s/\$ROOT\b/$ROOT/g; | ||
| 167 | $path =~ s/\$\{ROOT\}/$ROOT/g; | ||
| 168 | |||
| 169 | $path =~ s/\$MYDanPATH\b/$PATH/g; | ||
| 170 | $path =~ s/\$\{MYDanPATH\}/$PATH/g; | ||
| 171 | } | ||
| 172 | |||
| 173 | return $path; | ||
| 174 | }; | ||
| 175 | |||
| 176 | sub save | ||
| 177 | { | ||
| 178 | my ( $class, %save ) = @_; | ||
| 179 | |||
| 180 | return unless defined $save{name} && $save{key}; | ||
| 181 | |||
| 182 | my $self = {}; | ||
| 183 | my $base = $save{base} || $RealBin; | ||
| 184 | my @conf = map { | ||
| 185 | my $p = $_; | ||
| 186 | map{ File::Spec->join( $base, $p, $_ ) }( $PRIVATE, $CONF ) | ||
| 187 | } qw( . .. ../.. ../../.. ); | ||
| 188 | return unless my ( $conf ) = $save{conf} ? $save{conf} : grep { -l $_ || -f $_ } @conf; | ||
| 189 | |||
| 190 | my $error = "invalid config $conf"; | ||
| 191 | $conf = readlink $conf if -l $conf; | ||
| 192 | confess "$error: not a regular file" unless -f $conf; | ||
| 193 | |||
| 194 | my $data = eval { YAML::XS::LoadFile( $conf ) }; | ||
| 195 | confess "$error: $@" if $@; | ||
| 196 | confess "$error: not HASH" if ref $self ne 'HASH'; | ||
| 197 | |||
| 198 | if( defined $save{value} ) | ||
| 199 | { | ||
| 200 | $data->{$save{name}}{$save{key}} = $save{value}; | ||
| 201 | } | ||
| 202 | else | ||
| 203 | { | ||
| 204 | delete $data->{$save{name}}{$save{key}}; | ||
| 205 | } | ||
| 206 | eval { YAML::XS::DumpFile( $conf, $data ) }; | ||
| 207 | |||
| 208 | confess "save error: $@" if $@; | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; |