| 1 | package CLI::Framework::Application; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.04'; | ||
| 7 | |||
| 8 | use Getopt::Long::Descriptive; | ||
| 9 | use Exception::Class::TryCatch; | ||
| 10 | |||
| 11 | use CLI::Framework::Exceptions qw( :all ); | ||
| 12 | use CLI::Framework::Command; | ||
| 13 | |||
| 14 | # Certain built-in commands are required: | ||
| 15 | use constant REQUIRED_BUILTINS_PKGS => qw( | ||
| 16 | CLI::Framework::Command::Help | ||
| 17 | ); | ||
| 18 | use constant REQUIRED_BUILTINS_NAMES => qw( | ||
| 19 | help | ||
| 20 | ); | ||
| 21 | # Certain built-in commands are required only in interactive mode: | ||
| 22 | use constant REQUIRED_BUILTINS_PKGS_INTERACTIVE => qw( | ||
| 23 | CLI::Framework::Command::Menu | ||
| 24 | ); | ||
| 25 | use constant REQUIRED_BUILTINS_NAMES_INTERACTIVE => qw( | ||
| 26 | menu | ||
| 27 | ); | ||
| 28 | |||
| 29 | #FIXME-TODO-CLASS_GENERATION: | ||
| 30 | #sub import { | ||
| 31 | # my ($class, $app_pkg, $app_def) = @_; | ||
| 32 | # | ||
| 33 | # # If caller has supplied import args, CLIF's "inline form" is being used. | ||
| 34 | # # The application class must be generated dynamically... | ||
| 35 | # | ||
| 36 | #} | ||
| 37 | |||
| 38 | #------- | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my ($class, %args) = @_; | ||
| 42 | |||
| 43 | my $interactive = $args{ interactive }; # boolean: interactive mode? | ||
| 44 | |||
| 45 | my $cache = CLI::Framework::Cache->new(); | ||
| 46 | |||
| 47 | my $app = { | ||
| 48 | _registered_command_objects => undef, # (k,v)=(cmd pkg name,cmd obj) for all registered commands | ||
| 49 | _default_command => 'help', # name of default command | ||
| 50 | _current_command => undef, # name of current (or last) command to run | ||
| 51 | _interactive => $interactive, # boolean: interactive state | ||
| 52 | _cache => $cache, # storage for data shared between app and cmd | ||
| 53 | _initialized => 0, # initialization status | ||
| 54 | }; | ||
| 55 | bless $app, $class; | ||
| 56 | |||
| 57 | # Validate some hook methods so we can assume that they behave properly... | ||
| 58 | $app->_validate_hooks(); | ||
| 59 | |||
| 60 | return $app; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _validate_hooks { | ||
| 64 | my ($app) = @_; | ||
| 65 | |||
| 66 | # Ensure that hook methods return expected data structure types according | ||
| 67 | # to their preconditions... | ||
| 68 | |||
| 69 | my $class = ref $app; | ||
| 70 | |||
| 71 | # Ensure that command_map() succeeds... | ||
| 72 | eval { $app->command_map() }; | ||
| 73 | if( catch my $e ) { | ||
| 74 | throw_app_hook_exception( error => | ||
| 75 | "method 'command_map' in class '$class' fails" ); | ||
| 76 | } | ||
| 77 | # Ensure that command_map() returns a "hash-worthy" list... | ||
| 78 | else { | ||
| 79 | eval { $app->_list_to_hashref( 'command_map' ) }; | ||
| 80 | if( catch my $e ) { | ||
| 81 | $e->isa( 'CLI::Framework::Exception' ) && do{ $e->rethrow() }; | ||
| 82 | throw_app_hook_exception( error => $e ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | # Ensure that command_alias() succeeds... | ||
| 86 | eval { $app->command_alias() }; | ||
| 87 | if( catch my $e ) { | ||
| 88 | throw_app_hook_exception( | ||
| 89 | error => "method 'command_alias' in class '$class' fails" ); | ||
| 90 | } | ||
| 91 | # Ensure that commandf_alias() returns a "hash-worthy" list... | ||
| 92 | else { | ||
| 93 | eval { $app->_list_to_hashref( 'command_alias' ) }; | ||
| 94 | if( catch my $e ) { | ||
| 95 | $e->isa( 'CLI::Framework::Exception' ) && do{ $e->rethrow() }; | ||
| 96 | throw_app_hook_exception( error => $e ); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | sub cache { $_[0]->{_cache} } | ||
| 102 | |||
| 103 | ############################### | ||
| 104 | # | ||
| 105 | # COMMAND INTROSPECTION & REGISTRATION | ||
| 106 | # | ||
| 107 | ############################### | ||
| 108 | |||
| 109 | # convert a list to a HASH ref if list is hash-worthy | ||
| 110 | sub _list_to_hashref { | ||
| 111 | my ($app, $method) = @_; | ||
| 112 | |||
| 113 | my $class = ref $app; | ||
| 114 | my @map = $app->$method; | ||
| 115 | |||
| 116 | # throw exception if command_map list is of odd length | ||
| 117 | if( scalar @map % 2 ) { | ||
| 118 | throw_app_hook_exception( error => | ||
| 119 | "odd-length list returned by application hook '$method' in class '$class' is not hash-worthy\n" ); | ||
| 120 | } | ||
| 121 | my %h; | ||
| 122 | for my $i (0..$#map-1) { | ||
| 123 | if($i % 2 == 0) { | ||
| 124 | my ($k,$v) = ( $map[$i], $map[$i+1] ); | ||
| 125 | # throw exception if command_map list-to-hash conversion would | ||
| 126 | # lose data due to duplicate keys | ||
| 127 | if( exists $h{$k} ) { | ||
| 128 | throw_app_hook_exception( error => | ||
| 129 | "list returned by application hook '$method' in class '$class' is not hash-worthy (duplicate keys for $i)\n" ); | ||
| 130 | } | ||
| 131 | $h{ $map[$i] } = $map[$i+1]; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | return \%h; | ||
| 135 | } | ||
| 136 | |||
| 137 | # Transform command map to hashref | ||
| 138 | sub command_map_hashref { | ||
| 139 | my ($app) = @_; | ||
| 140 | return $app->_list_to_hashref('command_map'); | ||
| 141 | } | ||
| 142 | |||
| 143 | # Return names of all valid commands in same order as specified by | ||
| 144 | # command_map() | ||
| 145 | sub _valid_command_names { | ||
| 146 | my ($app) = @_; | ||
| 147 | |||
| 148 | # ordered pairs of (command name, command class) | ||
| 149 | my @valid_command_name_class_pairs = $app->command_map(); | ||
| 150 | |||
| 151 | # unordered command names | ||
| 152 | my @command_names = keys %{ { @valid_command_name_class_pairs } }; | ||
| 153 | |||
| 154 | my @ordered_command_names; | ||
| 155 | for my $c (@valid_command_name_class_pairs) { | ||
| 156 | push @ordered_command_names, $c | ||
| 157 | if grep {$_ eq $c} @command_names; | ||
| 158 | } | ||
| 159 | return @ordered_command_names; | ||
| 160 | } | ||
| 161 | |||
| 162 | # Return package names for all valid commands | ||
| 163 | sub _valid_command_pkgs { | ||
| 164 | my ($app) = @_; | ||
| 165 | my $valid_commands_hashref = $app->command_map_hashref; | ||
| 166 | return values %$valid_commands_hashref; | ||
| 167 | } | ||
| 168 | |||
| 169 | ## Given a command name, return its package name | ||
| 170 | #sub _find_command_pkg_named { | ||
| 171 | # my ($app, $cmd_name) = @_; | ||
| 172 | # | ||
| 173 | # my $valid_commands_hashref = $app->command_map_hashref; | ||
| 174 | # return $valid_commands_hashref->{$cmd_name}; | ||
| 175 | #} | ||
| 176 | |||
| 177 | sub is_valid_command_pkg { | ||
| 178 | my ($app, $cmd_pkg) = @_; | ||
| 179 | return unless $cmd_pkg; | ||
| 180 | |||
| 181 | my @valid_pkgs = ( $app->_valid_command_pkgs(), REQUIRED_BUILTINS_PKGS ); | ||
| 182 | push @valid_pkgs, REQUIRED_BUILTINS_PKGS_INTERACTIVE | ||
| 183 | if $app->get_interactivity_mode(); | ||
| 184 | |||
| 185 | return grep { $cmd_pkg eq $_ } @valid_pkgs; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub is_valid_command_name { | ||
| 189 | my ($app, $cmd_name) = @_; | ||
| 190 | return unless $cmd_name; | ||
| 191 | |||
| 192 | my @valid_aliases = ( $app->_valid_command_names() ); | ||
| 193 | push @valid_aliases, REQUIRED_BUILTINS_NAMES; | ||
| 194 | push @valid_aliases, REQUIRED_BUILTINS_NAMES_INTERACTIVE | ||
| 195 | if $app->get_interactivity_mode(); | ||
| 196 | |||
| 197 | return grep { $cmd_name eq $_ } @valid_aliases; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub registered_command_names { | ||
| 201 | my ($app) = @_; | ||
| 202 | |||
| 203 | my @names; | ||
| 204 | |||
| 205 | # For each registered command package (name)... | ||
| 206 | for my $cmd_pkg_name (keys %{ $app->{_registered_command_objects} }) { | ||
| 207 | # Find command names that this command package was registered under... | ||
| 208 | push @names, grep { $_ } map { | ||
| 209 | $_ if $app->command_map_hashref->{$_} eq $cmd_pkg_name | ||
| 210 | } $app->_valid_command_names | ||
| 211 | } | ||
| 212 | return @names; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub registered_command_object { | ||
| 216 | my ($app, $cmd_name) = @_; | ||
| 217 | return unless $cmd_name; | ||
| 218 | |||
| 219 | my $cmd_pkg = $app->command_map_hashref->{$cmd_name}; | ||
| 220 | |||
| 221 | return unless $cmd_pkg | ||
| 222 | && exists $app->{_registered_command_objects} | ||
| 223 | && exists $app->{_registered_command_objects}->{$cmd_pkg}; | ||
| 224 | |||
| 225 | return $app->{_registered_command_objects}->{$cmd_pkg}; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub register_command { | ||
| 229 | my ($app, $cmd) = @_; | ||
| 230 | return unless $cmd; | ||
| 231 | |||
| 232 | if( ref $cmd && $app->is_valid_command_pkg(ref $cmd) ) { | ||
| 233 | # Register by reference... | ||
| 234 | return unless $cmd->isa( 'CLI::Framework::Command' ); | ||
| 235 | $app->{_registered_command_objects}->{ref $cmd} = $cmd; | ||
| 236 | } | ||
| 237 | elsif( $app->is_valid_command_pkg($app->command_map_hashref->{$cmd}) ) { | ||
| 238 | # Register by command name... | ||
| 239 | my $pkg = $app->command_map_hashref->{$cmd}; | ||
| 240 | $cmd = CLI::Framework::Command->manufacture( $pkg ); | ||
| 241 | $app->{_registered_command_objects}->{ref $cmd} = $cmd; | ||
| 242 | } | ||
| 243 | #FIXME:use REQUIRED_BUILTINS_PKGS_INTERACTIVE & REQUIRED_BUILTINS_NAMES_INTERACTIVE | ||
| 244 | elsif( $cmd eq 'help' ) { | ||
| 245 | # Required built-in is always valid... | ||
| 246 | $cmd = CLI::Framework::Command->manufacture( 'CLI::Framework::Command::Help' ); | ||
| 247 | $app->{_registered_command_objects}->{'CLI::Framework::Command::Help'} = $cmd; | ||
| 248 | } | ||
| 249 | elsif( $app->get_interactivity_mode() && $cmd eq 'menu' ) { | ||
| 250 | # Required built-in for interactive usage is always valid... | ||
| 251 | $cmd = CLI::Framework::Command->manufacture( 'CLI::Framework::Command::Menu' ); | ||
| 252 | $app->{_registered_command_objects}->{'CLI::Framework::Command::Menu'} = $cmd; | ||
| 253 | } | ||
| 254 | else { | ||
| 255 | throw_cmd_registration_exception( | ||
| 256 | error => "Error: failed attempt to register invalid command '$cmd'" ); | ||
| 257 | } | ||
| 258 | # Metacommands should be app-aware... | ||
| 259 | $cmd->set_app( $app ) if $cmd->isa( 'CLI::Framework::Command::Meta' ); | ||
| 260 | |||
| 261 | return $cmd; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub get_default_command { $_[0]->{_default_command} } | ||
| 265 | sub set_default_command { $_[0]->{_default_command} = $_[1] } | ||
| 266 | |||
| 267 | sub get_current_command { $_[0]->{_current_command} } | ||
| 268 | sub set_current_command { $_[0]->{_current_command} = $_[1] } | ||
| 269 | |||
| 270 | sub get_default_usage { $_[0]->{_default_usage} } | ||
| 271 | sub set_default_usage { $_[0]->{_default_usage} = $_[1] } | ||
| 272 | |||
| 273 | ############################### | ||
| 274 | # | ||
| 275 | # PARSING & RUNNING COMMANDS | ||
| 276 | # | ||
| 277 | ############################### | ||
| 278 | |||
| 279 | sub usage { | ||
| 280 | my ($app, $command_name, @args) = @_; | ||
| 281 | |||
| 282 | # Allow aliases in place of command name... | ||
| 283 | $app->_canonicalize_cmd( $command_name ); | ||
| 284 | |||
| 285 | my $usage_text; | ||
| 286 | if( $command_name && $app->is_valid_command_name($command_name) ) { | ||
| 287 | # Get usage from Command object... | ||
| 288 | my $cmd = $app->registered_command_object( $command_name ) | ||
| 289 | || $app->register_command( $command_name ); | ||
| 290 | $usage_text = $cmd->usage(@args); | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | # Get usage from Application object... | ||
| 294 | $usage_text = $app->usage_text(); | ||
| 295 | } | ||
| 296 | # Finally, fall back to default application usage message... | ||
| 297 | $usage_text ||= $app->get_default_usage(); | ||
| 298 | |||
| 299 | return $usage_text; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub _canonicalize_cmd { | ||
| 303 | my ($self, $input) = @_; | ||
| 304 | |||
| 305 | # Translate shorthand aliases for commands to full names... | ||
| 306 | |||
| 307 | return unless $input; | ||
| 308 | |||
| 309 | my $command_name; | ||
| 310 | my %aliases = $self->command_alias(); | ||
| 311 | return unless %aliases; | ||
| 312 | $command_name = $aliases{$input} || $input; | ||
| 313 | $_[1] = $command_name; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub _handle_global_app_options { | ||
| 317 | my ($app) = @_; | ||
| 318 | |||
| 319 | # Process the [app-opts] prefix of the command request... | ||
| 320 | |||
| 321 | # preconditions: | ||
| 322 | # - tail of @ARGV has been parsed and removed, leaving only the | ||
| 323 | # [app-opts] portion of the request | ||
| 324 | # postconditions: | ||
| 325 | # - application options have been parsed and any application-specific | ||
| 326 | # validation and initialization that is defined has been performed | ||
| 327 | # - invalid tokens after [app-opts] and before <cmd> are detected and | ||
| 328 | # handled | ||
| 329 | |||
| 330 | # Parse [app-opts], consuming them from @ARGV... | ||
| 331 | my ($app_options, $app_usage); | ||
| 332 | eval { ($app_options, $app_usage) = describe_options( '%c %o ...', $app->option_spec ) }; | ||
| 333 | if( catch my $e ) { # (failed application options parsing) | ||
| 334 | throw_app_opts_parse_exception( error => $e ); | ||
| 335 | } | ||
| 336 | $app->set_default_usage( $app_usage->text ); | ||
| 337 | |||
| 338 | # Detect invalid tokens in the [app-opts] part of the request | ||
| 339 | # (@ARGV should be empty unless such invalid tokens exist because <cmd> has | ||
| 340 | # been removed and any valid options have been processed)... | ||
| 341 | if( @ARGV ) { | ||
| 342 | my $err = @ARGV > 1 ? 'Unrecognized options: ' : 'Unrecognized option: '; | ||
| 343 | $err .= join(' ', @ARGV ) . "\n"; | ||
| 344 | throw_app_opts_parse_exception( error => $err ); | ||
| 345 | } | ||
| 346 | # --- VALIDATE APP OPTIONS --- | ||
| 347 | eval { $app->validate_options($app_options) }; | ||
| 348 | if( catch my $e ) { # (failed application options validation) | ||
| 349 | $e->isa( 'CLI::Framework::Exception' ) && do{ $e->rethrow() }; | ||
| 350 | throw_app_opts_validation_exception( error => $e . "\n" . $app->usage ); | ||
| 351 | } | ||
| 352 | # --- INITIALIZE APP --- | ||
| 353 | eval{ $app->init($app_options) }; | ||
| 354 | if( catch my $e ) { # (application failed initialization) | ||
| 355 | $e->isa( 'CLI::Framework::Exception' ) && do{ $e->rethrow() }; | ||
| 356 | throw_app_init_exception( error => $e ); | ||
| 357 | } | ||
| 358 | $app->{_initialized} = 1; | ||
| 359 | |||
| 360 | return 1; | ||
| 361 | } | ||
| 362 | |||
| 363 | sub _parse_request { | ||
| 364 | my ($app, %param) = @_; | ||
| 365 | |||
| 366 | # Parse options/arguments from a command request and set the name of the | ||
| 367 | # current command... | ||
| 368 | |||
| 369 | # If requested, perform validation and initialization of the application. | ||
| 370 | # NOTE: Application validation/initialization should NOT be performed here | ||
| 371 | # in interactive mode for each command request because it should only be | ||
| 372 | # done once for the application, not every time a command is run. | ||
| 373 | |||
| 374 | #~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 375 | # ARGV_Format | ||
| 376 | # | ||
| 377 | # non-interactive case: @ARGV: [app-opts] <cmd> [cmd-opts] [cmd-args] | ||
| 378 | # interactive case: @ARGV: <cmd> [cmd-opts] [cmd-args] | ||
| 379 | #~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 380 | |||
| 381 | my $initialize_app = $param{initialize}; | ||
| 382 | |||
| 383 | # Parse options/arguments for the application and the command from @ARGV... | ||
| 384 | my ($command_name, @command_opts_and_args); | ||
| 385 | for my $i ( 0..$#ARGV ) { | ||
| 386 | # Find first valid command name in @ARGV... | ||
| 387 | $app->_canonicalize_cmd( $ARGV[$i] ); | ||
| 388 | if( $app->is_valid_command_name($ARGV[$i]) ) { | ||
| 389 | # Extract and store '<cmd> [cmd-opts] [cmd-args]', leaving | ||
| 390 | # preceding contents (potentially '[app-opts]') in @ARGV... | ||
| 391 | ($command_name, @command_opts_and_args) = @ARGV[$i..@ARGV-1]; | ||
| 392 | splice @ARGV, $i; | ||
| 393 | last; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | unless( defined $command_name ) { | ||
| 397 | # If no valid command, fall back to default, ignoring any args... | ||
| 398 | $command_name = $app->get_default_command(); | ||
| 399 | @command_opts_and_args = (); | ||
| 400 | |||
| 401 | # If no valid command then any non-option tokens are invalid args... | ||
| 402 | my @invalid_args = grep { substr($_, 0, 1) ne '-' } @ARGV; | ||
| 403 | if( @invalid_args ) { | ||
| 404 | my $err = @invalid_args > 1 ? 'Invalid arguments: ' : 'Invalid argument: '; | ||
| 405 | $err .= join(' ', @invalid_args ); | ||
| 406 | throw_invalid_cmd_exception( error => $err ); | ||
| 407 | } | ||
| 408 | } | ||
| 409 | # Set internal current command name... | ||
| 410 | $app->set_current_command( $command_name ); | ||
| 411 | |||
| 412 | # If requested, parse [app-opts] and initialize application... | ||
| 413 | # (this is an optional step because in interactive mode, it should not be | ||
| 414 | # done for every request) | ||
| 415 | $app->_handle_global_app_options() if $initialize_app; | ||
| 416 | |||
| 417 | # Leave '[cmd-opts] [cmd-args]' in @ARGV... | ||
| 418 | @ARGV = @command_opts_and_args; | ||
| 419 | |||
| 420 | return 1; | ||
| 421 | } | ||
| 422 | |||
| 423 | sub run { | ||
| 424 | my ($app, %param) = @_; | ||
| 425 | |||
| 426 | # Auto-instantiate if necessary... | ||
| 427 | unless( ref $app ) { | ||
| 428 | my $class = $app; | ||
| 429 | $app = $class->new(); | ||
| 430 | } | ||
| 431 | # Determine whether to do initialization -- if not explicitly indicated, | ||
| 432 | # default to doing initialization only if it has not yet been done... | ||
| 433 | my $initialize = $param{initialize}; | ||
| 434 | $initialize = not $app->{_initialized} unless defined $initialize; | ||
| 435 | |||
| 436 | # Parse request; perform initialization... | ||
| 437 | eval { $app->_parse_request( initialize => $initialize ) }; | ||
| 438 | if( catch my $e ) { $app->handle_exception($e); return } | ||
| 439 | |||
| 440 | my $command_name = $app->get_current_command(); | ||
| 441 | |||
| 442 | # Lazy registration of commands... | ||
| 443 | my $command = $app->registered_command_object( $command_name ) | ||
| 444 | || $app->register_command( $command_name ); | ||
| 445 | |||
| 446 | # Parse command options and auto-generate minimal usage message... | ||
| 447 | my ($cmd_options, $cmd_usage); | ||
| 448 | my $currently_interactive = $app->get_interactivity_mode(); | ||
| 449 | my $format = "$command_name %o ..."; # Getopt::Long::Descriptive format string | ||
| 450 | $format = '%c '.$format unless $currently_interactive; # (%c is command name -- irrelevant in interactive mode) | ||
| 451 | |||
| 452 | # (configure Getopt::Long to stop consuming tokens when first non-option is | ||
| 453 | # encountered on input stream) | ||
| 454 | my $getopt_configuration = { getopt_conf => [qw(require_order)] }; | ||
| 455 | eval { ($cmd_options, $cmd_usage) = | ||
| 456 | describe_options( $format, $command->option_spec, $getopt_configuration ) | ||
| 457 | }; | ||
| 458 | # (handle failed command options parsing) | ||
| 459 | if( catch my $e ) { | ||
| 460 | if( $e->isa('CLI::Framework::Exception') ) { | ||
| 461 | $app->handle_exception($e); | ||
| 462 | return; | ||
| 463 | } | ||
| 464 | eval{ throw_cmd_opts_parse_exception( error => $e ) }; | ||
| 465 | if( catch my $e ) { $app->handle_exception( $e ); return } | ||
| 466 | } | ||
| 467 | $command->set_default_usage( $cmd_usage->text ); | ||
| 468 | |||
| 469 | # Share session data with command... | ||
| 470 | # (init() method may have populated shared session data in cache for use by all commands) | ||
| 471 | $command->set_cache( $app->cache ); | ||
| 472 | |||
| 473 | # --- APP HOOK: COMMAND PRE-DISPATCH --- | ||
| 474 | $app->pre_dispatch( $command ); | ||
| 475 | |||
| 476 | # --- RUN COMMAND --- | ||
| 477 | my $output; | ||
| 478 | eval { $output = $command->dispatch( $cmd_options, @ARGV ) }; | ||
| 479 | if( catch my $e ) { $app->handle_exception($e); return } | ||
| 480 | |||
| 481 | # Display output of command, if any... | ||
| 482 | $app->render( $output ) if defined $output; | ||
| 483 | |||
| 484 | return 1; | ||
| 485 | } | ||
| 486 | |||
| 487 | ############################### | ||
| 488 | # | ||
| 489 | # INTERACTIVITY | ||
| 490 | # | ||
| 491 | ############################### | ||
| 492 | |||
| 493 | sub get_interactivity_mode { $_[0]->{_interactive} } | ||
| 494 | sub set_interactivity_mode { $_[0]->{_interactive} = $_[1] } | ||
| 495 | |||
| 496 | sub is_interactive_command { | ||
| 497 | my ($app, $command_name) = @_; | ||
| 498 | |||
| 499 | my @noninteractive_commands = $app->noninteractive_commands(); | ||
| 500 | |||
| 501 | # Command must be valid... | ||
| 502 | return 0 unless $app->is_valid_command_name( $command_name ); | ||
| 503 | |||
| 504 | # Command must NOT be non-interactive... | ||
| 505 | return 1 unless grep { $command_name eq $_ } @noninteractive_commands; | ||
| 506 | |||
| 507 | return 0; | ||
| 508 | } | ||
| 509 | |||
| 510 | sub get_interactive_commands { | ||
| 511 | my ($app) = @_; | ||
| 512 | |||
| 513 | my @valid_commands = $app->_valid_command_names; | ||
| 514 | |||
| 515 | # All valid commands are enabled in non-interactive mode... | ||
| 516 | return @valid_commands unless( $app->get_interactivity_mode() ); | ||
| 517 | |||
| 518 | # ...otherwise, in interactive mode, include only interactive commands... | ||
| 519 | my @command_names; | ||
| 520 | for my $c ( @valid_commands ) { | ||
| 521 | push @command_names, $c if $app->is_interactive_command( $c ); | ||
| 522 | } | ||
| 523 | return @command_names; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub run_interactive { | ||
| 527 | my ($app, %param) = @_; | ||
| 528 | |||
| 529 | # Auto-instantiate if necessary... | ||
| 530 | unless( ref $app ) { | ||
| 531 | my $class = $app; | ||
| 532 | $app = $class->new(); | ||
| 533 | } | ||
| 534 | $app->set_interactivity_mode(1); | ||
| 535 | |||
| 536 | # If default command is non-interactive, reset it, remembering default... | ||
| 537 | my $orig_default_command = $app->get_default_command(); | ||
| 538 | if( grep { $orig_default_command eq $_ } $app->noninteractive_commands() ) { | ||
| 539 | $app->set_default_command( 'help' ); | ||
| 540 | } | ||
| 541 | # If initialization indicated, run init() and handle existing input... | ||
| 542 | eval { $app->_parse_request( initialize => $param{initialize} ) | ||
| 543 | if $param{initialize} | ||
| 544 | }; | ||
| 545 | if( catch my $e ) { $app->handle_exception($e); return } | ||
| 546 | |||
| 547 | # Find how many prompts to display in sequence between displaying menu... | ||
| 548 | my $menu_cmd = $app->registered_command_object('menu') | ||
| 549 | || $app->register_command( 'menu' ); | ||
| 550 | $menu_cmd->isa( 'CLI::Framework::Command::Menu' ) | ||
| 551 | or throw_type_exception( | ||
| 552 | error => "Menu command must be a subtype of " . | ||
| 553 | "CLI::Framework::Command::Menu" ); | ||
| 554 | |||
| 555 | my $invalid_request_threshold = $param{invalid_request_threshold} | ||
| 556 | || $menu_cmd->line_count(); # num empty prompts b4 re-displaying menu | ||
| 557 | |||
| 558 | $app->_run_cmd_processing_loop( | ||
| 559 | menu_cmd => $menu_cmd, | ||
| 560 | invalid_request_threshold => $invalid_request_threshold | ||
| 561 | ); | ||
| 562 | # Restore original default command... | ||
| 563 | $app->set_default_command( $orig_default_command ); | ||
| 564 | } | ||
| 565 | |||
| 566 | sub _run_cmd_processing_loop { | ||
| 567 | my ($app, %param) = @_; | ||
| 568 | |||
| 569 | my $menu_cmd = $param{menu_cmd}; | ||
| 570 | my $invalid_request_threshold = $param{invalid_request_threshold}; | ||
| 571 | |||
| 572 | $app->render( $menu_cmd->run() ); | ||
| 573 | |||
| 574 | my ($cmd_succeeded, $invalid_request_count, $done) = (0,0,0); | ||
| 575 | until( $done ) { | ||
| 576 | if( $invalid_request_count >= $invalid_request_threshold ) { | ||
| 577 | # Reached threshold for invalid cmd requests => re-display menu... | ||
| 578 | $invalid_request_count = 0; | ||
| 579 | $app->render( $menu_cmd->run() ); | ||
| 580 | } | ||
| 581 | elsif( $cmd_succeeded ) { | ||
| 582 | # Last command request was successful => re-display menu... | ||
| 583 | $app->render( $menu_cmd->run() ); | ||
| 584 | $cmd_succeeded = $invalid_request_count = 0; | ||
| 585 | } | ||
| 586 | # Read a command request... | ||
| 587 | $app->read_cmd(); | ||
| 588 | |||
| 589 | if( @ARGV ) { | ||
| 590 | # Recognize quit requests... | ||
| 591 | if( $app->is_quit_signal($ARGV[0]) ) { | ||
| 592 | undef @ARGV; | ||
| 593 | last; | ||
| 594 | } | ||
| 595 | $app->_canonicalize_cmd($ARGV[0]); # translate cmd aliases | ||
| 596 | |||
| 597 | if( $app->is_interactive_command($ARGV[0]) ) { | ||
| 598 | if( $app->run() ) { | ||
| 599 | $cmd_succeeded = 1; | ||
| 600 | } | ||
| 601 | else { $invalid_request_count++ } | ||
| 602 | } | ||
| 603 | else { | ||
| 604 | $app->render( 'unrecognized command request: ' . join(' ',@ARGV) . "\n"); | ||
| 605 | $invalid_request_count++; | ||
| 606 | } | ||
| 607 | } | ||
| 608 | else { $invalid_request_count++ } | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | sub read_cmd { | ||
| 613 | my ($app) = @_; | ||
| 614 | |||
| 615 | require Text::ParseWords; | ||
| 616 | |||
| 617 | # Retrieve or cache Term::ReadLine object (this is necessary to save | ||
| 618 | # command-line history in persistent object)... | ||
| 619 | my $term = $app->{_readline}; | ||
| 620 | unless( $term ) { | ||
| 621 | require Term::ReadLine; | ||
| 622 | $term = Term::ReadLine->new('CLIF Application'); | ||
| 623 | select $term->OUT; | ||
| 624 | $app->{_readline} = $term; | ||
| 625 | |||
| 626 | #FIXME-TODO-CMDLINE_COMPLETION: | ||
| 627 | # # Arrange for command-line completion... | ||
| 628 | # my $attribs = $term->Attribs; | ||
| 629 | # $attribs->{completion_function} = $app->_cmd_request_completions(); | ||
| 630 | } | ||
| 631 | # Prompt for the name of a command and read input from STDIN. | ||
| 632 | # Store the individual tokens that are read in @ARGV. | ||
| 633 | my $command_request = $term->readline('> '); | ||
| 634 | if(! defined $command_request ) { | ||
| 635 | # Interpret CTRL-D (EOF) as a quit signal... | ||
| 636 | @ARGV = $app->quit_signals(); | ||
| 637 | print "\n"; # since EOF character is rendered as '' | ||
| 638 | } | ||
| 639 | else { | ||
| 640 | # Prepare command for usual parsing... | ||
| 641 | @ARGV = Text::ParseWords::shellwords( $command_request ); | ||
| 642 | $term->addhistory($command_request) | ||
| 643 | if $command_request =~ /\S/ and !$term->Features->{autohistory}; | ||
| 644 | } | ||
| 645 | return 1; | ||
| 646 | } | ||
| 647 | |||
| 648 | ##FIXME-TODO-CMDLINE_COMPLETION:this should only return interactive commands; it should pay attention | ||
| 649 | ##to its text/line/start args, ...; also: make it work with subcommands | ||
| 650 | ## --see Term::Readline::Gnu | ||
| 651 | #sub _cmd_request_completions { | ||
| 652 | # my ($app) = @_; | ||
| 653 | # return sub { | ||
| 654 | # my ($text, $line, $start) = @_; | ||
| 655 | # return $app->_valid_command_names; | ||
| 656 | # } | ||
| 657 | #} | ||
| 658 | |||
| 659 | sub is_quit_signal { | ||
| 660 | my ($app, $command_name) = @_; | ||
| 661 | |||
| 662 | my @quit_signals = $app->quit_signals(); | ||
| 663 | return grep { $command_name eq $_ } @quit_signals; | ||
| 664 | } | ||
| 665 | |||
| 666 | ############################### | ||
| 667 | # | ||
| 668 | # APPLICATION SUBCLASS HOOKS | ||
| 669 | # | ||
| 670 | ############################### | ||
| 671 | |||
| 672 | #XXX-CONSIDER: consider making default implementation of init(): | ||
| 673 | # $app->set_current_command('help') if $opts->{help} | ||
| 674 | sub init { 1 } | ||
| 675 | |||
| 676 | sub pre_dispatch { } | ||
| 677 | |||
| 678 | sub usage_text { } | ||
| 679 | |||
| 680 | sub option_spec { } | ||
| 681 | |||
| 682 | sub validate_options { 1 } | ||
| 683 | |||
| 684 | sub command_map { | ||
| 685 | help => 'CLI::Framework::Command::Help', | ||
| 686 | console => 'CLI::Framework::Command::Console', | ||
| 687 | menu => 'CLI::Framework::Command::Menu', | ||
| 688 | list => 'CLI::Framework::Command::List', | ||
| 689 | 'dump' => 'CLI::Framework::Command::Dump', | ||
| 690 | tree => 'CLI::Framework::Command::Tree', | ||
| 691 | alias => 'CLI::Framework::Command::Alias', | ||
| 692 | } | ||
| 693 | |||
| 694 | sub command_alias { } | ||
| 695 | |||
| 696 | sub noninteractive_commands { qw( console menu ) } | ||
| 697 | |||
| 698 | sub quit_signals { qw( q quit exit ) } | ||
| 699 | |||
| 700 | sub handle_exception { | ||
| 701 | my ($app, $e) = @_; | ||
| 702 | $app->render( $e->description . "\n\n" . $e->error ); | ||
| 703 | return; | ||
| 704 | } | ||
| 705 | |||
| 706 | sub render { | ||
| 707 | my ($app, $output) = @_; | ||
| 708 | |||
| 709 | #XXX-CONSIDER: consider built-in features to help simplify associating templates | ||
| 710 | #with commands (each command would probably have its own template for its | ||
| 711 | #output) | ||
| 712 | print $output; | ||
| 713 | } | ||
| 714 | |||
| 715 | ############################### | ||
| 716 | # | ||
| 717 | # CACHING | ||
| 718 | # | ||
| 719 | ############################### | ||
| 720 | |||
| 721 | package CLI::Framework::Cache; | ||
| 722 | |||
| 723 | use strict; | ||
| 724 | use warnings; | ||
| 725 | |||
| 726 | sub new { | ||
| 727 | my ($class) = @_; | ||
| 728 | |||
| 729 | bless { _cache => { } }, $class; | ||
| 730 | } | ||
| 731 | |||
| 732 | sub get { | ||
| 733 | my ($self, $k) = @_; | ||
| 734 | |||
| 735 | my $v = $self->{_cache}->{$k}; | ||
| 736 | return $v; | ||
| 737 | } | ||
| 738 | |||
| 739 | sub set { | ||
| 740 | my ($self, $k, $v) = @_; | ||
| 741 | |||
| 742 | $self->{_cache}->{$k} = $v; | ||
| 743 | return $v; | ||
| 744 | } | ||
| 745 | |||
| 746 | #------- | ||
| 747 | 1; | ||
| 748 | |||
| 749 | __END__ |