Class: Yacl::Define::Cli::Parser
- 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
"Usage: myapp [options]+"
MyOptions
end
p = MyParser.new( :argv => ARGV )
p.properties #=> a Properties instance
Instance Attribute Summary
Attributes inherited from Loader
Class Method Summary collapse
-
.banner(*args) ⇒ Object
Public: Set or retrieve the banner text.
-
.opt(*args) ⇒ Object
Public: Define a commandline option for this Parser.
-
.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.
Instance Method Summary collapse
-
#banner ⇒ Object
Public: Retrive the class level banner text.
-
#initialize(opts = {}) ⇒ Parser
constructor
Create a new Parser instance.
-
#properties ⇒ Object
Public: Return the Properties instance that is created by parsing the commandline options.
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
.banner(*args) ⇒ Object
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.( *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 ) ( Class.new( Yacl::Define::Cli::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.( *args ) @options_klass ||= args.first unless args.empty? return @options_klass end |
Instance Method Details
#banner ⇒ Object
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 self.class. end |
#properties ⇒ Object
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 = .new( h ) o.properties end |