App-Cmd

 view release on metacpan or  search on metacpan

lib/App/Cmd.pm  view on Meta::CPAN

  {
    my $base = $self->_default_command_base;
    Class::Load::load_class($base);
    no strict 'refs';
    push @{"$into\::ISA"}, $base;
  }

  $self->_register_command($into);

  for my $plugin ($self->_plugin_plugins) {
    $plugin->import_from_plugin({ into => $into });
  }

  1;
}

sub _setup_ignore {
  my ($self, $val, $data) = @_;
  my $into = $data->{into};

  Carp::confess "App::Cmd -ignore setup requested for already-setup class"
    if $into->isa('App::Cmd::Command');

  $self->_register_ignore($into);

  1;
}

sub _plugin_plugins { return }

#pod =head1 SYNOPSIS
#pod
#pod in F<yourcmd>:
#pod
#pod   use YourApp;
#pod   YourApp->run;
#pod
#pod in F<YourApp.pm>:
#pod
#pod   package YourApp;
#pod   use App::Cmd::Setup -app;
#pod   1;
#pod
#pod in F<YourApp/Command/blort.pm>:
#pod
#pod   package YourApp::Command::blort;
#pod   use YourApp -command;
#pod   use strict; use warnings;
#pod
#pod   sub abstract { "blortex algorithm" }
#pod
#pod   sub description { "Long description on blortex algorithm" }
#pod
#pod   sub opt_spec {
#pod     return (
#pod       [ "blortex|X",  "use the blortex algorithm" ],
#pod       [ "recheck|r",  "recheck all results"       ],
#pod     );
#pod   }
#pod
#pod   sub validate_args {
#pod     my ($self, $opt, $args) = @_;
#pod
#pod     # no args allowed but options!
#pod     $self->usage_error("No args allowed") if @$args;
#pod   }
#pod
#pod   sub execute {
#pod     my ($self, $opt, $args) = @_;
#pod
#pod     my $result = $opt->{blortex} ? blortex() : blort();
#pod
#pod     recheck($result) if $opt->{recheck};
#pod
#pod     print $result;
#pod   }
#pod
#pod and, finally, at the command line:
#pod
#pod   knight!rjbs$ yourcmd blort --recheck
#pod
#pod   All blorts successful.
#pod
#pod =head1 DESCRIPTION
#pod
#pod App::Cmd is intended to make it easy to write complex command-line applications
#pod without having to think about most of the annoying things usually involved.
#pod
#pod For information on how to start using App::Cmd, see L<App::Cmd::Tutorial>.
#pod
#pod =method new
#pod
#pod   my $cmd = App::Cmd->new(\%arg);
#pod
#pod This method returns a new App::Cmd object.  During initialization, command
#pod plugins will be loaded.
#pod
#pod Valid arguments are:
#pod
#pod   no_commands_plugin - if true, the command list plugin is not added
#pod
#pod   no_help_plugin     - if true, the help plugin is not added
#pod
#pod   no_version_plugin  - if true, the version plugin is not added
#pod
#pod   show_version_cmd -   if true, the version command will be shown in the
#pod                        command list
#pod
#pod   plugin_search_path - The path to search for commands in. Defaults to
#pod                        results of plugin_search_path method
#pod
#pod If C<no_commands_plugin> is not given, L<App::Cmd::Command::commands> will be
#pod required, and it will be registered to handle all of its command names not
#pod handled by other plugins.
#pod
#pod If C<no_help_plugin> is not given, L<App::Cmd::Command::help> will be required,
#pod and it will be registered to handle all of its command names not handled by
#pod other plugins. B<Note:> "help" is the default command, so if you do not load
#pod the default help plugin, you should provide your own or override the
#pod C<default_command> method.
#pod

lib/App/Cmd.pm  view on Meta::CPAN

#pod actually execute the command.
#pod
#pod =cut

sub prepare_command {
  my ($self, @args) = @_;

  # figure out first-level dispatch
  my ($command, $opt, @sub_args) = $self->get_command(@args);

  # set up the global options (which we just determined)
  $self->set_global_options($opt);

  # find its plugin or else call default plugin (default default is help)
  if ($command) {
    $self->_prepare_command($command, $opt, @sub_args);
  } else {
    $self->_prepare_default_command($opt, @sub_args);
  }
}

sub _prepare_command {
  my ($self, $command, $opt, @args) = @_;
  if (my $plugin = $self->plugin_for($command)) {
    return $plugin->prepare($self, @args);
  } else {
    return $self->_bad_command($command, $opt, @args);
  }
}

sub _prepare_default_command {
  my ($self, $opt, @sub_args) = @_;
  $self->_prepare_command($self->default_command, $opt, @sub_args);
}

sub _bad_command {
  my ($self, $command, $opt, @args) = @_;
  print "Unrecognized command: $command.\n\nUsage:\n" if defined($command);

  # This should be class data so that, in Bizarro World, two App::Cmds will not
  # conflict.
  our $_bad++;
  $self->prepare_command(qw(commands --stderr));
}

END { exit 1 if our $_bad };

#pod =method default_command
#pod
#pod This method returns the name of the command to run if none is given on the
#pod command line.  The default default is "help"
#pod
#pod =cut

sub default_command { "help" }

#pod =method execute_command
#pod
#pod   $app->execute_command($cmd, \%opt, @args);
#pod
#pod This method will invoke C<validate_args> and then C<run> on C<$cmd>.
#pod
#pod =cut

sub execute_command {
  my ($self, $cmd, $opt, @args) = @_;

  local our $active_cmd = $cmd;

  $cmd->validate_args($opt, \@args);
  $cmd->execute($opt, \@args);
}

#pod =method plugin_search_path
#pod
#pod This method returns the plugin_search_path as set.  The default implementation,
#pod if called on "YourApp::Cmd" will return "YourApp::Cmd::Command"
#pod
#pod This is a method because it's fun to override it with, for example:
#pod
#pod   use constant plugin_search_path => __PACKAGE__;
#pod
#pod =cut

sub _default_command_base {
  my ($self) = @_;
  my $class = ref $self || $self;
  return "$class\::Command";
}

sub _default_plugin_base {
  my ($self) = @_;
  my $class = ref $self || $self;
  return "$class\::Plugin";
}

sub plugin_search_path {
  my ($self) = @_;

  my $dcb = $self->_default_command_base;
  my $ccb = $dcb eq 'App::Cmd::Command'
          ? $self->App::Cmd::_default_command_base
          : $self->_default_command_base;

  my @default = ($ccb, $self->_default_plugin_base);

  if (ref $self) {
    return $self->{plugin_search_path} //= \@default;
  } else {
    return \@default;
  }
}

#pod =method allow_any_unambiguous_abbrev
#pod
#pod If this method returns true (which, by default, it does I<not>), then any
#pod unambiguous abbreviation for a registered command name will be allowed as a
#pod means to use that command.  For example, given the following commands:
#pod
#pod   reticulate
#pod   reload
#pod   rasterize
#pod
#pod Then the user could use C<ret> for C<reticulate> or C<ra> for C<rasterize> and
#pod so on.
#pod
#pod =cut

sub allow_any_unambiguous_abbrev { return 0 }

lib/App/Cmd.pm  view on Meta::CPAN

  my ($self, @args) = @_;

  return (
    $self->usage_desc(@args),
    $self->global_opt_spec(@args),
    { getopt_conf => [ $self->_getopt_conf->@*, 'pass_through' ] },
  );
}

sub _getopt_conf {
  return [qw/pass_through/];
}

#pod =method usage
#pod
#pod   print $self->app->usage->text;
#pod
#pod Returns the usage object for the global options.
#pod
#pod =cut

sub usage { $_[0]{usage} };

#pod =method usage_desc
#pod
#pod The top level usage line. Looks something like
#pod
#pod   "yourapp <command> [options]"
#pod
#pod =cut

sub usage_desc {
  # my ($self) = @_; # no point in creating these ops, just to toss $self
  return "%c <command> %o";
}

#pod =method global_opt_spec
#pod
#pod Returns a list with help command unless C<no_help_plugin> has been specified or
#pod an empty list. Can be overridden for pre-dispatch option processing.  This is
#pod useful for flags like --verbose.
#pod
#pod =cut

sub global_opt_spec {
  my ($self) = @_;

  my $cmd = $self->{command};
  my %seen;
  my @help = grep { ! $seen{$_}++ }
             reverse sort map { s/^--?//; $_ }
             grep { $cmd->{$_} eq 'App::Cmd::Command::help' } keys %$cmd;

  return (@help ? [ join('|', @help) => "show help" ] : ());
}

#pod =method usage_error
#pod
#pod   $self->usage_error("Something's wrong!");
#pod
#pod Used to die with nice usage output, during C<validate_args>.
#pod
#pod =cut

sub usage_error {
  my ($self, $error) = @_;
  die "Error: $error\nUsage: " . $self->_usage_text;
}

sub _usage_text {
  my ($self) = @_;
  my $text = $self->usage->text;
  $text =~ s/\A(\s+)/!/;
  return $text;
}

#pod =head1 TODO
#pod
#pod =for :list
#pod * publish and bring in Log::Speak (simple quiet/verbose output)
#pod * publish and use our internal enhanced describe_options
#pod * publish and use our improved simple input routines
#pod
#pod =cut

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Cmd - write command line apps with less suffering

=head1 VERSION

version 0.340

=head1 SYNOPSIS

in F<yourcmd>:

  use YourApp;
  YourApp->run;

in F<YourApp.pm>:

  package YourApp;
  use App::Cmd::Setup -app;
  1;

in F<YourApp/Command/blort.pm>:

  package YourApp::Command::blort;
  use YourApp -command;
  use strict; use warnings;

  sub abstract { "blortex algorithm" }

  sub description { "Long description on blortex algorithm" }

  sub opt_spec {
    return (
      [ "blortex|X",  "use the blortex algorithm" ],
      [ "recheck|r",  "recheck all results"       ],
    );
  }

  sub validate_args {
    my ($self, $opt, $args) = @_;

    # no args allowed but options!
    $self->usage_error("No args allowed") if @$args;
  }

  sub execute {
    my ($self, $opt, $args) = @_;

    my $result = $opt->{blortex} ? blortex() : blort();

    recheck($result) if $opt->{recheck};

    print $result;
  }

and, finally, at the command line:

  knight!rjbs$ yourcmd blort --recheck

  All blorts successful.

=head1 DESCRIPTION

App::Cmd is intended to make it easy to write complex command-line applications
without having to think about most of the annoying things usually involved.

For information on how to start using App::Cmd, see L<App::Cmd::Tutorial>.

=head1 PERL VERSION

This library should run on perls released even a long time ago.  It should
work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased.  The version may be increased
for any reason, and there is no promise that patches will be accepted to
lower the minimum required perl.

=head1 METHODS

=head2 new

  my $cmd = App::Cmd->new(\%arg);

This method returns a new App::Cmd object.  During initialization, command
plugins will be loaded.

Valid arguments are:

  no_commands_plugin - if true, the command list plugin is not added

  no_help_plugin     - if true, the help plugin is not added

  no_version_plugin  - if true, the version plugin is not added

  show_version_cmd -   if true, the version command will be shown in the
                       command list

  plugin_search_path - The path to search for commands in. Defaults to

lib/App/Cmd.pm  view on Meta::CPAN

If C<L</prepare_args>> is not changed and there are no arguments in C<@ARGV>,
this method is called and should return an arrayref to be used as the arguments
to the program.  By default, it returns an empty arrayref.

=head2 abstract

   sub abstract { "command description" }

Defines the command abstract: a short description that will be printed in the
main command options list.

=head2 description

   sub description { "Long description" }

Defines a longer command description that will be shown when the user asks for
help on a specific command.

=head2 arg0

=head2 full_arg0

  my $program_name = $app->arg0;

  my $full_program_name = $app->full_arg0;

These methods return the name of the program invoked to run this application.
This is determined by inspecting C<$0> when the App::Cmd object is
instantiated, so it's probably correct, but doing weird things with App::Cmd
could lead to weird values from these methods.

If the program was run like this:

  knight!rjbs$ ~/bin/rpg dice 3d6

Then the methods return:

  arg0      - rpg
  full_arg0 - /Users/rjbs/bin/rpg

These values are captured when the App::Cmd object is created, so it is safe to
assign to C<$0> later.

=head2 prepare_command

  my ($cmd, $opt, @args) = $app->prepare_command(@ARGV);

This method will load the plugin for the requested command, use its options to
parse the command line arguments, and eventually return everything necessary to
actually execute the command.

=head2 default_command

This method returns the name of the command to run if none is given on the
command line.  The default default is "help"

=head2 execute_command

  $app->execute_command($cmd, \%opt, @args);

This method will invoke C<validate_args> and then C<run> on C<$cmd>.

=head2 plugin_search_path

This method returns the plugin_search_path as set.  The default implementation,
if called on "YourApp::Cmd" will return "YourApp::Cmd::Command"

This is a method because it's fun to override it with, for example:

  use constant plugin_search_path => __PACKAGE__;

=head2 allow_any_unambiguous_abbrev

If this method returns true (which, by default, it does I<not>), then any
unambiguous abbreviation for a registered command name will be allowed as a
means to use that command.  For example, given the following commands:

  reticulate
  reload
  rasterize

Then the user could use C<ret> for C<reticulate> or C<ra> for C<rasterize> and
so on.

=head2 global_options

  if ($cmd->app->global_options->{verbose}) { ... }

This method returns the running application's global options as a hashref.  If
there are no options specified, an empty hashref is returned.

=head2 set_global_options

  $app->set_global_options(\%opt);

This method sets the global options.

=head2 command_names

  my @names = $cmd->command_names;

This returns the commands names which the App::Cmd object will handle.

=head2 command_groups

  my @groups = $cmd->commands_groups;

This method can be implemented to return a grouped list of command names with
optional headers. Each group is given as arrayref and each header as string.
If an empty list is returned, the commands plugin will show two groups without
headers: the first group is for the "help" and "commands" commands, and all
other commands are in the second group.

=head2 command_plugins

  my @plugins = $cmd->command_plugins;

This method returns the package names of the plugins that implement the
App::Cmd object's commands.

=head2 plugin_for

  my $plugin = $cmd->plugin_for($command);

This method returns the plugin (module) for the given command.  If no plugin
implements the command, it returns false.

=head2 get_command

  my ($command_name, $opt, @args) = $app->get_command(@args);

Process arguments and into a command name and (optional) global options.

=head2 usage

  print $self->app->usage->text;

Returns the usage object for the global options.

=head2 usage_desc

The top level usage line. Looks something like

  "yourapp <command> [options]"

=head2 global_opt_spec

Returns a list with help command unless C<no_help_plugin> has been specified or
an empty list. Can be overridden for pre-dispatch option processing.  This is
useful for flags like --verbose.

=head2 usage_error

  $self->usage_error("Something's wrong!");

Used to die with nice usage output, during C<validate_args>.

=head1 TODO

=over 4

=item *

publish and bring in Log::Speak (simple quiet/verbose output)

=item *

publish and use our internal enhanced describe_options

=item *

publish and use our improved simple input routines

=back

=head1 AUTHOR

Ricardo Signes <cpan@semiotic.systems>

=head1 CONTRIBUTORS

=for stopwords Adam Prime ambs Andreas Hernitscheck A. Sinan Unur Chris 'BinGOs' Williams David Golden Steinbrunner Davor Cubranic Denis Ibaev Diab Jerius Glenn Fowler Ingy dot Net Jakob Voss Jérôme Quelin John SJ Anderson Karen Etheridge Kent Fred...

=over 4

=item *

Adam Prime <aprime@oanda.com>

=item *

ambs <ambs@cpan.org>

=item *

Andreas Hernitscheck <andreash@lxhe.(none)>

=item *

A. Sinan Unur <nanis@cpan.org>

=item *

Chris 'BinGOs' Williams <chris@bingosnet.co.uk>

=item *

David Golden <dagolden@cpan.org>

=item *

David Steinbrunner <dsteinbrunner@pobox.com>

=item *

Davor Cubranic <cubranic@stat.ubc.ca>



( run in 0.766 second using v1.01-cache-2.11-cpan-140bd7fdf52 )