| 1 | package Declare::Args; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = "0.009"; | ||
| 6 | |||
| 7 | use Carp qw/croak/; | ||
| 8 | |||
| 9 | use Exporter::Declare qw{ | ||
| 10 | import | ||
| 11 | gen_default_export | ||
| 12 | default_export | ||
| 13 | }; | ||
| 14 | |||
| 15 | gen_default_export 'ARGS_META' => sub { | ||
| 16 | my ( $class, $caller ) = @_; | ||
| 17 | my $meta = $class->new(); | ||
| 18 | $meta->{class} = $caller; | ||
| 19 | return sub { $meta }; | ||
| 20 | }; | ||
| 21 | |||
| 22 | default_export arg => sub { caller->ARGS_META->arg( @_ ) }; | ||
| 23 | default_export parse_args => sub { caller->ARGS_META->parse( @_ ) }; | ||
| 24 | default_export arg_info => sub { caller->ARGS_META->info }; | ||
| 25 | |||
| 26 | sub class { shift->{class} } | ||
| 27 | sub args { shift->{args} } | ||
| 28 | sub default { shift->{default} } | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $class = shift; | ||
| 32 | my ( %args ) = @_; | ||
| 33 | |||
| 34 | my $self = bless { args => {}, default => {} } => $class; | ||
| 35 | $self->arg( $_, $args{$_} ) for keys %args; | ||
| 36 | |||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub valid_arg_params { | ||
| 41 | return qr/^(alias|list|bool|default|check|transform|description)$/; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub arg { | ||
| 45 | my $self = shift; | ||
| 46 | my ( $name, %config ) = @_; | ||
| 47 | |||
| 48 | croak "arg '$name' already defined" | ||
| 49 | if $self->args->{$name}; | ||
| 50 | |||
| 51 | for my $prop ( keys %config ) { | ||
| 52 | next if $prop =~ $self->valid_arg_params; | ||
| 53 | croak "invalid arg property: '$prop'"; | ||
| 54 | } | ||
| 55 | |||
| 56 | $config{name} = $name; | ||
| 57 | |||
| 58 | croak "'check' cannot be used with 'bool'" | ||
| 59 | if $config{bool} && $config{check}; | ||
| 60 | |||
| 61 | croak "'transform' cannot be used with 'bool'" | ||
| 62 | if $config{bool} && $config{transform}; | ||
| 63 | |||
| 64 | croak "arg properties 'list' and 'bool' are mutually exclusive" | ||
| 65 | if $config{list} && $config{bool}; | ||
| 66 | |||
| 67 | if (exists $config{default}) { | ||
| 68 | croak "References cannot be used in default, wrap them in a sub." | ||
| 69 | if ref $config{default} && ref $config{default} ne 'CODE'; | ||
| 70 | $self->default->{$name} = $config{default}; | ||
| 71 | } | ||
| 72 | |||
| 73 | if ( exists $config{check} ) { | ||
| 74 | my $ref = ref $config{check}; | ||
| 75 | croak "'$config{check}' is not a valid value for 'check'" | ||
| 76 | if ($ref && $ref !~ m/^(CODE|Regexp)$/) | ||
| 77 | || (!$ref && $config{check} !~ m/^(file|dir|number)$/); | ||
| 78 | } | ||
| 79 | |||
| 80 | if ( exists $config{alias} ) { | ||
| 81 | my $aliases = ref $config{alias} ? $config{alias} | ||
| 82 | : [ $config{alias} ]; | ||
| 83 | |||
| 84 | $config{_alias} = { map { $_ => 1 } @$aliases }; | ||
| 85 | |||
| 86 | for my $alias ( @$aliases ) { | ||
| 87 | croak "Cannot use alias '$alias', name is already taken by another arg." | ||
| 88 | if $self->args->{$alias}; | ||
| 89 | |||
| 90 | $self->args->{$alias} = \%config; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | $self->args->{$name} = \%config; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub parse { | ||
| 98 | my $self = shift; | ||
| 99 | my @args = @_; | ||
| 100 | |||
| 101 | my $params = []; | ||
| 102 | my $flags = {}; | ||
| 103 | my $no_flags = 0; | ||
| 104 | |||
| 105 | while ( my $arg = shift @args ) { | ||
| 106 | if ( $arg eq '--' ) { | ||
| 107 | $no_flags++; | ||
| 108 | } | ||
| 109 | elsif ( $arg =~ m/^-+([^-=]+)(?:=(.+))?$/ && !$no_flags ) { | ||
| 110 | my ( $key, $value ) = ( $1, $2 ); | ||
| 111 | |||
| 112 | my $name = $self->_flag_name( $key ); | ||
| 113 | my $values = $self->_flag_value( | ||
| 114 | $name, | ||
| 115 | $value, | ||
| 116 | \@args | ||
| 117 | ); | ||
| 118 | |||
| 119 | if( $self->args->{$name}->{list} ) { | ||
| 120 | push @{$flags->{$name}} => @$values; | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | $flags->{$name} = $values->[0]; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | push @$params => $arg; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | # Add defaults for args not provided | ||
| 132 | for my $arg ( keys %{ $self->default } ) { | ||
| 133 | next if exists $flags->{$arg}; | ||
| 134 | my $val = $self->default->{$arg}; | ||
| 135 | $flags->{$arg} = ref $val ? $val->() : $val; | ||
| 136 | } | ||
| 137 | |||
| 138 | return ( $params, $flags ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub info { | ||
| 142 | my $self = shift; | ||
| 143 | return { | ||
| 144 | map { $self->args->{$_}->{name} => $self->args->{$_}->{description} || "No Description" } | ||
| 145 | keys %{ $self->args } | ||
| 146 | }; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _flag_value { | ||
| 150 | my $self = shift; | ||
| 151 | my ( $flag, $value, $args ) = @_; | ||
| 152 | |||
| 153 | my $spec = $self->args->{$flag}; | ||
| 154 | |||
| 155 | if ( $spec->{bool} ) { | ||
| 156 | return [$value] if defined $value; | ||
| 157 | return [$spec->{default} ? 0 : 1]; | ||
| 158 | } | ||
| 159 | |||
| 160 | my $val = defined $value ? $value : shift @$args; | ||
| 161 | |||
| 162 | my $out = $spec->{list} ? [ split /\s*,\s*/, $val ] | ||
| 163 | : [ $val ]; | ||
| 164 | |||
| 165 | $self->_validate( $flag, $spec, $out ); | ||
| 166 | |||
| 167 | return $out unless $spec->{transform}; | ||
| 168 | return [ map { $spec->{transform}->($_) } @$out ]; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _validate { | ||
| 172 | my $self = shift; | ||
| 173 | my ( $flag, $spec, $value ) = @_; | ||
| 174 | |||
| 175 | my $check = $spec->{check}; | ||
| 176 | return unless $check; | ||
| 177 | my $ref = ref $check || ""; | ||
| 178 | |||
| 179 | my @bad; | ||
| 180 | |||
| 181 | if ( $ref eq 'Regexp' ) { | ||
| 182 | @bad = grep { $_ !~ $check } @$value; | ||
| 183 | } | ||
| 184 | elsif ( $ref eq 'CODE' ) { | ||
| 185 | @bad = grep { !$check->( $_ ) } @$value; | ||
| 186 | } | ||
| 187 | elsif ( $check eq 'file' ) { | ||
| 188 | @bad = grep { ! -f $_ } @$value; | ||
| 189 | } | ||
| 190 | elsif ( $check eq 'dir' ) { | ||
| 191 | @bad = grep { ! -d $_ } @$value; | ||
| 192 | } | ||
| 193 | elsif ( $check eq 'number' ) { | ||
| 194 | @bad = grep { m/\D/ } @$value; | ||
| 195 | } | ||
| 196 | |||
| 197 | return unless @bad; | ||
| 198 | my $type = $ref || $check; | ||
| 199 | die "Validation Failed for '$flag=$type': " . join( ", ", @bad ) . "\n"; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub _flag_name { | ||
| 203 | my $self = shift; | ||
| 204 | my ( $key ) = @_; | ||
| 205 | |||
| 206 | # Exact match | ||
| 207 | return $self->args->{$key}->{name} | ||
| 208 | if $self->args->{$key}; | ||
| 209 | |||
| 210 | my %matches = map { $self->args->{$_}->{name} => 1 } | ||
| 211 | grep { m/^$key/ } | ||
| 212 | keys %{ $self->args }; | ||
| 213 | my @matches = keys %matches; | ||
| 214 | |||
| 215 | die "argument '$key' is ambiguous, could be: " . join( ", " => @matches ) . "\n" | ||
| 216 | if @matches > 1; | ||
| 217 | |||
| 218 | die "unknown argument '$key'\n" | ||
| 219 | unless @matches; | ||
| 220 | |||
| 221 | return $matches[0]; | ||
| 222 | } | ||
| 223 | |||
| 224 | 1; | ||
| 225 | |||
| 226 | __END__ |