Class: CLI::Switches

Inherits:
Array
  • Object
show all
Defined in:
lib/cli/switches.rb

Direct Known Subclasses

Options

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSwitches

Returns a new instance of Switches.



2
3
4
5
# File 'lib/cli/switches.rb', line 2

def initialize
	@long = {}
	@short = {}
end

Class Method Details

.is_switch?(arg) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/cli/switches.rb', line 13

def self.is_switch?(arg)
	arg =~ /^-/
end

Instance Method Details

#<<(switch_dsl) ⇒ Object



7
8
9
10
11
# File 'lib/cli/switches.rb', line 7

def <<(switch_dsl)
	super(switch_dsl)
	@long[switch_dsl.name] = switch_dsl
	@short[switch_dsl.short] = switch_dsl if switch_dsl.has_short?
end

#find(arg) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/cli/switches.rb', line 17

def find(arg)
	if arg =~ /^--/
		find_long(arg)
	else
		find_short(arg)
	end
end

#find_long(arg) ⇒ Object



25
26
27
# File 'lib/cli/switches.rb', line 25

def find_long(arg)
	@long[arg.sub(/^--/, '').tr('-', '_').to_sym]
end

#find_short(arg) ⇒ Object



29
30
31
# File 'lib/cli/switches.rb', line 29

def find_short(arg)
	@short[arg.sub(/^-/, '').tr('-', '_').to_sym]
end

#has_long?(switch_dsl) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/cli/switches.rb', line 33

def has_long?(switch_dsl)
	@long.member?(switch_dsl.name)
end

#has_short?(switch_dsl) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/cli/switches.rb', line 37

def has_short?(switch_dsl)
	@short.member?(switch_dsl.short) if switch_dsl.has_short?
end