module Cyclops::OptionParserExtension

Constants

KEY_POOL
OPTION_RE
SWITCH_RE

Attributes

cli[RW]

Public Instance Methods

keys() click to toggle source
# File lib/cyclops/option_parser_extension.rb, line 39
def keys
  { used: keys = top.short.keys, free: KEY_POOL - keys }
end
option(name, *args) { |value| ... } click to toggle source

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.

# 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 = '') click to toggle source
Calls superclass method
# File lib/cyclops/option_parser_extension.rb, line 43
def separator(string = '')
  super
end
switch(name, *args) { || ... } click to toggle source

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.

# 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

Private Instance Methods

__on_opts(name, args, sym) click to toggle source
# File lib/cyclops/option_parser_extension.rb, line 119
def __on_opts(name, args, sym)
  args.insert(0, "-#{args[0].is_a?(Symbol) ? args.shift : name[0]}") if sym
  args.insert(sym ? 1 : 0, "--#{name.to_s.tr('_', '-')}")
end