Maroš Kollár > MooseX-App-1.27 > MooseX::App

Download:
MooseX-App-1.27.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 1.27   Source   Latest Release: MooseX-App-1.30

NAME ^

MooseX::App - Write user-friendly command line apps with even less suffering

SYNOPSIS ^

In your base class:

  package MyApp;
  use MooseX::App qw(Color);
 
  option 'global_option' => (
      is            => 'rw',
      isa           => 'Bool',
      documentation => q[Enable this to do fancy stuff],
  ); # Global option
  
  has 'private' => ( 
      is              => 'rw',
  ); # not exposed

Write multiple command classes (If you have only a single command class you should use MooseX::App::Simple instead)

  package MyApp::SomeCommand;
  use MooseX::App::Command; # important (also imports Moose)
  extends qw(MyApp); # optional, only if you want to use global options from base class
  
  # Positional parameter
  parameter 'some_parameter' => (
      is            => 'rw',
      isa           => 'Str',
      required      => 1,
      documentation => q[Some parameter that you need to supply],
  );
  
  option 'some_option' => (
      is            => 'rw',
      isa           => 'Int',
      required      => 1,
      documentation => q[Very important option!],
  ); # Option
  
  sub run {
      my ($self) = @_;
      # Do something
  }

And then you need a simple wrapper script (called eg. myapp):

 #!/usr/bin/env perl
 use MyApp;
 MyApp->new_with_command->run;

On the command line:

 bash$ myapp help
 usage:
     myapp <command> [long options...]
     myapp help
 
 global options:
     --global_option    Enable this to do fancy stuff [Flag]
     --help --usage -?  Prints this usage information. [Flag]
 
 available commands:
     some_command    Description of some command
     another_command Description of another command
     help            Prints this usage information

or

 bash$ myapp some_command --help
 usage:
     myapp some_command <SOME_PARAMETER> [long options...]
     myapp help
     myapp some_command --help
 
 parameters:
     some_parameter     Some parameter that you need to supply [Required]
 
 options:
     --global_option    Enable this to do fancy stuff [Flag]
     --some_option      Very important option! [Int,Required]
     --help --usage -?  Prints this usage information. [Flag]

DESCRIPTION ^

MooseX-App is a highly customisable helper to write user-friendly command line applications without having to worry about most of the annoying things usually involved. Just take any existing Moose class, add a single line (use MooseX-App qw(PluginA PluginB ...);) and create one class for each command in an underlying namespace. Options and positional parameters can be defined as simple Moose accessors.

MooseX-App will then

Commandline options are defined using the 'option' keyword which accepts the same attributes as Moose' 'has' keyword.

  option 'some_option' => (
      is            => 'rw',
      isa           => 'Str',
  );

This is equivalent to

  has 'some_option' => (
      is            => 'rw',
      isa           => 'Str',
      traits        => ['AppOption'],   # Load extra metaclass
      cmd_type      => 'option',        # Set attribute type
  );

Positional parameters are defined with the 'parameter' keyword

  parameter 'some_option' => (
      is            => 'rw',
      isa           => 'Str',
  );

This is equivalent to

  has 'some_option' => (
      is            => 'rw',
      isa           => 'Str',
      traits        => ['AppOption'],
      cmd_type      => 'parameter',
  );

Read the Tutorial for getting started with a simple MooseX::App command line application.

METHODS ^

new_with_command

 my $myapp_command = MyApp->new_with_command();

This method reads the command line arguments from the user and tries to create a command object. If it fails it retuns a MooseX::App::Message::Envelope object holding an error message.

You can pass a hash of default params to new_with_command

 MyApp->new_with_command(%default);

initialize_command_class

 my $myapp_command = MyApp->initialize_command_class($command_name,%default);

Helper method to initialize the command class for the given command.

GLOBAL OPTIONS ^

app_base

 app_base 'my_script';

Usually MooseX::App will take the name of the calling wrapper script to construct the program name in various help messages. This name can be changed via the app_base function.

app_namespace

 app_namespace 'MyApp::Commands', 'YourApp::MoreCommands';

Usually MooseX::App will take the package name of the base class as the namespace for commands. This namespace can be changed and you can add several other namespaces.

app_fuzzy

 app_fuzzy(1); # default
 OR
 app_fuzzy(0);

Enables fuzzy matching of commands and attributes. Is turned on by default.

app_strict

 app_strict(0); # default 
 OR
 app_strict(1); 

If strict is enabled the program will terminate with an error message if superfluous/unknown positional parameters are supplied. If disabled all extra parameters will be copied to the extra_argv attribute.

The command_strict config in the command classes allows one to set this option individually for each command.

app_command_name

 app_command_name {
     my ($package) = shift;
     # munge package name;
     return $command_name;
 };

This sub can be used to control how package names should be translated to command names.

GLOBAL ATTRIBUTES ^

All MooseX::App classes will have two extra attributes/accessors

extra_argv

Carries all parameters from @ARGV that were not consumed.

help_flag

Help flag that is set when help was requested.

ATTRIBUTE OPTIONS ^

Refer to MooseX::App::Meta::Role::Attribute::Option for detailed documentation.

PLUGINS ^

The behaviour of MooseX-App can be customised with plugins. To load a plugin just pass a list of plugin names after the use MooseX-App statement.

 use MooseX::App qw(PluginA PluginB);

Read the Writing MooseX-App Plugins documentation on how to create your own plugins.

Currently the following plugins are shipped with MooseX::App

CAVEATS & KNOWN BUGS ^

Startup time may be an issue. If you do not require plugins and ability for fine grained customisation then you should probably use MooX::Options or MooX::Cmd.

In some cases - especially when using non-standard class inheritance - you may end up with command classes lacking the help attribute. In this case you need to include the following line in your base class

 with qw(MooseX::App::Role::Common);

SEE ALSO ^

Read the Tutorial for getting started with a simple MooseX::App command line application.

For alternatives you can check out

MooseX::App::Cmd, MooseX::Getopt, MooX::Options, MooX::Cmd and App::Cmd

SUPPORT ^

Please report any bugs or feature requests to bug-moosex-app@rt.cpan.org, or through the web interface at http://rt.cpan.org/Public/Bug/Report.html?Queue=MooseX-App. I will be notified, and then you'll automatically be notified of progress on your report as I make changes.

AUTHOR ^

    Maroš Kollár
    CPAN ID: MAROS
    maros [at] k-1.com
    
    http://www.k-1.com

CONTRIBUTORS ^

In no particular order: Andrew Jones, George Hartzell, Steve Nolte, Michael G, Thomas Klausner, Yanick Champoux, Edward Baudrez, David Golden, J.R. Mash, Thilo Fester, Gregor Herrmann

COPYRIGHT ^

MooseX::App is Copyright (c) 2012-14 Maroš Kollár.

This library is free software and may be distributed under the same terms as perl itself. The full text of the licence can be found in the LICENCE file included with this module.

syntax highlighting: