Class: Yacl::Define::Cli::Options

Inherits:
Loader
  • Object
show all
Defined in:
lib/yacl/define/cli/options.rb

Overview

Public: Inherit from Cli::Options to define your the mapping between your application properties and commandline switches.

The class you create from here is to be used in conjuction with a class you create that is a child class of Cli::Parser.

Example:

class MyOptions < ::Yacl::Define::Cli::Options
  opt 'log.level', :long => 'log-level', :short => 'l', :description => "Logging Level", :cast => :string
  opt 'config.dir',:long => 'config-dir', :short => 'c', :description => "Configuration directory", :cast => :string
end

o = MyOptions.new( 'log-level' => 'debug', 'config-dir' => '/tmp/foo' )
o.properties # => Properties instance with 'log.level' => debug and 'config.dir' => '/tmp/foo'

Instance Attribute Summary

Attributes inherited from Loader

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Loader

extract_path, #initialize, mapify_key, #reference_properties

Constructor Details

This class inherits a constructor from Yacl::Loader

Class Method Details

.opt(name, params = {}) ⇒ Object

Public: Declare a property and its corresponding commandline options.

name - The property name as a String params - The Hash options to accompany the property name (default: {})

:long        - The long commandline option. Without leading dashes
:short       - The short commandline option. Without leading dashes
:description - The text description of this commandline option
:cast        - What to cast the value of the commandline option
               to. This can be one of: :integer, :float, :string,
               :boolean

Examples:

opt 'app.thing.one', :long => 'thing-one', :short => 't', :description => "Thing One", :cast => :string

Returns nothing.



48
49
50
# File 'lib/yacl/define/cli/options.rb', line 48

def self.opt(name, params = {} )
  opt_list << Option.new( name, params[:long], params[:short], params[:description ], params[:cast] )
end

.opt_listObject

Internal: access to the defined list of options

Returns the Array of options



28
29
30
# File 'lib/yacl/define/cli/options.rb', line 28

def self.opt_list
  @opt_list ||= []
end

Instance Method Details

#eachObject

Public: yield each defined Option in this class to the caller

Yields the Option instance of the iteration.

Returns nothing.



65
66
67
68
69
# File 'lib/yacl/define/cli/options.rb', line 65

def each
  opt_list.each do |o|
    yield o
  end
end

#load_properties(hash) ⇒ Object

Internal: convert the given hash of long options into a Properties instance using the defined options.

It is assumed that the commandline has already been parsed and that the hash passed in is the long options that were parsed out of the commandline.

hash - a Hash of long options and their values.

Returns a Properties instance



88
89
90
91
92
93
94
95
# File 'lib/yacl/define/cli/options.rb', line 88

def load_properties( hash )
  prop_hash = {}
  opt_list.each do |o|
    next unless hash.has_key?( o.long )
    prop_hash[o.property_name] = hash[o.long]
  end
  return Yacl::Properties.new( prop_hash )
end

#opt_listObject

Internal: Return the list of defined options for this Class.

Returns an Array of Option instances



74
75
76
# File 'lib/yacl/define/cli/options.rb', line 74

def opt_list
  self.class.opt_list
end

#propertiesObject

Public: Load the Properties from the options that were passed to the initializer.

Returns a Properties instance.



56
57
58
# File 'lib/yacl/define/cli/options.rb', line 56

def properties
  load_properties( @options )
end