Class | Puppet::Application |
In: |
lib/puppet/application.rb
|
Parent: | Object |
This class handles all the aspects of a Puppet application/executable
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
The preinit block is the first code to be called in your application, before option parsing, setup or command execution.
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.
the option is encountered in the command-line argument.
stores the argument (or true/false if the option doesn‘t require an argument) in the global (to the application) options array.
* 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.
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
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.
opt_parser | [R] | |
options | [R] |