Module: Configliere::Commandline

Defined in:
lib/configliere/commandline.rb

Overview

Command line tool to manage param info

Examples:

Settings.use :commandline

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/configliere/commandline.rb', line 11

def description
  @description
end

#restObject

Returns the value of attribute rest.



10
11
12
# File 'lib/configliere/commandline.rb', line 10

def rest
  @rest
end

#unknown_argvsObject (readonly)

Returns the value of attribute unknown_argvs.



12
13
14
# File 'lib/configliere/commandline.rb', line 12

def unknown_argvs
  @unknown_argvs
end

#usageObject

Usage line



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

def usage
  %Q{usage: #{raw_script_name} [...--param=val...]}
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

Examples:

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


107
108
109
110
111
# File 'lib/configliere/commandline.rb', line 107

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



114
115
116
# File 'lib/configliere/commandline.rb', line 114

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

Examples:

Settings.define :foo, :finally => lambda{ Settings.foo.to_i < 5 or die("too much foo!") }

Parameters:

  • str (String)

    the string to dump out before exiting

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

    UNIX exit code to set, default -1



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

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



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

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



130
131
132
133
134
135
136
137
138
# File 'lib/configliere/commandline.rb', line 130

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.



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
87
88
89
90
# File 'lib/configliere/commandline.rb', line 46

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_nameObject

the script basename, for recycling into help messages



147
148
149
# File 'lib/configliere/commandline.rb', line 147

def raw_script_name
  File.basename($0)
end

#resolve!(print_help_and_exit = true) ⇒ Object

Processing to reconcile all options

  • processes all commandline params
  • calls up the resolve! chain.

  • if the --help param was given, prints out a usage statement describing all #define'd params and exits

If the --help param was given, Will print out a usage statement describing all #define'd params and exit. To avoid this, pass false as the first argument.

Parameters:

  • print_help_and_exit (true, false) (defaults to: true)

    whether to print help and exit if the --help param was given



26
27
28
29
30
31
32
33
34
# File 'lib/configliere/commandline.rb', line 26

def resolve!(print_help_and_exit=true)
  process_argv!
  if print_help_and_exit && self[:help]
    dump_help
    exit(2)
  end
  super()
  self
end