| 1 | package App::Tool::Base; | ||
| 2 | { | ||
| 3 | $App::Tool::Base::VERSION = '0.07'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: simple framework for command-line utils | ||
| 7 | |||
| 8 | use 5.010; | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use utf8; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | |||
| 15 | use Attribute::Handlers; | ||
| 16 | use Getopt::Long; | ||
| 17 | |||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | # initialize framework and run tool | ||
| 22 | use App::Tool::Base qw/ run /; | ||
| 23 | run(); | ||
| 24 | exit 0; | ||
| 25 | |||
| 26 | # describe actions | ||
| 27 | sub new | ||
| 28 | :Action | ||
| 29 | :Descriprion("Create new instance") | ||
| 30 | :Argument(name) | ||
| 31 | { | ||
| 32 | # all arguments and options in plain hash | ||
| 33 | my %opt = @_; | ||
| 34 | |||
| 35 | # ... some useful code here | ||
| 36 | } | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | App::Tool::Base is a simple framework for rapid tool creation. | ||
| 41 | |||
| 42 | Here word <tool> means command-line utility that can perform some actions, and has common command-line format: | ||
| 43 | |||
| 44 | <utility-name> <action> <arguments and options> | ||
| 45 | |||
| 46 | $ svn checkout $REPO/trunk . -r 888 | ||
| 47 | $ apt-get install $DEB | ||
| 48 | $ docker images | ||
| 49 | |||
| 50 | App::Tool::Base provides smart command-line options processing with some checks, and help message generation. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | |||
| 55 | # action info storage | ||
| 56 | { | ||
| 57 | my %action; | ||
| 58 | my %action_by_method; | ||
| 59 | my @actions; | ||
| 60 | |||
| 61 | sub _add_action { | ||
| 62 | my ($info, $names) = @_; | ||
| 63 | |||
| 64 | my @names = ref $names ? @$names : ($names); | ||
| 65 | $info->{action} //= $names[0]; | ||
| 66 | |||
| 67 | push @actions, $info; | ||
| 68 | $action{$_} = $info for @names; | ||
| 69 | |||
| 70 | if ( my $method = $info->{method} ) { | ||
| 71 | $action_by_method{$method} = $info; | ||
| 72 | } | ||
| 73 | |||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _get_action_by_method { | ||
| 78 | my ($method) = @_; | ||
| 79 | |||
| 80 | my $info = $action_by_method{$method} | ||
| 81 | or croak "Action for $method is not defined"; | ||
| 82 | |||
| 83 | return $info; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _get_action_by_name { | ||
| 87 | my ($name) = @_; | ||
| 88 | my $info = $action{$name} | ||
| 89 | or croak "Action $name is not defined"; | ||
| 90 | return $info; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _get_actions_list { | ||
| 94 | return [grep {!$_->{hidden}} @actions]; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _get_action_names { | ||
| 98 | my ($full) = @_; | ||
| 99 | my @actions = $full | ||
| 100 | ? (sort keys %action) | ||
| 101 | : (map {$_->{action}} grep {!$_->{hidden}} @actions); | ||
| 102 | return \@actions; | ||
| 103 | } | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | =head1 ATTRIBUTE HANDLERS | ||
| 111 | |||
| 112 | =head2 :Action | ||
| 113 | |||
| 114 | sub init :Action { ... some code ... } | ||
| 115 | |||
| 116 | sub recalculate_everything :Action(refresh) { ... } | ||
| 117 | |||
| 118 | Registers attributed sub as action worker. | ||
| 119 | |||
| 120 | If action name is not defined, sub name will be used | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub Action :ATTR(CODE, BEGIN) { | ||
| 125 | my ($package, $symbol, $referent, $attr, $data, $level, $source, $line) = @_; | ||
| 126 | |||
| 127 | my $method = *{$symbol}{NAME}; | ||
| 128 | my $action_names = $data || $method; | ||
| 129 | |||
| 130 | my $action_info = { | ||
| 131 | method => $method, | ||
| 132 | sub => $referent, | ||
| 133 | source => $source, | ||
| 134 | }; | ||
| 135 | |||
| 136 | _add_action($action_info, $action_names); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | =head2 :Hidden | ||
| 142 | |||
| 143 | sub init :Action | ||
| 144 | :Hidden | ||
| 145 | { ... some code ... } | ||
| 146 | |||
| 147 | Mark action as hidden. | ||
| 148 | |||
| 149 | Hidden actions is not shown in lists, but still can be executed. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub Hidden :ATTR(CODE, BEGIN) { | ||
| 154 | my ($package, $symbol, $referent, $attr, $data) = @_; | ||
| 155 | my $method = *{$symbol}{NAME}; | ||
| 156 | my $info = _get_action_by_method($method); | ||
| 157 | $info->{hidden} = 1; | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | =head2 :Description | ||
| 163 | |||
| 164 | sub init :Action | ||
| 165 | :Description("Initialize workplace") | ||
| 166 | { ... some code ... } | ||
| 167 | |||
| 168 | Provides brief worker description. | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub Description :ATTR(CODE, BEGIN) { | ||
| 173 | my ($package, $symbol, $referent, $attr, $data) = @_; | ||
| 174 | my $method = *{$symbol}{NAME}; | ||
| 175 | my $info = _get_action_by_method($method); | ||
| 176 | $info->{description} = (ref $data ? $data->[0] : $data); | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | =head2 :Argument | ||
| 182 | |||
| 183 | sub init :Action | ||
| 184 | :Argument(name, "workplace name") | ||
| 185 | { ... some code ... } | ||
| 186 | |||
| 187 | Declares that action has required positional argument | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub Argument :ATTR(CODE, BEGIN) { | ||
| 192 | my ($package, $symbol, $referent, $attr, $data) = @_; | ||
| 193 | my $method = *{$symbol}{NAME}; | ||
| 194 | my $info = _get_action_by_method($method); | ||
| 195 | |||
| 196 | my ($arg, $descr) = (ref $data ? @$data : $data); | ||
| 197 | push @{$info->{req_args}}, $arg; | ||
| 198 | $info->{arg_descr}->{$arg} = $descr if $descr; | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | =head2 :OptionalArgument | ||
| 204 | |||
| 205 | sub init :Action | ||
| 206 | :Argument(name, "workplace name") | ||
| 207 | :OptionalArgument(template, "name of template") | ||
| 208 | { ... some code ... } | ||
| 209 | |||
| 210 | Declares that action has optional positional argument | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub OptionalArgument :ATTR(CODE, BEGIN) { | ||
| 215 | my ($package, $symbol, $referent, $attr, $data) = @_; | ||
| 216 | my $method = *{$symbol}{NAME}; | ||
| 217 | my $info = _get_action_by_method($method); | ||
| 218 | |||
| 219 | my ($arg, $descr) = (ref $data ? @$data : $data); | ||
| 220 | push @{$info->{opt_args}}, $arg; | ||
| 221 | $info->{arg_descr}->{$arg} = $descr if $descr; | ||
| 222 | return; | ||
| 223 | } | ||
| 224 | |||
| 225 | =head2 :Option | ||
| 226 | |||
| 227 | sub init :Action | ||
| 228 | :Argument(name, "workplace name") | ||
| 229 | :Option("v|verbose=i", "verbosity level") | ||
| 230 | { ... some code ... } | ||
| 231 | |||
| 232 | Declares that action has getopt-style option | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | sub Option :ATTR(CODE, BEGIN) { | ||
| 237 | my ($package, $symbol, $referent, $attr, $data) = @_; | ||
| 238 | my $method = *{$symbol}{NAME}; | ||
| 239 | my $info = _get_action_by_method($method); | ||
| 240 | |||
| 241 | my ($opt, $descr) = (ref $data ? @$data : $data); | ||
| 242 | push @{$info->{options}}, $opt; | ||
| 243 | $info->{opt_descr}->{$opt} = $descr if $descr; | ||
| 244 | return; | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | =head1 FUNCTIONS | ||
| 249 | |||
| 250 | =head2 run | ||
| 251 | |||
| 252 | Process command-line options and execute action | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | sub run { | ||
| 257 | my ($self, $action_name) = @_; | ||
| 258 | |||
| 259 | my $action = shift @ARGV || 'help'; | ||
| 260 | my $action_info = _get_action_by_name($action); | ||
| 261 | |||
| 262 | my %arg; | ||
| 263 | if ( $action_info->{options} ) { | ||
| 264 | GetOptions( | ||
| 265 | map {( $_ => \$arg{_get_option_key($_)} )} | ||
| 266 | @{$action_info->{options}} | ||
| 267 | ) or croak "Bad options, stop"; | ||
| 268 | } | ||
| 269 | |||
| 270 | for my $arg_name ( @{$action_info->{req_args} || []} ) { | ||
| 271 | my $arg_value = shift @ARGV; | ||
| 272 | croak "Required argument <$arg_name> is not defined" if !defined $arg_value; | ||
| 273 | $arg{$arg_name} = $arg_value; | ||
| 274 | } | ||
| 275 | |||
| 276 | for my $arg_name ( @{$action_info->{opt_args} || []} ) { | ||
| 277 | my $arg_value = shift @ARGV; | ||
| 278 | last if !defined $arg_value; | ||
| 279 | $arg{$arg_name} = $arg_value; | ||
| 280 | } | ||
| 281 | |||
| 282 | return $action_info->{sub}->( map {($_ => $arg{$_})} grep {defined $arg{$_}} keys %arg ); | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | =head1 BUILT-IN ACTIONS | ||
| 287 | |||
| 288 | =head2 help | ||
| 289 | |||
| 290 | Assemble and show usage info. | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub print_usage_info | ||
| 295 | :Action(help, '-h', '--help') | ||
| 296 | :Description("Show basic usage info") | ||
| 297 | :OptionalArgument(action) | ||
| 298 | { | ||
| 299 | my %opt = @_; | ||
| 300 | |||
| 301 | my $action = $opt{action}; | ||
| 302 | if ( $action ) { | ||
| 303 | my $action_info = _get_action_by_name($action) | ||
| 304 | or croak "Unknown action <$action>"; | ||
| 305 | |||
| 306 | say "Usage:"; | ||
| 307 | say " $0 $action " . _join_arg_help_list($action_info->{req_args}, $action_info->{opt_args}); | ||
| 308 | say "\n$action_info->{description}" if $action_info->{description}; | ||
| 309 | |||
| 310 | my @arg_table = | ||
| 311 | grep {$_->[1]} | ||
| 312 | map {[$_ => $action_info->{arg_descr}->{$_}]} | ||
| 313 | map {@{$action_info->{$_} || []}} | ||
| 314 | qw/ req_args opt_args /; | ||
| 315 | |||
| 316 | if ( @arg_table ) { | ||
| 317 | say "\nArguments:"; | ||
| 318 | say _format_table(\@arg_table, " %s - %-s"); | ||
| 319 | } | ||
| 320 | |||
| 321 | my @opt_table = | ||
| 322 | grep {$_->[1]} | ||
| 323 | map {[_get_option_key($_) => $action_info->{opt_descr}->{$_}]} | ||
| 324 | @{$action_info->{options} || []}; | ||
| 325 | |||
| 326 | if ( @opt_table ) { | ||
| 327 | say "\nOptions:"; | ||
| 328 | say _format_table(\@opt_table, " --%-s %-s"); | ||
| 329 | } | ||
| 330 | |||
| 331 | if ( my $pod = `podselect -sections /$action_info->{method} $action_info->{source} | pod2text` ) { | ||
| 332 | # remove first line: we don't need header | ||
| 333 | $pod =~ s/^[^\n]+//xms; | ||
| 334 | say $pod; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | # main usage | ||
| 338 | else { | ||
| 339 | say "Usage:"; | ||
| 340 | say " $0 <action> [<args>] [<options>]"; | ||
| 341 | |||
| 342 | if ( my $pod = `podselect -sections DESCRIPTION/!.+ $0 | pod2text` ) { | ||
| 343 | # remove first line: we don't need header | ||
| 344 | $pod =~ s/^[^\n]+//xms; | ||
| 345 | print $pod; | ||
| 346 | } | ||
| 347 | |||
| 348 | say "\nActions:"; | ||
| 349 | |||
| 350 | my @table; | ||
| 351 | for my $action_info ( @{ _get_actions_list() } ) { | ||
| 352 | # next if !$action_info->{description}; | ||
| 353 | push @table, [ | ||
| 354 | $action_info->{action}, | ||
| 355 | _join_arg_help_list($action_info->{req_args}, $action_info->{opt_args}), | ||
| 356 | $action_info->{description}, | ||
| 357 | ]; | ||
| 358 | } | ||
| 359 | say _format_table(\@table, " %s %-s - %-s"); | ||
| 360 | } | ||
| 361 | |||
| 362 | return; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | sub _join_arg_help_list { | ||
| 367 | my ($req_args, $opt_args) = @_; | ||
| 368 | return join ( q{ }, | ||
| 369 | ( map {"<$_>"} @{ $req_args || [] } ), | ||
| 370 | ( map {"[<$_>]"} @{ $opt_args || [] } ), | ||
| 371 | ); | ||
| 372 | } | ||
| 373 | |||
| 374 | |||
| 375 | sub _get_option_key { | ||
| 376 | my ($getopt_key) = @_; | ||
| 377 | my ($key) = $getopt_key =~ /^([\w\-]+)/xms; | ||
| 378 | return $key; | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | sub _format_table { | ||
| 383 | my ($table, $format) = @_; | ||
| 384 | |||
| 385 | my @maxlens; | ||
| 386 | for my $row (@$table) { | ||
| 387 | for my $i (0 .. $#$row) { | ||
| 388 | next if $maxlens[$i] && length $row->[$i] <= $maxlens[$i]; | ||
| 389 | $maxlens[$i] = length $row->[$i]; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | $format //= join q{ }, ("%-s") x scalar @maxlens; | ||
| 394 | $format =~ s/(%-?)/$1 . shift @maxlens/gexms; | ||
| 395 | |||
| 396 | return join qq{\n}, map {sprintf $format, @$_} @$table; | ||
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | =head2 LIST | ||
| 401 | |||
| 402 | Show list of supported actions. | ||
| 403 | |||
| 404 | =cut | ||
| 405 | |||
| 406 | sub list_actions | ||
| 407 | :Action(LIST) | ||
| 408 | :Hidden | ||
| 409 | :Description('Show list of available actions') | ||
| 410 | :Option(full, 'show hidden actions also') | ||
| 411 | { | ||
| 412 | my %opt = @_; | ||
| 413 | |||
| 414 | my $actions = _get_action_names($opt{full}); | ||
| 415 | say join qq{\n}, @$actions; | ||
| 416 | return; | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | sub import | ||
| 422 | { | ||
| 423 | my $class = shift; | ||
| 424 | my $inheritor = caller(0); | ||
| 425 | my ($run) = @_; | ||
| 426 | |||
| 427 | { | ||
| 428 | no strict 'refs'; | ||
| 429 | push @{"$inheritor\::ISA"}, $class; | ||
| 430 | *{"$inheritor\::$run"} = \&run if $run; | ||
| 431 | }; | ||
| 432 | |||
| 433 | return; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | 1; | ||
| 438 |