Module: Configliere::Commandline
- Defined in:
- lib/configliere/commandline.rb
Overview
Command line tool to manage param info
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#rest ⇒ Object
Returns the value of attribute rest.
-
#unknown_argvs ⇒ Object
readonly
Returns the value of attribute unknown_argvs.
Instance Method Summary collapse
-
#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
dashed_flag_for each given setting that has a value.
-
#die(str, exit_code = -1) ⇒ Object
die with a warning.
-
#dump_help(str = nil) ⇒ Object
Write the help string to stderr.
-
#help(str = nil) ⇒ Object
The contents of the help message.
-
#process_argv! ⇒ Object
Parse the command-line args into the params hash.
-
#raw_script_name ⇒ Object
the script basename, for recycling into help messages.
-
#resolve! ⇒ Object
Processing to reconcile all options.
-
#usage ⇒ Object
Usage line.
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
11 12 13 |
# File 'lib/configliere/commandline.rb', line 11 def description @description end |
#rest ⇒ Object
Returns the value of attribute rest.
10 11 12 |
# File 'lib/configliere/commandline.rb', line 10 def rest @rest end |
#unknown_argvs ⇒ Object (readonly)
Returns the value of attribute unknown_argvs.
12 13 14 |
# File 'lib/configliere/commandline.rb', line 12 def unknown_argvs @unknown_argvs end |
Instance Method Details
#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
103 104 105 106 107 |
# File 'lib/configliere/commandline.rb', line 103 def dashed_flag_for setting_name, flag_name=nil return unless self[setting_name] flag_name ||= setting_name (self[setting_name] == true ? "--#{flag_name.to_s.gsub(/_/,"-")}" : "--#{flag_name.to_s.gsub(/_/,"-")}=#{self[setting_name]}" ) end |
#dashed_flags(*settings_and_names) ⇒ Object
dashed_flag_for each given setting that has a value
110 111 112 |
# File 'lib/configliere/commandline.rb', line 110 def dashed_flags *settings_and_names settings_and_names.map{|args| dashed_flag_for(*args) }.compact end |
#die(str, exit_code = -1) ⇒ Object
die with a warning
153 154 155 156 |
# File 'lib/configliere/commandline.rb', line 153 def die str, exit_code=-1 dump_help "****\n#{str}\n****" exit exit_code end |
#dump_help(str = nil) ⇒ Object
Write the help string to stderr
120 121 122 |
# File 'lib/configliere/commandline.rb', line 120 def dump_help str=nil warn help(str)+"\n" end |
#help(str = nil) ⇒ Object
The contents of the help message. Lists the usage as well as any defined parameters and environment variables
126 127 128 129 130 131 132 133 134 |
# File 'lib/configliere/commandline.rb', line 126 def help str=nil buf = [] buf << usage buf << "\n"+@description if @description buf << param_lines buf << commands_help if respond_to?(:commands_help) buf << "\n\n"+str if str buf.flatten.compact.join("\n")+"\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.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/configliere/commandline.rb', line 42 def process_argv! args = ARGV.dup self.rest = [] @unknown_argvs = [] until args.empty? do arg = args.shift case # end of options parsing when arg == '--' self.rest += args break # --param=val or --param when arg =~ /\A--([\w\-\.]+)(?:=(.*))?\z/ param, val = [$1, $2] warn "Configliere uses _underscores not dashes for params" if param.include?('-') @unknown_argvs << param.to_sym if (not has_definition?(param)) self[param] = parse_value(val) # -abc when arg =~ /\A-(\w\w+)\z/ $1.each_char do |flag| param = find_param_for_flag(flag) unless param then @unknown_argvs << flag ; next ; end self[param] = true end # -a val when arg =~ /\A-(\w)\z/ flag = find_param_for_flag($1) unless flag then @unknown_argvs << flag ; next ; end if (not args.empty?) && (args.first !~ /\A-/) val = args.shift else val = nil end self[flag] = parse_value(val) # -a=val when arg =~ /\A-(\w)=(.*)\z/ flag, val = [find_param_for_flag($1), $2] unless flag then @unknown_argvs << flag ; next ; end self[flag] = parse_value(val) else self.rest << arg end end @unknown_argvs.uniq! end |
#raw_script_name ⇒ Object
the script basename, for recycling into help messages
142 143 144 |
# File 'lib/configliere/commandline.rb', line 142 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 describing all #define'd params and exits
- calls up the resolve! chain.
22 23 24 25 26 27 28 29 30 |
# File 'lib/configliere/commandline.rb', line 22 def resolve! process_argv! if self[:help] dump_help exit(2) end super() self end |
#usage ⇒ Object
Usage line
137 138 139 |
# File 'lib/configliere/commandline.rb', line 137 def usage %Q{usage: #{raw_script_name} [...--param=val...]} end |