Module: Configliere::Commands

Defined in:
lib/configliere/commands.rb

Overview

Command line tool to manage param info

To include, specify

Settings.use :commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command_nameObject

The name of the command.



12
13
14
# File 'lib/configliere/commands.rb', line 12

def command_name
  @command_name
end

Instance Method Details

#command_infoObject



33
34
35
# File 'lib/configliere/commands.rb', line 33

def command_info
  commands[command_name] if command_name
end

#commandsObject



29
30
31
# File 'lib/configliere/commands.rb', line 29

def commands
  @commands ||= DeepHash.new
end

#define_command(cmd, options = {}) {|command_configuration| ... } ⇒ Object

Add a command, along with a description of its predicates and the command itself.

Yields:

  • (command_configuration)


19
20
21
22
23
24
25
26
27
# File 'lib/configliere/commands.rb', line 19

def define_command cmd, options={}, &block
  cmd = cmd.to_sym
  command_configuration = Configliere::Param.new
  command_configuration.use :commandline, :env_var
  yield command_configuration if block_given?
  commands[cmd] = options
  commands[cmd][:config] = command_configuration
  commands[cmd]
end

#process_argv!Object

Parse the command-line args into the params hash.

'--happy_flag' produces :happy_flag => true in the params hash '--foo=foo_val' produces :foo => 'foo_val' in the params hash. '--' Stop parsing; all remaining args are piled into :rest

self.rest contains all arguments that don't start with a '--' and all args following the '--' sentinel if any.



60
61
62
63
64
65
66
67
68
# File 'lib/configliere/commands.rb', line 60

def process_argv!
  super()
  base, cmd = script_base_and_command
  if cmd
    self.command_name = cmd.to_sym
  elsif (not rest.empty?) && commands.include?(rest.first.to_sym)
    self.command_name = rest.shift.to_sym
  end
end

#resolve!Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/configliere/commands.rb', line 37

def resolve!
  super()
  commands.each do |cmd, cmd_info|
    cmd_info[:config].resolve!
  end
  if command_name && commands[command_name]
    sub_config = commands[command_name][:config]
    adoptable  = sub_config.send(:definitions).keys
    merge!(sub_config.select{|k,v| adoptable.include?(k) } )
  end
  self
end

#usageObject

Usage line



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

def usage
  %Q{usage: #{script_base_and_command.first} [command] [...--param=val...]}
end