Module: Clinode

Extended by:
Clinode
Included in:
Clinode
Defined in:
lib/clinode.rb,
lib/clinode/ui.rb,
lib/clinode/helper.rb,
lib/clinode/command.rb,
lib/clinode/version.rb

Defined Under Namespace

Modules: UI Classes: Command, Helper

Constant Summary collapse

BasePath =
File.expand_path(File.dirname(__FILE__))
VERSION =
"1.0.4"

Instance Method Summary collapse

Instance Method Details

#activate(args) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/clinode.rb', line 103

def activate(args)
  @@original_args = args.clone
  @options = parse_options(args)
  @debug = @options.delete(:debug)
  Dir[BasePath + '/commands/*.rb'].each do |command|
    load command
  end
  invoke(args.shift, *args)
end

#command(command, options = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/clinode.rb', line 21

def command(command, options = {}, &block)
  command = command.to_s
  debug "Registered `#{command}`"
  descriptions[command] = @next_description if @next_description
  @next_description = nil
  flag_descriptions[command].update @next_flags if @next_flags
  usage_descriptions[command] = @next_usage if @next_usage
  @next_flags = nil
  @next_usage = []
  commands[command] = Command.new(block)
  Array(options[:alias] || options[:aliases]).each do |command_alias|
    commands[command_alias.to_s] = commands[command.to_s]
  end
end

#commandsObject



124
125
126
# File 'lib/clinode.rb', line 124

def commands
  @commands ||= {}
end

#debug(*messages) ⇒ Object



55
56
57
# File 'lib/clinode.rb', line 55

def debug(*messages)
  puts *messages.map { |m| "== #{m}" } if debug?
end

#debug?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/clinode.rb', line 59

def debug?
  !!@debug
end

#desc(str) ⇒ Object



36
37
38
# File 'lib/clinode.rb', line 36

def desc(str)
  @next_description = str
end

#descriptionsObject



63
64
65
# File 'lib/clinode.rb', line 63

def descriptions
  @descriptions ||= {}
end

#find_command(name) ⇒ Object



119
120
121
122
# File 'lib/clinode.rb', line 119

def find_command(name)
  name = name.to_s
  commands[name] || (commands[name] = Command.new(name)) || commands['default']
end

#flag_descriptionsObject



67
68
69
# File 'lib/clinode.rb', line 67

def flag_descriptions
  @flagdescs ||= Hash.new { |h, k| h[k] = {} }
end

#flags(hash) ⇒ Object



40
41
42
43
# File 'lib/clinode.rb', line 40

def flags(hash)
  @next_flags ||= {}
  @next_flags.update hash
end

#helper(command, &block) ⇒ Object



50
51
52
53
# File 'lib/clinode.rb', line 50

def helper(command, &block)
  debug "Helper'd `#{command}`"
  Helper.send :define_method, command, &block
end

#invoke(command, *args) ⇒ Object



113
114
115
116
117
# File 'lib/clinode.rb', line 113

def invoke(command, *args)
  block = find_command(command)
  debug "Invoking `#{command}`"
  block.call(*args)
end

#load(file) ⇒ Object



128
129
130
131
132
# File 'lib/clinode.rb', line 128

def load(file)
  file[0] =~ /^\// ? path = file : path = BasePath + "/commands/#{File.basename(file)}"
  data = File.read(path)
  Clinode.module_eval data, path
end

#optionsObject



75
76
77
# File 'lib/clinode.rb', line 75

def options
  @options
end

#original_argsObject



79
80
81
# File 'lib/clinode.rb', line 79

def original_args
  @@original_args ||= []
end

#parse_options(args) ⇒ Object



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

def parse_options(args)
  idx = 0
  args.clone.inject({}) do |memo, arg|
    case arg
    when /^--(.+?)=(.*)/
      args.delete_at(idx)
      memo.merge($1.to_sym => $2)
    when /^--(.+)/
      args.delete_at(idx)
      memo.merge($1.to_sym => true)
    when "--"
      args.delete_at(idx)
      return memo
    else
      idx += 1
      memo
    end
  end
end

#usage(string) ⇒ Object



45
46
47
48
# File 'lib/clinode.rb', line 45

def usage(string)
  @next_usage ||= []
  @next_usage << string
end

#usage_descriptionsObject



71
72
73
# File 'lib/clinode.rb', line 71

def usage_descriptions
  @usage_descriptions ||= Hash.new { |h, k| h[k] = [] }
end