Class: Brut::CLI::Options

Inherits:
Object
  • Object
show all
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

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)
  @parsed_options = 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).

Examples:


# Suppose the user provided `--log-level=debug --dry-run` on the command line
options.log_level               # => "debug"
options.verbose                 # => nil
options.verbose(default: "yes") # => "yes"
options.dry_run?                # => true
options.slow?                   # => false

Parameters:

  • sym (Symbol)

    kebab or snake case value of the option. A question mark should be used if the option is to be coerced to a booealn.

  • args (Array|Hash)

    if the first arg is a Hash with the key default:, this value will be used if sym has no value

Returns:

  • (true|false|nil|Object)

    the value of the option used on the command line, or the default used in default:, or the



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.

Parameters:

  • key (String)

    the key to use. This must be the exact name you used when calling opts.on or when creating the OptionParser for your app or command. Generally, use the #method_missing-provided version instead of this.



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.

Parameters:

  • key (String)

    the key to use. This must be the exact name you used when calling opts.on or when creating the OptionParser for your app or command.

Returns:

  • (Boolean)


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.

Parameters:

  • sym (Symbol)

    the symbol of the option, either in kebab-case or snake_case.

  • default_value (Object)

    the value to return if that option 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_hHash

Returns the parsed options as a Hash

Returns:

  • (Hash)


10
# File 'lib/brut/cli/options.rb', line 10

def to_h = @parsed_options