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
39 def keys
40   { used: keys = top.short.keys, free: KEY_POOL - keys }
41 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
61 def option(name, *args, &block)
62   sym = name.is_a?(Symbol)
63 
64   if name =~ OPTION_RE
65     name, arg, opt = $1, $2, !!$3
66     __on_opts(name, args, sym)
67 
68     arg = "[#{arg}]" if opt
69     args.grep(/\A--/).first << " #{arg}"
70 
71     on(*args) { |value|
72       cli.options[name.to_sym] = value
73       yield value if block_given?
74     }
75   else
76     on(*__on_opts(name, args, sym), &block)
77   end
78 end
separator(string = '') click to toggle source
Calls superclass method
   # File lib/cyclops/option_parser_extension.rb
43 def separator(string = '')
44   super
45 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
 96 def switch(name, *args)
 97   sym = name.is_a?(Symbol)
 98 
 99   name, opt = $1, !!$2 if name =~ SWITCH_RE
100 
101   if opt
102     __on_opts(name, args, false)
103     args.first.insert(2, '[no-]')
104 
105     on(*args) { |value|
106       cli.options[name.to_sym] = value
107       yield if block_given?
108     }
109   else
110     on(*__on_opts(name, args, sym)) {
111       cli.options[name.to_sym] = true
112       yield if block_given?
113     }
114   end
115 end

Private Instance Methods

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