configliere 0.0.9 → 0.1.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.0.9
1
+ 0.1.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{configliere}
8
- s.version = "0.0.9"
8
+ s.version = "0.1.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{2010-05-10}
12
+ s.date = %q{2010-08-10}
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
 
@@ -69,7 +69,7 @@ Configliere manage settings from many sources: static constants, simple config f
69
69
  s.homepage = %q{http://github.com/mrflip/configliere}
70
70
  s.rdoc_options = ["--charset=UTF-8"]
71
71
  s.require_paths = ["lib"]
72
- s.rubygems_version = %q{1.3.6}
72
+ s.rubygems_version = %q{1.3.7}
73
73
  s.summary = %q{Wise, discreet configuration management}
74
74
  s.test_files = [
75
75
  "spec/configliere/commandline_spec.rb",
@@ -94,7 +94,7 @@ Configliere manage settings from many sources: static constants, simple config f
94
94
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
95
95
  s.specification_version = 3
96
96
 
97
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
97
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
98
98
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
99
99
  s.add_development_dependency(%q<yard>, [">= 0"])
100
100
  else
@@ -17,6 +17,10 @@ module Configliere
17
17
  require "configliere/#{mixin}"
18
18
  end
19
19
  end
20
+
21
+ # Base class for Configliere errors.
22
+ Error = Class.new(StandardError)
23
+
20
24
  end
21
25
 
22
26
  # Defines a global config object
@@ -47,6 +47,11 @@ module Configliere
47
47
  if val == nil then val = true # --flag option on its own means 'set that option'
48
48
  elsif val == '' then val = nil end # --flag='' the explicit empty string means nil
49
49
  self[param] = val
50
+ when arg =~ /\A-(\w+)\z/
51
+ $1.each_char do |flag|
52
+ param = param_with_flag(flag)
53
+ self[param] = true if param
54
+ end
50
55
  else
51
56
  self.rest << arg
52
57
  end
@@ -79,11 +84,56 @@ module Configliere
79
84
  self[attr] = ask("#{attr}"+(hint ? " for #{hint}?" : '?'))
80
85
  end
81
86
 
87
+ # Retreive the first param defined with the given flag.
88
+ def param_with_flag flag
89
+ params_with(:flag).each do |param|
90
+ return param if param_definitions[param][:flag].to_s == flag.to_s
91
+ end
92
+ raise Configliere::Error.new("Unknown option: -#{flag}") if complain_about_bad_flags?
93
+ end
94
+
95
+ # Returns a flag in dashed form, suitable for recycling into the commandline
96
+ # of an external program.
97
+ # Can specify a specific flag name, otherwise the given setting key is used
98
+ #
99
+ # @example
100
+ # Settings.dashed_flag_for(:flux_capacitance)
101
+ # #=> --flux-capacitance=0.7
102
+ # Settings.dashed_flag_for(:screw_you, :hello_friend)
103
+ # #=> --hello-friend=true
104
+ #
105
+ def dashed_flag_for setting_name, flag_name=nil
106
+ flag_name ||= setting_name
107
+ "--#{flag_name.to_s.gsub(/_/,"-")}=#{Settings[setting_name]}"
108
+ end
109
+
110
+ # Complain about bad flags?
111
+ def complain_about_bad_flags?
112
+ @complain_about_bad_flags
113
+ end
114
+
115
+ # Force this params object to complain about bad (single-letter)
116
+ # flags on the command-line.
117
+ def complain_about_bad_flags!
118
+ @complain_about_bad_flags = true
119
+ end
120
+
82
121
  # The contents of the help message.
83
122
  # Lists the usage as well as any defined parameters and environment variables
84
123
  def help
85
124
  help_str = [ usage ]
86
- help_str += [ "\nParams:", descriptions.sort_by{|p,d| p.to_s }.map{|param, desc| " --%-25s %s"%[param.to_s+':', desc]}.join("\n"), ] if respond_to?(:descriptions)
125
+ if respond_to?(:descriptions)
126
+ help_str += ["\nParams"]
127
+ ordered_params_and_descs = descriptions.sort_by{|p,d| p.to_s }
128
+ param_help_strings = ordered_params_and_descs.map do |param, desc|
129
+ if flag = param_definitions[param][:flag]
130
+ " -%s, --%-21s %s" % [flag.to_s.first, param.to_s + ':', desc]
131
+ else
132
+ " --%-25s %s" % [param.to_s + ':', desc]
133
+ end
134
+ end
135
+ help_str += param_help_strings
136
+ end
87
137
  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)
88
138
  help_str.join("\n")
89
139
  end
@@ -11,12 +11,15 @@ module Configliere
11
11
  # Settings.define 'delorean.power_source', :description => 'Delorean subsytem supplying power to the Flux Capacitor.'
12
12
  # Settings.define :password, :required => true, :obscure => true
13
13
  #
14
- def define param, definitions={}
14
+ def define param, definitions={}, &block
15
15
  self.param_definitions[param].merge! definitions
16
- self.use(:env_var) if definitions.include?(:env_var)
17
- self.use(:encrypted) if definitions.include?(:encrypted)
16
+ self.use(:env_var) if definitions.include?(:env_var)
17
+ self.use(:encrypted) if definitions.include?(:encrypted)
18
+ self.use(:config_block) if definitions.include?(:finally)
18
19
  self[param] = definitions[:default] if definitions.include?(:default)
19
20
  self.env_vars param => definitions[:env_var] if definitions.include?(:env_var)
21
+ self.finally(&definitions[:finally]) if definitions.include?(:finally)
22
+ self.finally(&block) if block
20
23
  end
21
24
 
22
25
  def param_definitions
@@ -2,61 +2,101 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
  require 'configliere/commandline'
3
3
 
4
4
  describe "Configliere::Commandline" do
5
- before do
6
- @config = Configliere::Param.new :param_1 => 'val 1', :cat => :hat
7
- end
8
5
 
9
- it 'handles --param=val pairs' do
10
- ::ARGV.replace ['--my_param=my_val']
11
- @config.resolve!
12
- @config.should == { :my_param => 'my_val', :param_1 => 'val 1', :cat => :hat}
13
- end
14
- it 'handles --dotted.param.name=val pairs' do
15
- ::ARGV.replace ['--dotted.param.name=my_val']
16
- @config.resolve!
17
- @config.rest.should be_empty
18
- @config.should == { :dotted => { :param => { :name => 'my_val' }}, :param_1 => 'val 1', :cat => :hat}
19
- end
20
- it 'handles --dashed-param-name=val pairs' do
21
- ::ARGV.replace ['--dashed-param-name=my_val']
22
- @config.resolve!
23
- @config.rest.should be_empty
24
- @config.should == { :dashed => { :param => { :name => 'my_val' }}, :param_1 => 'val 1', :cat => :hat}
25
- end
26
- it 'uses the last-seen of the commandline values' do
27
- ::ARGV.replace ['--param_1=A', '--param_1=B']
28
- @config.resolve!
29
- @config.rest.should be_empty
30
- @config.should == { :param_1 => 'B', :cat => :hat}
31
- end
32
- it 'sets a bare parameter (no "=") to true' do
33
- ::ARGV.replace ['--param_1', '--deep.param']
34
- @config.resolve!
35
- @config.rest.should be_empty
36
- @config.should == { :param_1 => true, :deep => { :param => true }, :cat => :hat}
37
- end
38
- it 'sets an explicit blank to nil' do
39
- ::ARGV.replace ['--param_1=', '--deep.param=']
40
- @config.resolve!
41
- @config.should == { :param_1 => nil, :deep => { :param => nil }, :cat => :hat}
6
+ after do
7
+ ::ARGV.replace []
42
8
  end
9
+
10
+ describe "processing long-format flags" do
11
+ before do
12
+ @config = Configliere::Param.new :param_1 => 'val 1', :cat => :hat
13
+ end
43
14
 
44
- it 'saves non --param args into rest' do
45
- ::ARGV.replace ['--param_1', 'file1', 'file2']
46
- @config.resolve!
47
- @config.should == { :param_1 => true, :cat => :hat}
48
- @config.rest.should == ['file1', 'file2']
49
- end
15
+ it 'should handle --param=val pairs' do
16
+ ::ARGV.replace ['--my_param=my_val']
17
+ @config.resolve!
18
+ @config.should == { :my_param => 'my_val', :param_1 => 'val 1', :cat => :hat}
19
+ end
20
+ it 'should handle --dotted.param.name=val pairs' do
21
+ ::ARGV.replace ['--dotted.param.name=my_val']
22
+ @config.resolve!
23
+ @config.rest.should be_empty
24
+ @config.should == { :dotted => { :param => { :name => 'my_val' }}, :param_1 => 'val 1', :cat => :hat}
25
+ end
26
+ it 'should handle --dashed-param-name=val pairs' do
27
+ ::ARGV.replace ['--dashed-param-name=my_val']
28
+ @config.resolve!
29
+ @config.rest.should be_empty
30
+ @config.should == { :dashed => { :param => { :name => 'my_val' }}, :param_1 => 'val 1', :cat => :hat}
31
+ end
32
+ it 'should handle the last-seen of the commandline values' do
33
+ ::ARGV.replace ['--param_1=A', '--param_1=B']
34
+ @config.resolve!
35
+ @config.rest.should be_empty
36
+ @config.should == { :param_1 => 'B', :cat => :hat}
37
+ end
38
+ it 'should set a bare parameter (no "=") to true' do
39
+ ::ARGV.replace ['--param_1', '--deep.param']
40
+ @config.resolve!
41
+ @config.rest.should be_empty
42
+ @config.should == { :param_1 => true, :deep => { :param => true }, :cat => :hat}
43
+ end
44
+ it 'should set an explicit blank to nil' do
45
+ ::ARGV.replace ['--param_1=', '--deep.param=']
46
+ @config.resolve!
47
+ @config.should == { :param_1 => nil, :deep => { :param => nil }, :cat => :hat}
48
+ end
50
49
 
51
- it 'stops processing on "--"' do
52
- ::ARGV.replace ['--param_1=A', '--', '--param_1=B']
53
- @config.resolve!
54
- @config.rest.should == ['--param_1=B']
55
- @config.should == { :param_1 => 'A', :cat => :hat}
50
+ it 'should save non --param args into rest' do
51
+ ::ARGV.replace ['--param_1', 'file1', 'file2']
52
+ @config.resolve!
53
+ @config.should == { :param_1 => true, :cat => :hat}
54
+ @config.rest.should == ['file1', 'file2']
55
+ end
56
+
57
+ it 'should stop processing on "--"' do
58
+ ::ARGV.replace ['--param_1=A', '--', '--param_1=B']
59
+ @config.resolve!
60
+ @config.rest.should == ['--param_1=B']
61
+ @config.should == { :param_1 => 'A', :cat => :hat}
62
+ end
56
63
  end
57
64
 
58
- after do
59
- ::ARGV.replace []
65
+ describe "processing single-letter flags" do
66
+
67
+ before do
68
+ @config = Configliere::Param.new :param_1 => 'val 1', :cat => nil, :foo => nil
69
+ @config.param_definitions = { :param_1 => { :flag => :p }, :cat => { :flag => 'c' } }
70
+ end
71
+
72
+ it 'should parse flags given separately' do
73
+ ::ARGV.replace ['-p', '-c']
74
+ @config.resolve!
75
+ @config.rest.should == []
76
+ @config.should == { :param_1 => true, :cat => true, :foo => nil}
77
+ end
78
+
79
+ it 'should parse flags given together' do
80
+ ::ARGV.replace ['-pc']
81
+ @config.resolve!
82
+ @config.rest.should == []
83
+ @config.should == { :param_1 => true, :cat => true, :foo => nil}
84
+ end
85
+
86
+ it 'should not complain about bad single-letter flags by default' do
87
+ ::ARGV.replace ['-pcz']
88
+ @config.resolve!
89
+ @config.rest.should == []
90
+ @config.should == { :param_1 => true, :cat => true, :foo => nil}
91
+ end
92
+
93
+ it 'should raise an error about bad single-letter flags if asked' do
94
+ ::ARGV.replace ['-pcz']
95
+ @config.complain_about_bad_flags!
96
+ lambda { @config.resolve! }.should raise_error(Configliere::Error)
97
+ end
98
+
60
99
  end
100
+
61
101
  end
62
102
 
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: 27
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
8
+ - 1
7
9
  - 0
8
- - 9
9
- version: 0.0.9
10
+ version: 0.1.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - mrflip
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-10 00:00:00 -05:00
18
+ date: 2010-08-10 00:00:00 -05:00
18
19
  default_executable: configliere
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rspec
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 13
27
30
  segments:
28
31
  - 1
29
32
  - 2
@@ -35,9 +38,11 @@ dependencies:
35
38
  name: yard
36
39
  prerelease: false
37
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
38
42
  requirements:
39
43
  - - ">="
40
44
  - !ruby/object:Gem::Version
45
+ hash: 3
41
46
  segments:
42
47
  - 0
43
48
  version: "0"
@@ -105,23 +110,27 @@ rdoc_options:
105
110
  require_paths:
106
111
  - lib
107
112
  required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
108
114
  requirements:
109
115
  - - ">="
110
116
  - !ruby/object:Gem::Version
117
+ hash: 3
111
118
  segments:
112
119
  - 0
113
120
  version: "0"
114
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
115
123
  requirements:
116
124
  - - ">="
117
125
  - !ruby/object:Gem::Version
126
+ hash: 3
118
127
  segments:
119
128
  - 0
120
129
  version: "0"
121
130
  requirements: []
122
131
 
123
132
  rubyforge_project:
124
- rubygems_version: 1.3.6
133
+ rubygems_version: 1.3.7
125
134
  signing_key:
126
135
  specification_version: 3
127
136
  summary: Wise, discreet configuration management