Module: Cyclops::OptionParserExtension

Defined in:
lib/cyclops/option_parser_extension.rb

Constant Summary collapse

OPTION_RE =
/(\w+)__(\w+)(\?)?\z/
SWITCH_RE =
/(\w+)(\?)?\z/
KEY_POOL =
('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cliObject

Returns the value of attribute cli.



31
32
33
# File 'lib/cyclops/option_parser_extension.rb', line 31

def cli
  @cli
end

Instance Method Details

#keysObject



39
40
41
# File 'lib/cyclops/option_parser_extension.rb', line 39

def keys
  { used: keys = top.short.keys, free: KEY_POOL - keys }
end

#option(name, *args, &block) ⇒ Object

Delegates to #on with some convenience shortcuts.

If name is a Symbol, installs both long and short options. If the first element of args is a Symbol, this is installed as the short option, otherwise the first character of name is installed as the short option.

If name is a String, installs only the long option.

If name contains an argument name, separated by double underscore, additionally sets the CLI’s name option (as a Symbol) to the provided value and calls the optional block with that value. If the argument name ends with a question mark, the value is marked as optional.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cyclops/option_parser_extension.rb', line 61

def option(name, *args, &block)
  sym = name.is_a?(Symbol)

  if name =~ OPTION_RE
    name, arg, opt = $1, $2, !!$3
    __on_opts(name, args, sym)

    arg = "[#{arg}]" if opt
    args.grep(/\A--/).first << " #{arg}"

    on(*args) { |value|
      cli.options[name.to_sym] = value
      yield value if block_given?
    }
  else
    on(*__on_opts(name, args, sym), &block)
  end
end

#separator(string = '') ⇒ Object



43
44
45
# File 'lib/cyclops/option_parser_extension.rb', line 43

def separator(string = '')
  super
end

#switch(name, *args) ⇒ Object

Delegates to #on with some convenience shortcuts.

If name is a Symbol, installs both long and short options. If the first element of args is a Symbol, this is installed as the short option, otherwise the first character of name is installed as the short option.

If name is a String, installs only the long option.

Sets the CLI’s name option (as a Symbol) to true and calls the optional block (with no argument).

If name ends with a question mark, installs only the long option and sets the CLI’s name option (as a Symbol) to either true or false, depending on whether --name or --no-name was given on the command line.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cyclops/option_parser_extension.rb', line 96

def switch(name, *args)
  sym = name.is_a?(Symbol)

  name, opt = $1, !!$2 if name =~ SWITCH_RE

  if opt
    __on_opts(name, args, false)
    args.first.insert(2, '[no-]')

    on(*args) { |value|
      cli.options[name.to_sym] = value
      yield if block_given?
    }
  else
    on(*__on_opts(name, args, sym)) {
      cli.options[name.to_sym] = true
      yield if block_given?
    }
  end
end