Module: Configliere::Commandline
- Defined in:
- lib/configliere/commandline.rb
Overview
Command line tool to manage param info
Instance Attribute Summary collapse
-
#rest ⇒ Object
Returns the value of attribute rest.
Instance Method Summary collapse
-
#complain_about_bad_flags! ⇒ Object
Force this params object to complain about bad (single-letter) flags on the command-line.
-
#complain_about_bad_flags? ⇒ Boolean
Complain about bad flags?.
-
#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.
- #dashed_flags(*settings_and_names) ⇒ Object
-
#define_special_params ⇒ Object
Configliere internal params.
-
#die(str, exit_code = -1) ⇒ Object
die with a warning.
-
#dump_help(extra_msg = nil) ⇒ Object
Output the help message to $stderr, along with an optional extra message appended.
-
#help ⇒ Object
The contents of the help message.
-
#normal_params ⇒ Object
All commandline name-value params that aren’t internal to configliere.
-
#param_or_ask(attr, hint = nil) ⇒ Object
Retrieve the given param, or prompt for it.
-
#param_with_flag(flag) ⇒ Object
Retreive the first param defined with the given flag.
-
#process_argv! ⇒ Object
Parse the command-line args into the params hash.
- #raw_script_name ⇒ Object
-
#resolve! ⇒ Object
Processing to reconcile all options.
-
#usage ⇒ Object
Usage line.
Instance Attribute Details
#rest ⇒ Object
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.
122 123 124 |
# File 'lib/configliere/commandline.rb', line 122 def complain_about_bad_flags! @complain_about_bad_flags = true end |
#complain_about_bad_flags? ⇒ Boolean
Complain about bad flags?
116 117 118 |
# File 'lib/configliere/commandline.rb', line 116 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
105 106 107 108 109 |
# File 'lib/configliere/commandline.rb', line 105 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
111 112 113 |
# File 'lib/configliere/commandline.rb', line 111 def dashed_flags *settings_and_names settings_and_names.map{|args| dashed_flag_for(*args) } end |
#define_special_params ⇒ Object
Configliere internal params
62 63 64 |
# File 'lib/configliere/commandline.rb', line 62 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
75 76 77 78 |
# File 'lib/configliere/commandline.rb', line 75 def die str, exit_code=-1 dump_help "****\n#{str}\n****" exit exit_code end |
#dump_help(extra_msg = nil) ⇒ Object
Output the help message to $stderr, along with an optional extra message appended.
147 148 149 150 151 |
# File 'lib/configliere/commandline.rb', line 147 def dump_help extra_msg=nil $stderr.puts help $stderr.puts "\n\n"+extra_msg unless extra_msg.blank? $stderr.puts '' end |
#help ⇒ Object
The contents of the help message. Lists the usage as well as any defined parameters and environment variables
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/configliere/commandline.rb', line 128 def help help_str = [ usage ] if respond_to?(:descriptions) help_str += ["\nParams"] ordered_params_and_descs = descriptions.sort_by{|p,d| p.to_s } param_help_strings = ordered_params_and_descs.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 help_str += param_help_strings end help_str += [ "\nEnvironment Variables can be used to set:", params_from_env_vars.map{|param, env| " %-27s %s"%[env.to_s+':', param]}.join("\n"), ] if respond_to?(:params_from_env_vars) help_str.join("\n") end |
#normal_params ⇒ Object
All commandline name-value params that aren’t internal to configliere
67 68 69 |
# File 'lib/configliere/commandline.rb', line 67 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
81 82 83 84 85 |
# File 'lib/configliere/commandline.rb', line 81 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.
88 89 90 91 92 93 |
# File 'lib/configliere/commandline.rb', line 88 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 |
#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.
34 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 |
# File 'lib/configliere/commandline.rb', line 34 def process_argv! args = ARGV.dup self.rest = [] until args.empty? do arg = args.shift case when arg == '--' self.rest += args break 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 if val == nil then val = true # --flag option on its own means 'set that option' elsif val == '' then val = nil end # --flag='' the explicit empty string means nil self[param] = val when arg =~ /\A-(\w+)\z/ $1.each_char do |flag| param = param_with_flag(flag) self[param] = true if param end else self.rest << arg end end end |
#raw_script_name ⇒ Object
153 154 155 |
# File 'lib/configliere/commandline.rb', line 153 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
:descriptionset with #define) and then exits -
lastly, calls the next method in the resolve! chain.
18 19 20 21 22 |
# File 'lib/configliere/commandline.rb', line 18 def resolve! process_argv! dump_help_if_requested super() end |
#usage ⇒ Object
Usage line
158 159 160 |
# File 'lib/configliere/commandline.rb', line 158 def usage %Q{usage: #{raw_script_name} [...--param=val...]} end |