Module: Voke

Defined in:
lib/voke.rb,
lib/voke/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



8
9
10
# File 'lib/voke.rb', line 8

def self.extended(klass)
  klass.extend(ClassMethods)
end

.included(klass) ⇒ Object



4
5
6
# File 'lib/voke.rb', line 4

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#help(command, *args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/voke.rb', line 12

def help(command, *args)
  command = command.to_sym rescue nil

  if command and respond_to?(command)
    methods = [ command ]
  else
    methods = self.class.public_instance_methods(false)
  end

  methods.each do |method_name|
    method = self.method(method_name)
    parameters = method.parameters

    # remove the options parameter
    parameters.pop

    parts = parameters.collect do |type, name|
      case type
      when :req
        name
      when :opt
        "[#{ name }]"
      when :part
        "#{ name }*"
      end
    end
  end
end

#voke(*args) ⇒ Object



41
42
43
44
45
46
# File 'lib/voke.rb', line 41

def voke(*args)
  args = ARGV if args.empty?

  command, arguments, options = voke_parse(*args)
  voke_call(command, arguments, options)
end

#voke_call(command, arguments, options) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/voke.rb', line 48

def voke_call(command, arguments, options)
  begin
    method = method(command.to_sym)
  rescue
    return help(command, *arguments, options)
  end

  method.call(*arguments, options)
end

#voke_parse(*args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/voke.rb', line 58

def voke_parse(*args)
  command = args.shift
  arguments = Array.new
  options = Hash.new

  args.each do |arg|
    if arg =~ /^--(\w+)(?:=(.*))?$/
      key = $1.to_sym

      value = true
      value = voke_parse_value($2) if $2

      options[key] = value
    else
      key = nil
      value = voke_parse_value(arg)

      arguments << value
    end
  end

  [ command, arguments, options ]
end

#voke_parse_value(value) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/voke.rb', line 82

def voke_parse_value(value)
  case value
  when "", "nil"
    nil
  when "true"
    true
  when "false"
    false
  when /^-?\d+$/
    value.to_i
  when /^-?\d*\.\d*$/
    value.to_f
  when /^['"](.+)['"]$/
    $1
  when /^(.*),(.*)$/
    value = value.split(',')
    value.collect { |v| voke_parse_value(v) }
  else
    value
  end
end