Module: Configliere::Commands

Defined in:
lib/configliere/commands.rb

Overview

Command line tool to manage param info

To include, specify

Configliere.use :commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command_nameObject

The name of the command.



14
15
16
# File 'lib/configliere/commands.rb', line 14

def command_name
  @command_name
end

Instance Method Details

#base_script_nameObject

The script name without command appendix if any: For $0 equal to any of ‘git’, ‘git-reset’, or ‘git-cherry-pick’, base_script_name is ‘git’



81
82
83
# File 'lib/configliere/commands.rb', line 81

def base_script_name
  raw_script_name.gsub(/-.*/, '')
end

#commandObject



43
44
45
# File 'lib/configliere/commands.rb', line 43

def command
  command_name && commands[command_name]
end

#command?(cmd) ⇒ Boolean

Is cmd the name of a known command?

Returns:

  • (Boolean)


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

def command? cmd
  return false if cmd.blank?
  commands.include?(cmd) || commands.include?(cmd.to_s)
end

#command_settingsObject

The Param object for the command



48
49
50
# File 'lib/configliere/commands.rb', line 48

def command_settings
  command && command[:config]
end

#commandsObject



39
40
41
# File 'lib/configliere/commands.rb', line 39

def commands
  @commands ||= Sash.new
end

#commands?Boolean

Are there any commands that have been defined?

Returns:

  • (Boolean)


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

def commands?
  (! commands.empty?)
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)


17
18
19
20
21
# File 'lib/configliere/commands.rb', line 17

def define_command cmd, options={}, &block
  command_configuration = Configliere.new
  yield command_configuration if block_given?
  commands[cmd] = options.merge(:config => command_configuration)
end

#define_help_command!Object

Define a help command.



24
25
26
# File 'lib/configliere/commands.rb', line 24

def define_help_command!
  define_command :help, :description => "Print detailed help on each command"
end

#dump_command_helpObject

Return help on commands.



91
92
93
94
95
96
97
98
# File 'lib/configliere/commands.rb', line 91

def dump_command_help
  help = ["Available commands"]
  help += commands.keys.map(&:to_s).sort.map do |key|
    "  %-27s %s" % [key.to_s, commands[key][:description]] unless commands[key][:no_help]
  end
  help += ["\nRun `#{base_script_name} help COMMAND' for more help on COMMAND"] if command?(:help)
  $stderr.puts help.join("\n")
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.


69
70
71
72
73
74
75
76
# File 'lib/configliere/commands.rb', line 69

def process_argv!
  super()
  if raw_script_name =~ /(\w+)-([\w\-]+)/
    self.command_name = $2
  else
    self.command_name = rest.shift if command?(rest.first)
  end
end

#resolve!Object



52
53
54
55
56
57
# File 'lib/configliere/commands.rb', line 52

def resolve!
  super()
  commands.each_value do |command|
    command[:config].resolve!
  end
end

#usageObject

Usage line



86
87
88
# File 'lib/configliere/commands.rb', line 86

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