Class Puppet::Application
In: lib/puppet/application.rb
Parent: Object

This class handles all the aspects of a Puppet application/executable

  • setting up options
  • setting up logs
  • choosing what to run

Usage

The application is a Puppet::Application object that register itself in the list of available application. Each application needs a name and a getopt options description array.

The executable uses the application object like this:

     Puppet::Application[:example].run

Puppet::Application.new(:example) do

    preinit do
        # perform some pre initialization
        @all = false
    end

    # dispatch is called to know to what command to call
    dispatch do
        ARGV.shift
    end

    option("--arg ARGUMENT") do |v|
        @args << v
    end

    option("--debug", "-d") do |v|
        @debug = v
    end

    option("--all", "-a:) do |v|
        @all = v
    end

    unknown do |opt,arg|
        # last chance to manage an option
        ...
        # let's say to the framework we finally handle this option
        true
    end

    command(:read) do
        # read action
    end

    command(:write) do
        # writeaction
    end

end

Preinit

The preinit block is the first code to be called in your application, before option parsing, setup or command execution.

Options

Puppet::Application uses OptionParser to manage the application options. Options are defined with the option method to which are passed various arguments, including the long option, the short option, a description… Refer to OptionParser documentation for the exact format.

  • If the option method is given a block, this one will be called whenever

the option is encountered in the command-line argument.

  • If the option method has no block, a default functionnality will be used, that

stores the argument (or true/false if the option doesn‘t require an argument) in the global (to the application) options array.

  • If a given option was not defined by a the option method, but it exists as a Puppet settings:
 * if +unknown+ was used with a block, it will be called with the option name and argument
 * if +unknown+ wasn't used, then the option/argument is handed to Puppet.settings.handlearg for
   a default behavior

help is managed directly by the Puppet::Application class, but can be overriden.

Setup

Applications can use the setup block to perform any initialization. The defaul setup behaviour is to: read Puppet configuration and manage log level and destination

What and how to run

If the dispatch block is defined it is called. This block should return the name of the registered command to be run. If it doesn‘t exist, it defaults to execute the main command if defined.

Methods

Included Modules

Puppet::Util Puppet::Util

Attributes

opt_parser  [R] 
options  [R] 

Public Class methods

this is used for testing

Public Instance methods

used to declare accessor in a more natural way in the various applications

used to declare code to choose which command to run

initialize default application behaviour

used to declare code that handle an option

used to execute code before running anything else

This is the main application entry point

used to declare code run instead the default setup

used as a catch-all for unknown option

[Validate]