module Configliere::Commands
Command line tool to manage param info
To include, specify
Settings.use :commands
Attributes
command_name[RW]
The name of the command.
Public Instance Methods
command_info()
click to toggle source
# File lib/configliere/commands.rb, line 33 def command_info commands[command_name] if command_name end
commands()
click to toggle source
# File lib/configliere/commands.rb, line 29 def commands @commands ||= DeepHash.new end
define_command(cmd, options={}) { |command_configuration| ... }
click to toggle source
Add a command, along with a description of its predicates and the command itself.
# 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!()
click to toggle source
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.
Calls superclass method
# 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!()
click to toggle source
Calls superclass method
# 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!( Hash[sub_config.select{|k,v| adoptable.include?(k) }] ) end self end
usage()
click to toggle source
Usage line
# File lib/configliere/commands.rb, line 71 def usage %Q{usage: #{script_base_and_command.first} [command] [...--param=val...]} end
Protected Instance Methods
commands_help()
click to toggle source
Return help on commands.
# File lib/configliere/commands.rb, line 78 def commands_help help = ["\nAvailable commands:"] commands.sort_by(&:to_s).each do |cmd, info| help << (" %-27s %s" % [cmd, info[:description]]) unless info[:internal] info[:config].param_lines[1..-1].each{|line| help << " #{line}" } rescue nil end help << "\nRun `#{script_base_and_command.first} help COMMAND' for more help on COMMAND" if commands.include?(:help) help.flatten.join("\n") end
script_base_and_command()
click to toggle source
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’
# File lib/configliere/commands.rb, line 91 def script_base_and_command raw_script_name.split('-', 2) end