6.5

Table Of Contents
VMware, Inc. 21
Chapter 2 Writing vSphere SDK for Perl Scripts
ThevSphereSDKforPerlhasdefinedallcommonoptionsusingattributesandsubroutinesspecifiedinthe
VILib::Optspackage.YoucansimilarlyusetheVILib::Optspackagetocreatecustomoptionsforyourown
applicationsandscripts,tosimplifyuseofyourscript,ortoallowuserstospecifyotherinformation.
Example 2
1definesanentityoptionthatmustbemadeavailabletothescriptatruntime.Theoption
specifieswhichoftheavailableentitytypesispassedasaparametertotheVim::find_entity_views()
subroutineforfurtherprocessing.AnydirectorindirectsubclassofManagedEntityisavalidoption(for
exampleHostSystem
, ResourcePool,orVirtualMachine).Theexamplecreatesandparsesanew
commandlineoption.
1Theexampledeclarestheoptionasahash.Thehashkeyistheoptionname,andthevalueisahashref
containingGetopt::Longstyleoptionattributes.SeeTable 22forattributedetails.
Example 21createsarequiredcommandlineoption
thatacceptsastringvalue,asfollows:
my %opts = (
entity => {
type => "=s",
variable => "VI_ENTITY",
help => "ManagedEntity type: HostSystem, etc",
required => 1,
},
);
Table 22listsallattributesyoucanusetodefinecommandlineoptions.Thecodefragmentin“Step1:
ImportthevSphereSDKforPerlModules”onpage 20aboveusesonlytype,variable,help,and
required.Forrelatedinformation,seethedocumentationfortheGetopt::Longmodule.
2Theexampleadds
theoptiontothedefaultoptionsusingtheOpts::add_options()subroutine:
Opts::add_options(%opts);
3Theexampleparsesandvalidatestheoptionsbeforeconnectingtotheserver,asfollows:
Opts::parse();
Opts::validate();
InExample 21,theentityoptionisrequired,sothescriptcannotrununlesstheuserpassesintheoption
nameandvalue(see“SpecifyingOptions”onpage 12).
Table 2-2. Attributes for Defining New Options
Attribute Description
default Defaultvalueusediftheoptionisnotexplicitlyset.Anunsetoptionwithnodefaultreturnsundef
tothecallingscript.
func Enablescreatingderivedoptions.Setfunctoanexternalcodereferencetorunthecodewhenthe
SDKqueriesthevalueoftheoption.
help Descriptivetextexplainingtheoption,
displayedinthescript’shelpmessage.
required Ifthisattributeissetto1,usersmustprovideavalueforthisoptionorthescriptexitsanddisplay
thehelpmessage.Setto1torequireavalue.Setto0ifthevalueisoptional.
variable Allowsyoutospecifytheoption
inanenvironmentvariableoraconfigurationfile.See“Specifying
Options”onpage 12.
type UsesPerlGetoptstylesyntaxtospecifyoptiontypeandwhethertheoptionisrequiredoroptional.
Usedoublequotestoindicatethatoptiondoesn’tacceptavalue.Thedefaultnumericvalueis0.The
defaultstringvalue
isʺʺ(emptystring).Youcanuseoneofthefollowing:
Equalsign(=)mandatory
Colon(:)optional
sstring
iinteger
ffloat