Class: Brut::CLI::Options
- Inherits:
-
Object
- Object
- Brut::CLI::Options
- Defined in:
- lib/brut/cli/options.rb
Overview
Wraps parsed command line options to provide a method-like interface instead of simple Hash
acceess. Also allows specifying default values when an option wasn't given on the command line, as well as boolean coercion for switches. Allows accessing options via snake_case in code, even if specified via kebab-case on the command line.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access an options value directly.
-
#[]=(key, value) ⇒ Object
-
#initialize(parsed_options) ⇒ Options
constructor
A new instance of Options.
-
#key?(key) ⇒ Boolean
Check if
key
was provided on the command line. -
#method_missing(sym, *args, &block) ⇒ true|false|nil|Object
Dynamically creates methods for each command-line option.
-
#set_default(sym, default_value) ⇒ Object
Set a default value for an option when #method_missing is used to access it and that flag or switch was not used on the command line.
-
#to_h ⇒ Hash
Returns the parsed options as a
Hash
.
Constructor Details
#initialize(parsed_options) ⇒ Options
Returns a new instance of Options.
3 4 5 6 |
# File 'lib/brut/cli/options.rb', line 3 def initialize() @parsed_options = @defaults = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ true|false|nil|Object
Dynamically creates methods for each command-line option. Note that this doesn't know what options could've been provided, so it
will respond to any method that either takes no arguments or where the only argument is default:
.
preconfigured default, or nil
. If sym
ended in a question mark, true or false will be returned (never nil).
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/brut/cli/options.rb', line 48 def method_missing(sym,*args,&block) boolean = false if sym.to_s =~ /\?$/ sym = sym.to_s[0..-2].to_sym boolean = true end sym_underscore = sym.to_s.gsub(/\-/,"_").to_sym sym_dash = sym.to_s.gsub(/_/,"-").to_sym value = if self.key?(sym_underscore) self[sym_underscore] elsif self.key?(sym_dash) self[sym_dash] elsif args[0].kind_of?(Hash) && args[0].key?(:default) return args[0][:default] elsif @defaults.key?(sym_underscore) @defaults[sym_underscore] elsif @defaults.key?(sym_dash) @defaults[sym_dash] else nil end if boolean !!value else value end end |
Instance Method Details
#[](key) ⇒ Object
Access an options value directly.
14 |
# File 'lib/brut/cli/options.rb', line 14 def [](key) = @parsed_options[key] |
#[]=(key, value) ⇒ Object
16 17 18 |
# File 'lib/brut/cli/options.rb', line 16 def []=(key,value) @parsed_options[key] = value end |
#key?(key) ⇒ Boolean
Check if key
was provided on the command line.
21 |
# File 'lib/brut/cli/options.rb', line 21 def key?(key) = @parsed_options.key?(key) |
#set_default(sym, default_value) ⇒ Object
Set a default value for an option when #method_missing is used to access it and that flag or switch was not used on the command line.
27 28 29 |
# File 'lib/brut/cli/options.rb', line 27 def set_default(sym,default_value) @defaults[sym] = default_value end |
#to_h ⇒ Hash
Returns the parsed options as a Hash
10 |
# File 'lib/brut/cli/options.rb', line 10 def to_h = @parsed_options |