Class: Yacl::Define::Cli::Parser

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

Overview

Public: Parser is a Loader that uses a Cli::Options class and the Trollop parser to convert commandline options into a Properties instance. It is to be inherited from so you can have your own parser per commandline program in your application suite.

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

class MyParser < ::Yacl::Define::Cli::Parser
  banner "Usage: myapp [options]+"
  options MyOptions
end

p = MyParser.new( :argv => ARGV )
p.properties #=> a Properties instance

Instance Attribute Summary

Attributes inherited from Loader

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Loader

extract_path, mapify_key, #reference_properties

Constructor Details

#initialize(opts = {}) ⇒ Parser

Create a new Parser instance

opts - The hash of options for this Loader

:argv - The commandline options passed to the Parser.
other options as per the Loader class.


60
61
62
63
# File 'lib/yacl/define/cli/parser.rb', line 60

def initialize( opts = {} )
  super
  @argv = opts[:argv] || []
end

Class Method Details

Public: Set or retrieve the banner text.

text - A String to be displayed as part of the help text.

Returns the value set, or the default value if one is not set.



41
42
43
44
# File 'lib/yacl/define/cli/parser.rb', line 41

def self.banner( *args )
  @banner = args.first unless args.empty?
  @banner ||= "Usage  :  #{File.basename($0)} [options]+\nOptions:"
end

.opt(*args) ⇒ Object

Public: Define a commandline option for this Parser. Using ‘opt’ over options will dynamically create a child class of Options for use by this class



49
50
51
52
# File 'lib/yacl/define/cli/parser.rb', line 49

def self.opt( *args )
  options( Class.new( Yacl::Define::Cli::Options ) )
  options.opt( *args )
end

.options(*args) ⇒ Object

Public: Define the options Class that is be used by this Parser, or return the existin Class if it is already defined.

klass - A Class that is a child class of Cli::Options.

Returns the current options Class.



31
32
33
34
# File 'lib/yacl/define/cli/parser.rb', line 31

def self.options( *args )
  @options_klass ||= args.first unless args.empty?
  return @options_klass
end

Instance Method Details

Public: Retrive the class level banner text.

Returns the String banner text



68
69
70
# File 'lib/yacl/define/cli/parser.rb', line 68

def banner
  self.class.banner
end

#propertiesObject

Public: Return the Properties instance that is created by parsing the commandline options.

Nil values from possible defaults from the commandline parser are filtered out before being merged onto the options.

Returns a Properties instance.



79
80
81
82
83
84
85
86
# File 'lib/yacl/define/cli/parser.rb', line 79

def properties
  h = {}
  parse( @argv ).each do |k,v|
    h[k] = v unless v.nil?
  end
  o = options_klass.new( h )
  o.properties
end