CommandLineApplicationUseroContributed Perl CommandLineApplicationComponent(3)NAMENOCpulse::CommandLineApplicationComponent - an abstract superclass for
classes that participate in the use of command line switches in an
application.
SYNOPSIS
package MyClass;
use NOCpulse::CommandLineApplicationComponent;
@ISA qw(NOCpulse::CommandLineApplicationComponent);
sub overview {
my $self = shift();
return "This component provides access to a shell via ssh";
}
sub registerSwitches {
my $self = shift();
$self->SUPER::registerSwitches; # good practice
$self->addSwitch('login','=s',1,'root@localhost','SSH login string');
$self->addSwitch('command','=s',1,'/bin/true','Command to run');
}
sub doit {
my $self = shift();
my $login = $self->get_login;
my $command = $self->get_command;
return `ssh $login $command`
}
package main;
use MyClass;
$thing = MyClass->newInitialized;
if ($thing->commandLineIsValid) {
print $thing->doit;
} else {
$thing->printUsage;
}
DESCRIPTIONNOCpulse::CommandLineApplicationComponent helps you write modular
command line applications without having to worry about dealing with
command line switch specifications and validation, and without having
to deal with writing help methods.
The typical NOCpulse::CommandLineApplicationComponent based application
will have one or more subclasses of this class, with one of them acting
as a "driver" for the rest (e.g. it would be responsible for dealing
with a --help switch).
It is especially handy for doing "polymorphic command line apps", where
documentation and switch requirements change depending on other
switches (e.g. you can have a switch in the mainline that specifies one
or more classes that must be used/instantiated dynamically).
REQUIRES
NOCpulse::PersistentObject, NOCpulse::CommandLineSwitch, Text::Wrap,
Carp
MODULE VARIABLES
@Instances - holds all instances of if this class and its subclasses
CLASS VARIABLES
$OutputTarget
==cut
CLASS METHODSAddInstance()
Register an instance with the
NOCpulse::CommandLineApplicationComponent class. This is done
automatically as part of object construction - you should probably
never call this.
commandLineIsValid()
(can also be called as an instance method) Called by the mainline
logic in your application only once, this causes all switches in
all components to calculate their validity. If everything is
valid, this returns true, else false.
printUsage()
(can be called as an instance method) Prints usage for all
NOCpulse::CommandLineApplicationComponent instances in the current
application.
printUsageNotes()
Callout for subclasses to add extra info to its usage - by default
does nothing.
printUsageAsXML()
(can be called as an instance method) Prints usage for any
components that have problems with any command line switch.
INSTANCE METHODSregisterSwitches()
Abstract method - you must override this in your subclass and make
calls to addSwitch() if your component needs switches. This method
gets called automatically during initialization of the component.
overview()
Abstract method - you must override this and define it such that it
returns a string describing the component.
newNamed(<name>)
Overrides NOCpulse::PersistentObject behavior, which would cache
the instance Cpulse:: in a way that is not useful to us here (
NOCpulse::PersistentObject uses a hash, we want an array)
instVarDefinitions()
Defines the following variables:
usage - (I don't think this is used - it's probably cruft)
switches - A hash of all the switches this component defines/owns ( name=>value where
value is an instance of NOCpulse::CommandLineSwitch)
initialize()
Initializes all switches. If you override this in your
subclass(es), be SURE to call $self->SUPER::initialize !!
switchesAreValid()
Returns true if switches are valid for this component
switchValue(<name>)
Returns the value of the switch whose name is <name>. You can get
here via get_name as well (see doesNotUnderstand).
hasSwitch(<name>)
Returns true if a switch whose name is <name> exists.
switch(<name>)
Returns the switch object whose name is <name>.
switchIsValid(<name>)
Returns true if a switch whose name is <name> is valid.
addSwitch(<name>,<spec>,<required>,<default>,<usage>)
Adds a NOCpulse::CommandLineSwitch object as described by the
parameters to this method to the component.
<name> = the name of the switch <spec> = the Getopt::Long
specification for the switch <required> = if true, the switch is
required <default> = default value for the switch (use undef if
none exists) <usage> = a string describing the switch
hasSwitches()
Returns a (possibly empty) list of the names of all switches
defined for the component.
print()
Printing via this method allows you to redirect output to
$NOCpulse::CommandLineApplicationComponent::OutputTarget, which may
be a glob or object or scalar.
usageAsSql()
Returns a string consisting of sql commands that update NOCpulse
tables which describe probes and their switches.
doesNotUnderstand()
Adds to NOCpulse::PersistentObject doesNotUnderstand logic such
that calls to get_xxx will also return the value of a switch whose
name is xxx
FreeAllInstances()
Cause all instances of NOCpulse::CommandLineApplicationComponent to
be cleared from memory (assuming nothing else is holding on to
them).
POD ERRORS
Hey! The above document had some coding errors, which are explained
below:
Around line 371:
You forgot a '=back' before '=head1'
Around line 379:
You forgot a '=back' before '=head1'
Around line 422:
You forgot a '=back' before '=head1'
perl v5.14.02009-02-19CommandLineApplicationComponent(3)