Class: Swamp::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, options = {}) ⇒ CLI

Returns a new instance of CLI.



15
16
17
18
19
# File 'lib/swamp/cli.rb', line 15

def initialize(app_name, options = {})
  @app_name = app_name
  @version = options[:version]
  @commands = {}
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



12
13
14
# File 'lib/swamp/cli.rb', line 12

def app_name
  @app_name
end

#commandsObject (readonly)

Returns the value of attribute commands.



11
12
13
# File 'lib/swamp/cli.rb', line 11

def commands
  @commands
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#add(command) ⇒ Object



21
22
23
# File 'lib/swamp/cli.rb', line 21

def add(command)
  @commands[command.name.to_sym] = command
end

#command(name) ⇒ Object



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

def command(name)
  @commands[name.to_sym]
end

#dispatch(argv, *additional_args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/swamp/cli.rb', line 29

def dispatch(argv, *additional_args)
  command_name = argv.shift
  if command_name && command = command(command_name)
    context = Context.new(self)
    context.options = command.parse_options(argv)
    context.args = argv

    command.action.call(context, *additional_args)
  else
    raise Error, "Command not found"
  end
end

#load_from_directory(path) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/swamp/cli.rb', line 42

def load_from_directory(path)
  unless File.directory?(path)
    raise Error, "No commands directory at #{path}"
  end

  Dir[File.join(path, '**', '*.rb')].each do |path|
    CLIDSL.new(self).instance_eval(File.read(path), path)
  end
end