configliere 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
data/configliere.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{configliere}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mrflip"]
12
- s.date = %q{2011-04-02}
12
+ s.date = %q{2011-03-01}
13
13
  s.default_executable = %q{configliere}
14
14
  s.description = %q{ You've got a script. It's got some settings. Some settings are for this module, some are for that module. Most of them don't change. Except on your laptop, where the paths are different. Or when you're in production mode. Or when you're testing from the command line.
15
15
 
@@ -39,6 +39,7 @@ Configliere manage settings from many sources: static constants, simple config f
39
39
  "examples/simple_script.yaml",
40
40
  "lib/configliere.rb",
41
41
  "lib/configliere/commandline.rb",
42
+ "lib/configliere/commands.rb",
42
43
  "lib/configliere/config_block.rb",
43
44
  "lib/configliere/config_file.rb",
44
45
  "lib/configliere/core_ext.rb",
@@ -49,9 +50,9 @@ Configliere manage settings from many sources: static constants, simple config f
49
50
  "lib/configliere/define.rb",
50
51
  "lib/configliere/encrypted.rb",
51
52
  "lib/configliere/env_var.rb",
52
- "lib/configliere/git_style_binaries.rb",
53
53
  "lib/configliere/param.rb",
54
54
  "spec/configliere/commandline_spec.rb",
55
+ "spec/configliere/commands_spec.rb",
55
56
  "spec/configliere/config_block_spec.rb",
56
57
  "spec/configliere/config_file_spec.rb",
57
58
  "spec/configliere/core_ext/hash_spec.rb",
@@ -75,6 +76,7 @@ Configliere manage settings from many sources: static constants, simple config f
75
76
  "examples/env_var_script.rb",
76
77
  "examples/simple_script.rb",
77
78
  "spec/configliere/commandline_spec.rb",
79
+ "spec/configliere/commands_spec.rb",
78
80
  "spec/configliere/config_block_spec.rb",
79
81
  "spec/configliere/config_file_spec.rb",
80
82
  "spec/configliere/core_ext/hash_spec.rb",
data/lib/configliere.rb CHANGED
@@ -10,11 +10,16 @@ module Configliere
10
10
  Configliere::Param.new(*args, &block)
11
11
  end
12
12
 
13
- ALL_MIXINS = [:define, :config_file, :commandline, :encrypted, :env_var, :config_block, :git_style_binaries]
13
+ ALL_MIXINS = [:define, :config_file, :commandline, :encrypted, :env_var, :config_block, :commands]
14
14
  def self.use *mixins
15
15
  mixins = ALL_MIXINS if mixins.include?(:all) || mixins.empty?
16
16
  mixins.each do |mixin|
17
- require "configliere/#{mixin}"
17
+ # backwards compatibility
18
+ if mixin.to_sym == :git_style_binaries
19
+ require "configliere/commands"
20
+ else
21
+ require "configliere/#{mixin}"
22
+ end
18
23
  end
19
24
  end
20
25
 
@@ -6,6 +6,7 @@ module Configliere
6
6
  #
7
7
  module Commandline
8
8
  attr_accessor :rest
9
+ alias_method :argv, :rest
9
10
 
10
11
  # Processing to reconcile all options
11
12
  #
@@ -37,27 +38,40 @@ module Configliere
37
38
  until args.empty? do
38
39
  arg = args.shift
39
40
  case
41
+ # end of options parsing
40
42
  when arg == '--'
41
43
  self.rest += args
42
44
  break
45
+ # --param=val
43
46
  when arg =~ /\A--([\w\-\.]+)(?:=(.*))?\z/
44
47
  param, val = [$1, $2]
45
48
  param.gsub!(/\-/, '.') # translate --scoped-flag to --scoped.flag
46
49
  param = param.to_sym unless (param =~ /\./) # symbolize non-scoped keys
47
- if val == nil then val = true # --flag option on its own means 'set that option'
48
- elsif val == '' then val = nil end # --flag='' the explicit empty string means nil
49
- self[param] = val
50
+ self[param] = parse_value(val)
51
+ # -abc
50
52
  when arg =~ /\A-(\w+)\z/
51
53
  $1.each_char do |flag|
52
54
  param = param_with_flag(flag)
53
55
  self[param] = true if param
54
56
  end
57
+ # -a=val
58
+ # when arg =~ /\A-(\w)=(.*)\z/
59
+ # param, val = param_with_flag($1), $2
60
+ # self[param] = parse_value(val) if param
55
61
  else
56
62
  self.rest << arg
57
63
  end
58
64
  end
59
65
  end
60
66
 
67
+ def parse_value val
68
+ case
69
+ when val == nil then true # --flag option on its own means 'set that option'
70
+ when val == '' then nil # --flag='' the explicit empty string means nil
71
+ else val # else just return the value
72
+ end
73
+ end
74
+
61
75
  # Configliere internal params
62
76
  def define_special_params
63
77
  Settings.define :encrypt_pass, :description => "Passphrase to extract encrypted config params.", :internal => true
@@ -123,33 +137,48 @@ module Configliere
123
137
  @complain_about_bad_flags = true
124
138
  end
125
139
 
126
- # The contents of the help message.
127
- # Lists the usage as well as any defined parameters and environment variables
128
- def help
129
- help_str = [ usage ]
130
- if respond_to?(:descriptions)
131
- help_str += ["\nParams"]
132
- ordered_params_and_descs = descriptions.sort_by{|p,d| p.to_s }
133
- param_help_strings = ordered_params_and_descs.map do |param, desc|
134
- if flag = param_definitions[param][:flag]
135
- " -%s, --%-21s %s" % [flag.to_s.first, param.to_s + ':', desc]
136
- else
137
- " --%-25s %s" % [param.to_s + ':', desc]
138
- end
140
+ # Return the params for which a line should be printed giving
141
+ # their usage.
142
+ def params_with_command_line_help
143
+ descriptions.reject{ |param, desc| param_definitions[param][:no_command_line_help] }.sort_by{ |param, desc| param.to_s }
144
+ end
145
+
146
+ # Help on the flags used.
147
+ def flags_help
148
+ help = ["\nParams"]
149
+ help += params_with_command_line_help.map do |param, desc|
150
+ if flag = param_definitions[param][:flag]
151
+ " -%s, --%-21s %s" % [flag.to_s.first, param.to_s, desc]
152
+ else
153
+ " --%-25s %s" % [param.to_s + ':', desc]
139
154
  end
140
- help_str += param_help_strings
141
155
  end
142
- 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)
143
- help_str.join("\n")
156
+ end
157
+
158
+ # Return the params for which a line should be printing giving
159
+ # their dependence on environment variables.
160
+ def params_with_env_help
161
+ definitions_for(:env_var).reject { |param, desc| param_definitions[param][:no_help] || param_definitions[param][:no_env_help] }
162
+ end
163
+
164
+ # Help on environment variables.
165
+ def env_var_help
166
+ [ "\nEnvironment Variables can be used to set:"] + params_with_env_help.map{ |param, env| " %-27s %s"%[env.to_s, param]}
144
167
  end
145
168
 
146
169
  # Output the help message to $stderr, along with an optional extra message appended.
147
- def dump_help extra_msg=nil
148
- $stderr.puts help
170
+ def dump_basic_help extra_msg=nil
171
+ $stderr.puts [:flags_help, :env_var_help].map { |help| send(help) }.flatten.compact.join("\n") if respond_to?(:descriptions)
149
172
  $stderr.puts "\n\n"+extra_msg unless extra_msg.blank?
150
173
  $stderr.puts ''
151
174
  end
152
175
 
176
+ def dump_help str=nil
177
+ dump_basic_help
178
+ dump_command_help if respond_to?(:dump_command_help)
179
+ puts str if str
180
+ end
181
+
153
182
  def raw_script_name
154
183
  File.basename($0)
155
184
  end
@@ -164,6 +193,7 @@ module Configliere
164
193
  # Ouput the help string if requested
165
194
  def dump_help_if_requested
166
195
  return unless self[:help]
196
+ $stderr.puts usage
167
197
  dump_help
168
198
  exit
169
199
  end
@@ -0,0 +1,111 @@
1
+ Configliere.use :commandline
2
+ module Configliere
3
+
4
+ #
5
+ # Command line tool to manage param info
6
+ #
7
+ # To include, specify
8
+ #
9
+ # Configliere.use :commands
10
+ #
11
+ module Commands
12
+
13
+ # The name of the command.
14
+ attr_accessor :command_name
15
+
16
+ # Add a command, along with a description of its predicates and the command itself.
17
+ def define_command cmd, options={}, &block
18
+ command_configuration = Configliere.new
19
+ yield command_configuration if block_given?
20
+ commands[cmd] = options.merge(:config => command_configuration)
21
+ end
22
+
23
+ # Define a help command.
24
+ def define_help_command!
25
+ define_command :help, :description => "Print detailed help on each command"
26
+ end
27
+
28
+ # Are there any commands that have been defined?
29
+ def commands?
30
+ (! commands.empty?)
31
+ end
32
+
33
+ # Is +cmd+ the name of a known command?
34
+ def command? cmd
35
+ return false if cmd.blank?
36
+ commands.include?(cmd) || commands.include?(cmd.to_s)
37
+ end
38
+
39
+ def commands
40
+ @commands ||= Sash.new
41
+ end
42
+
43
+ def command
44
+ command_name && commands[command_name]
45
+ end
46
+
47
+ # The Param object for the command
48
+ def command_settings
49
+ command && command[:config]
50
+ end
51
+
52
+ def resolve!
53
+ super()
54
+ commands.each_value do |command|
55
+ command[:config].resolve!
56
+ end
57
+ end
58
+
59
+ #
60
+ # Parse the command-line args into the params hash.
61
+ #
62
+ # '--happy_flag' produces :happy_flag => true in the params hash
63
+ # '--foo=foo_val' produces :foo => 'foo_val' in the params hash.
64
+ # '--' Stop parsing; all remaining args are piled into :rest
65
+ #
66
+ # self.rest contains all arguments that don't start with a '--'
67
+ # and all args following the '--' sentinel if any.
68
+ #
69
+ def process_argv!
70
+ super()
71
+ if raw_script_name =~ /(\w+)-([\w\-]+)/
72
+ self.command_name = $2
73
+ else
74
+ self.command_name = rest.shift if command?(rest.first)
75
+ end
76
+ end
77
+
78
+ # The script name without command appendix if any: For $0 equal to any of
79
+ # 'git', 'git-reset', or 'git-cherry-pick', base_script_name is 'git'
80
+ #
81
+ def base_script_name
82
+ raw_script_name.gsub(/-.*/, '')
83
+ end
84
+
85
+ # Usage line
86
+ def usage
87
+ %Q{usage: #{base_script_name} command [...--param=val...]}
88
+ end
89
+
90
+ # Return help on commands.
91
+ def dump_command_help
92
+ help = ["Available commands"]
93
+ help += commands.keys.map(&:to_s).sort.map do |key|
94
+ " %-27s %s" % [key.to_s, commands[key][:description]] unless commands[key][:no_help]
95
+ end
96
+ help += ["\nRun `#{base_script_name} help COMMAND' for more help on COMMAND"] if command?(:help)
97
+ $stderr.puts help.join("\n")
98
+ end
99
+
100
+ end
101
+
102
+ Param.class_eval do
103
+ # include command syntax methods in chain. Since commandline is required
104
+ # first at the top of this file, Commands methods sit below
105
+ # Commandline methods in the superclass chain.
106
+ include Commands
107
+ end
108
+ end
109
+
110
+
111
+ puts "Loaded #{__FILE__}"
@@ -13,16 +13,7 @@ module Configliere
13
13
  # Load params from disk.
14
14
  # * file is in YAML format, as a hash of handle => param_hash pairs
15
15
  # * filename defaults to Configliere::DEFAULT_CONFIG_FILE (~/.configliere, probably)
16
- #
17
- # @option [String] :env
18
- # If an :env option is given, only the indicated subhash is merged. This
19
- # lets you for example specify production / environment / test settings
20
- #
21
- # @example
22
- # # Read from config/apey_eye.yaml and use settings appropriate for development/staging/production
23
- # Settings.read(root_path('config/apey_eye.yaml'), :env => (ENV['RACK_ENV'] || 'production'))
24
- #
25
- def read handle, options={}
16
+ def read handle
26
17
  filename = filename_for_handle(handle)
27
18
  begin
28
19
  params = YAML.load(File.open(filename)) || {}
@@ -31,10 +22,6 @@ module Configliere
31
22
  params = {}
32
23
  end
33
24
  params = params[handle] if handle.is_a?(Symbol)
34
- # Extract the :env (production/development/etc)
35
- if options[:env]
36
- params = params[options[:env]]
37
- end
38
25
  deep_merge! params
39
26
  end
40
27
 
@@ -150,6 +150,7 @@ protected
150
150
  value
151
151
  end
152
152
  end
153
+
153
154
  end
154
155
 
155
156
 
@@ -55,7 +55,7 @@ module Configliere
55
55
 
56
56
  # All described params with their descriptions
57
57
  def descriptions
58
- definitions_for(:description) # .reject{|param, desc| param_definitions[param][:hide_help] }
58
+ definitions_for(:description).reject{ |param, desc| param_definitions[param][:no_help] }
59
59
  end
60
60
 
61
61
  # List of params that have descriptions
@@ -106,8 +106,9 @@ module Configliere
106
106
  when (type == Symbol) then val = val.to_s.to_sym rescue nil
107
107
  when ((val.to_s == 'now') && (type == Date)) then val = Date.today
108
108
  when ((val.to_s == 'now') && (type == DateTime)) then val = DateTime.now
109
- when (type == Date) then val = Date.parse(val) rescue nil
110
- when (type == DateTime) then val = DateTime.parse(val) rescue nil
109
+ when (type == Date) then val = Date.parse(val) rescue nil
110
+ when (type == DateTime) then val = DateTime.parse(val) rescue nil
111
+ when (type == Regexp) then val = Regexp.new(val) rescue nil
111
112
  else # nothing
112
113
  end
113
114
  self[param] = val
@@ -135,7 +136,7 @@ module Configliere
135
136
  required_params.each do |param|
136
137
  missing << param if self[param].nil?
137
138
  end
138
- raise "Missing values for:\n #{missing.map{|s| " --" + s.to_s + " (" + description_for(s) + ") " }.sort.join("\n")}" if (! missing.empty?)
139
+ raise "Missing values for:\n #{missing.map{|s| " --" + s.to_s + (description_for(s) ? (" (" + description_for(s).to_s + ") ") : '') }.sort.join("\n")}" if (! missing.empty?)
139
140
  end
140
141
 
141
142
  # all params with a value for the definable aspect
@@ -18,11 +18,7 @@ module Configliere
18
18
  end
19
19
  end
20
20
  end
21
-
22
- def params_from_env_vars
23
- definitions_for(:env_var)
24
- end
25
-
21
+
26
22
  protected
27
23
  def adopt_env_var! param, env
28
24
  env = env.to_s
@@ -1,5 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'configliere/commandline'
2
+ Configliere.use :commandline
3
3
 
4
4
  describe "Configliere::Commandline" do
5
5
 
@@ -83,6 +83,13 @@ describe "Configliere::Commandline" do
83
83
  @config.should == { :param_1 => true, :cat => true, :foo => nil}
84
84
  end
85
85
 
86
+ # it 'should parse a single-letter flag with a value' do
87
+ # ::ARGV.replace ['-p=new_val', '-c']
88
+ # @config.resolve!
89
+ # @config.rest.should == []
90
+ # @config.should == { :param_1 => 'new_val', :cat => true, :foo => nil }
91
+ # end
92
+
86
93
  it 'should not complain about bad single-letter flags by default' do
87
94
  ::ARGV.replace ['-pcz']
88
95
  @config.resolve!
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ Configliere.use :commands
3
+
4
+ describe "Configliere::Commands" do
5
+
6
+ after do
7
+ ::ARGV.replace []
8
+ end
9
+
10
+ describe "when no commands are defined" do
11
+ before do
12
+ @config = Configliere::Param.new
13
+ end
14
+
15
+ it "should know that no commands are defined" do
16
+ @config.commands?.should be_false
17
+ end
18
+
19
+ it "should not shift the ARGV when resolving" do
20
+ ::ARGV.replace ['not_command_but_arg', 'another_arg']
21
+ @config.resolve!
22
+ @config.rest.should == ['not_command_but_arg', 'another_arg']
23
+ @config.command.should be_nil
24
+ end
25
+
26
+ it "should still recognize a git-style binary command" do
27
+ ::ARGV.replace ['not_command_but_arg', 'another_arg']
28
+ File.should_receive(:basename).and_return('prog-subcommand')
29
+ @config.resolve!
30
+ @config.rest.should == ['not_command_but_arg', 'another_arg']
31
+ @config.command_name.should == 'subcommand'
32
+ @config.command.should be_nil
33
+ end
34
+ end
35
+
36
+ describe "a simple command" do
37
+ before do
38
+ @config = Configliere::Param.new :param => 'val 1'
39
+ @config.define_command "the_command", :description => "foobar"
40
+ end
41
+
42
+ it "should continue to parse flags when the command is given" do
43
+ ::ARGV.replace ['the_command', '--param=wuzz', 'an_arg']
44
+ @config.resolve!
45
+ @config.should == { :param => 'wuzz' }
46
+ end
47
+
48
+ it "should continue to set args when the command is given" do
49
+ ::ARGV.replace ['the_command', '--param=wuzz', 'an_arg']
50
+ @config.resolve!
51
+ @config.rest.should == ['an_arg']
52
+ end
53
+
54
+ it "should recognize the command when given" do
55
+ ::ARGV.replace ['the_command', '--param=wuzz', 'an_arg']
56
+ @config.resolve!
57
+ @config.command_name.should == 'the_command'
58
+ end
59
+
60
+ it "should recognize when the command is not given" do
61
+ ::ARGV.replace ['bogus_command', '--param=wuzz', 'an_arg']
62
+ @config.resolve!
63
+ @config.rest.should == ['bogus_command', 'an_arg']
64
+ @config.command_name.should be_nil
65
+ end
66
+ end
67
+
68
+ describe "a complex command" do
69
+ before do
70
+ @config = Configliere::Param.new :outer_param => 'val 1'
71
+ @config.define_command "the_command", :description => "the command" do |command|
72
+ command.define :inner_param, :description => "inside"
73
+ end
74
+ end
75
+
76
+ it "should still recognize the outer param and the args" do
77
+ ::ARGV.replace ['the_command', '--outer_param=wuzz', 'an_arg', '--inner_param=buzz']
78
+ @config.resolve!
79
+ @config.rest.should == ['an_arg']
80
+ @config.command_name.should == 'the_command'
81
+ @config[:outer_param].should == 'wuzz'
82
+ end
83
+
84
+ it "should recognize the inner param" do
85
+ ::ARGV.replace ['the_command', '--outer_param=wuzz', 'an_arg', '--inner_param=buzz']
86
+ @config.resolve!
87
+ @config[:inner_param].should == 'buzz'
88
+ @config.command[:config][:inner_param].should == 'buzz'
89
+ end
90
+
91
+ end
92
+ end
93
+
@@ -9,7 +9,8 @@ describe "Configliere::ConfigBlock" do
9
9
  describe 'resolving' do
10
10
  it 'runs blocks' do
11
11
  @block_watcher = 'watcher'
12
- @block_watcher.should_receive(:fnord).with(@config)
12
+ # @block_watcher.should_receive(:fnord).with(@config)
13
+ @block_watcher.should_receive(:fnord)
13
14
  @config.finally{|arg| @block_watcher.fnord(arg) }
14
15
  @config.resolve!
15
16
  end
@@ -5,11 +5,10 @@ end
5
5
 
6
6
  describe Sash do
7
7
  before(:each) do
8
- @hash = { "str_key" => "strk_val", :sym_key => "symk_val" }
8
+ @hash = { "str_key" => "strk_val", :sym_key => "symk_val"}
9
9
  @sub = AwesomeHash.new("str_key" => "strk_val", :sym_key => "symk_val")
10
10
  end
11
11
 
12
-
13
12
  describe "#deep_merge!" do
14
13
  before do
15
14
  @sash = Sash.new :hsh1 => { :hsh2 => { :key3 => "val3" }, :key2 => "val2" }
@@ -59,7 +58,7 @@ describe Sash do
59
58
  sash[:sym_key][:sym_key].should == "symk_val"
60
59
  end
61
60
 
62
- it 'doesn not convert Hash subclass values into Sashes' do
61
+ it 'does not convert Hash subclass values into Sashes' do
63
62
  sash = Sash.new :sub => @sub
64
63
  sash[:sub].should be_an_instance_of(AwesomeHash)
65
64
  end
@@ -92,14 +92,14 @@ describe "Configliere::Define" do
92
92
  end
93
93
  it 'counts nil-but-set values as missing' do
94
94
  @config.defaults :param_1 => true, :param_2 => nil
95
- lambda{ @config.validate! }.should raise_error("Missing values for param_2")
95
+ lambda{ @config.validate! }.should raise_error(RuntimeError)
96
96
  end
97
97
  it 'counts never-set values as missing' do
98
- lambda{ @config.validate! }.should raise_error("Missing values for param_1, param_2")
98
+ lambda{ @config.validate! }.should raise_error(RuntimeError)
99
99
  end
100
100
  it 'lists all missing values when it raises' do
101
101
  Configliere.use :define
102
- lambda{ @config.validate! }.should raise_error("Missing values for param_1, param_2")
102
+ lambda{ @config.validate! }.should raise_error(RuntimeError)
103
103
  end
104
104
  end
105
105
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configliere
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 2
8
8
  - 3
9
- version: 0.2.3
9
+ - 0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - mrflip
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-04-02 00:00:00 -05:00
18
+ date: 2011-03-01 00:00:00 -06:00
18
19
  default_executable: configliere
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - ">="
27
28
  - !ruby/object:Gem::Version
29
+ hash: 13
28
30
  segments:
29
31
  - 1
30
32
  - 2
@@ -40,6 +42,7 @@ dependencies:
40
42
  requirements:
41
43
  - - ">="
42
44
  - !ruby/object:Gem::Version
45
+ hash: 3
43
46
  segments:
44
47
  - 0
45
48
  version: "0"
@@ -71,6 +74,7 @@ files:
71
74
  - examples/simple_script.yaml
72
75
  - lib/configliere.rb
73
76
  - lib/configliere/commandline.rb
77
+ - lib/configliere/commands.rb
74
78
  - lib/configliere/config_block.rb
75
79
  - lib/configliere/config_file.rb
76
80
  - lib/configliere/core_ext.rb
@@ -81,9 +85,9 @@ files:
81
85
  - lib/configliere/define.rb
82
86
  - lib/configliere/encrypted.rb
83
87
  - lib/configliere/env_var.rb
84
- - lib/configliere/git_style_binaries.rb
85
88
  - lib/configliere/param.rb
86
89
  - spec/configliere/commandline_spec.rb
90
+ - spec/configliere/commands_spec.rb
87
91
  - spec/configliere/config_block_spec.rb
88
92
  - spec/configliere/config_file_spec.rb
89
93
  - spec/configliere/core_ext/hash_spec.rb
@@ -110,6 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
114
  requirements:
111
115
  - - ">="
112
116
  - !ruby/object:Gem::Version
117
+ hash: 3
113
118
  segments:
114
119
  - 0
115
120
  version: "0"
@@ -118,6 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
123
  requirements:
119
124
  - - ">="
120
125
  - !ruby/object:Gem::Version
126
+ hash: 3
121
127
  segments:
122
128
  - 0
123
129
  version: "0"
@@ -134,6 +140,7 @@ test_files:
134
140
  - examples/env_var_script.rb
135
141
  - examples/simple_script.rb
136
142
  - spec/configliere/commandline_spec.rb
143
+ - spec/configliere/commands_spec.rb
137
144
  - spec/configliere/config_block_spec.rb
138
145
  - spec/configliere/config_file_spec.rb
139
146
  - spec/configliere/core_ext/hash_spec.rb
@@ -1,74 +0,0 @@
1
- require 'configliere/commandline'
2
- module Configliere
3
-
4
- # If your script uses the 'script_name verb [...params...]'
5
- # pattern, list the commands here:
6
- Configliere::COMMANDS = []
7
-
8
- # Add a command, along with a description of its predicates and the command itself.
9
- def self.define_command cmd, preds_desc=nil, desc=nil
10
- Configliere::COMMANDS << [cmd, preds_desc, (desc || "#{cmd} command")]
11
- end
12
-
13
- #
14
- # Command line tool to manage param info
15
- #
16
- # To include, specify
17
- #
18
- # Configliere.use :git_style_binaries
19
- #
20
- module GitStyleBinaries
21
- attr_accessor :command
22
-
23
- #
24
- # Parse the command-line args into the params hash.
25
- #
26
- # '--happy_flag' produces :happy_flag => true in the params hash
27
- # '--foo=foo_val' produces :foo => 'foo_val' in the params hash.
28
- # '--' Stop parsing; all remaining args are piled into :rest
29
- #
30
- # self.rest contains all arguments that don't start with a '--'
31
- # and all args following the '--' sentinel if any.
32
- #
33
- def process_argv!
34
- super
35
- if raw_script_name =~ /(\w+)-([\w\-]+)/
36
- self.command = $2
37
- else
38
- self.command = rest.shift
39
- end
40
- end
41
-
42
- # The script name without command appendix if any: For $0 equal to any of
43
- # 'git', 'git-reset', or 'git-cherry-pick', base_script_name is 'git'
44
- #
45
- def base_script_name
46
- raw_script_name.gsub(/-.*/, '')
47
- end
48
-
49
- # The contents of the help message. Dumps the standard commandline help
50
- # message, and then lists the commands with their description.
51
- def help
52
- help_str = super()
53
- help_str << "\n\nCommands:\n"
54
- COMMANDS.map do |cmd, cmd_params, desc|
55
- cmd_template = " %-49s" % [base_script_name, cmd, cmd_params].join(" ")
56
- cmd_template += " :: " + desc if desc
57
- help_str << cmd_template+"\n"
58
- end
59
- help_str
60
- end
61
-
62
- # Usage line
63
- def usage
64
- %Q{usage: #{base_script_name} command [...--param=val...]}
65
- end
66
- end
67
-
68
- Param.class_eval do
69
- # include command syntax methods in chain. Since commandline is required
70
- # first at the top of this file, GitStyleBinaries methods sit below
71
- # Commandline methods in the superclass chain.
72
- include GitStyleBinaries
73
- end
74
- end