Module: Configliere::Commandline

Defined in:
lib/configliere/commandline.rb

Overview

Command line tool to manage param info

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#restObject Also known as: argv

Returns the value of attribute rest.



8
9
10
# File 'lib/configliere/commandline.rb', line 8

def rest
  @rest
end

Instance Method Details

#complain_about_bad_flags!Object

Force this params object to complain about bad (single-letter) flags on the command-line.



136
137
138
# File 'lib/configliere/commandline.rb', line 136

def complain_about_bad_flags!
  @complain_about_bad_flags = true
end

#complain_about_bad_flags?Boolean

Complain about bad flags?

Returns:

  • (Boolean)


130
131
132
# File 'lib/configliere/commandline.rb', line 130

def complain_about_bad_flags?
  @complain_about_bad_flags
end

#dashed_flag_for(setting_name, flag_name = nil) ⇒ Object

Returns a flag in dashed form, suitable for recycling into the commandline of an external program. Can specify a specific flag name, otherwise the given setting key is used

Examples:

Settings.dashed_flag_for(:flux_capacitance)
#=> --flux-capacitance=0.7
Settings.dashed_flag_for(:screw_you, :hello_friend)
#=> --hello-friend=true


119
120
121
122
123
# File 'lib/configliere/commandline.rb', line 119

def dashed_flag_for setting_name, flag_name=nil
  return unless Settings[setting_name]
  flag_name ||= setting_name
  (Settings[setting_name] == true ? "--#{flag_name.to_s.gsub(/_/,"-")}" : "--#{flag_name.to_s.gsub(/_/,"-")}=#{Settings[setting_name]}" )
end

#dashed_flags(*settings_and_names) ⇒ Object



125
126
127
# File 'lib/configliere/commandline.rb', line 125

def dashed_flags *settings_and_names
  settings_and_names.map{|args| dashed_flag_for(*args) }
end

#define_special_paramsObject

Configliere internal params



76
77
78
# File 'lib/configliere/commandline.rb', line 76

def define_special_params
  Settings.define :encrypt_pass, :description => "Passphrase to extract encrypted config params.", :internal => true
end

#die(str, exit_code = -1) ⇒ Object

die with a warning

Parameters:

  • str (String)

    the string to dump out before exiting

  • exit_code (Integer) (defaults to: -1)

    UNIX exit code to set, default -1



89
90
91
92
# File 'lib/configliere/commandline.rb', line 89

def die str, exit_code=-1
  dump_help "****\n#{str}\n****"
  exit exit_code
end

#dump_basic_help(extra_msg = nil) ⇒ Object

Output the help message to $stderr, along with an optional extra message appended.



170
171
172
173
174
# File 'lib/configliere/commandline.rb', line 170

def dump_basic_help extra_msg=nil
  $stderr.puts [:flags_help, :env_var_help].map { |help| send(help) }.flatten.compact.join("\n") if respond_to?(:descriptions)
  $stderr.puts "\n\n"+extra_msg unless extra_msg.blank?
  $stderr.puts ''
end

#dump_help(str = nil) ⇒ Object



176
177
178
179
180
# File 'lib/configliere/commandline.rb', line 176

def dump_help str=nil
  dump_basic_help
  dump_command_help if respond_to?(:dump_command_help)
  puts str if str
end

#env_var_helpObject

Help on environment variables.



165
166
167
# File 'lib/configliere/commandline.rb', line 165

def env_var_help
  [ "\nEnvironment Variables can be used to set:"] + params_with_env_help.map{ |param, env| "  %-27s %s"%[env.to_s, param]}
end

#flags_helpObject

Help on the flags used.



147
148
149
150
151
152
153
154
155
156
# File 'lib/configliere/commandline.rb', line 147

def flags_help
  help = ["\nParams"]
  help += params_with_command_line_help.map do |param, desc|
    if flag = param_definitions[param][:flag]
      "  -%s, --%-21s %s" % [flag.to_s.first, param.to_s, desc]
    else
      "  --%-25s %s" % [param.to_s + ':', desc]
    end
  end
end

#normal_paramsObject

All commandline name-value params that aren’t internal to configliere



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

def normal_params
  reject{|param, val| param_definitions[param][:internal] }
end

#param_or_ask(attr, hint = nil) ⇒ Object

Retrieve the given param, or prompt for it



95
96
97
98
99
# File 'lib/configliere/commandline.rb', line 95

def param_or_ask attr, hint=nil
  return self[attr] if include?(attr)
  require 'highline/import'
  self[attr] = ask("#{attr}"+(hint ? " for #{hint}?" : '?'))
end

#param_with_flag(flag) ⇒ Object

Retreive the first param defined with the given flag.

Raises:



102
103
104
105
106
107
# File 'lib/configliere/commandline.rb', line 102

def param_with_flag flag
  params_with(:flag).each do |param|
    return param if param_definitions[param][:flag].to_s == flag.to_s
  end
  raise Configliere::Error.new("Unknown option: -#{flag}") if complain_about_bad_flags?
end

#params_with_command_line_helpObject

Return the params for which a line should be printed giving their usage.



142
143
144
# File 'lib/configliere/commandline.rb', line 142

def params_with_command_line_help
  descriptions.reject{ |param, desc| param_definitions[param][:no_command_line_help] }.sort_by{ |param, desc| param.to_s }
end

#params_with_env_helpObject

Return the params for which a line should be printing giving their dependence on environment variables.



160
161
162
# File 'lib/configliere/commandline.rb', line 160

def params_with_env_help
  definitions_for(:env_var).reject { |param, desc| param_definitions[param][:no_help] || param_definitions[param][:no_env_help] }
end

#parse_value(val) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/configliere/commandline.rb', line 67

def parse_value val
  case
  when val == nil then true            # --flag    option on its own means 'set that option'
  when val == '' then nil             # --flag='' the explicit empty string means nil      
  else val                             # else just return the value
  end
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.


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/configliere/commandline.rb', line 35

def process_argv!
  args = ARGV.dup
  self.rest = []
  until args.empty? do
    arg = args.shift
    case
    # end of options parsing
    when arg == '--'
      self.rest += args
      break
    # --param=val
    when arg =~ /\A--([\w\-\.]+)(?:=(.*))?\z/
      param, val = [$1, $2]
      param.gsub!(/\-/, '.')                        # translate --scoped-flag to --scoped.flag
      param = param.to_sym unless (param =~ /\./)   # symbolize non-scoped keys
      self[param] = parse_value(val)
    # -abc
    when arg =~ /\A-(\w+)\z/
      $1.each_char do |flag|
        param = param_with_flag(flag)
        self[param] = true if param
      end
    # -a=val
    # when arg =~ /\A-(\w)=(.*)\z/
    #   param, val = param_with_flag($1), $2
    #   self[param] = parse_value(val) if param
    else
      self.rest << arg
    end
  end
end

#raw_script_nameObject



182
183
184
# File 'lib/configliere/commandline.rb', line 182

def raw_script_name
  File.basename($0)
end

#resolve!Object

Processing to reconcile all options

Configliere::Commandline’s resolve!:

  • processes all commandline params

  • if the –help param was given, prints out a usage statement (using any :description set with #define) and then exits

  • lastly, calls the next method in the resolve! chain.



19
20
21
22
23
# File 'lib/configliere/commandline.rb', line 19

def resolve!
  process_argv!
  dump_help_if_requested
  super()
end

#usageObject

Usage line



187
188
189
# File 'lib/configliere/commandline.rb', line 187

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