lab42_options 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d60b9c90244ebc315786a02ad9273611b52169a9
4
- data.tar.gz: 4c27dddfa798d3765a7edba5463c055cbcc9a88a
3
+ metadata.gz: d7cc89febbade4730d1c7112704d5655af6d9cd5
4
+ data.tar.gz: 5de219eff28bf69199cdb5c1c59ef157eed0efd9
5
5
  SHA512:
6
- metadata.gz: c75abdec7ecb175a6cbf31ea4c1b64b5e3450a4702f419a9cc1abad731d508d4bfe63663f596a8c46d90ecbb63b15f320a5a7188934cbaec39a85686f08e3581
7
- data.tar.gz: 5f1f185f3203293be14cb4cb2a9c3c989d57c77875c00a3bd21408baaee517ef2d591c3e072c092e0fde33118dd18abdc8aa5f5792c57ec2b8538763ccaeb9b1
6
+ metadata.gz: 1dc5104a71fbe2ca6572fdebb57b88d22215086fb2f8d69b020f49fc9d52f75aeae2c012d590ba20096dc48da311b69eb6478600f079b40b6992fcb9e5c3ce67
7
+ data.tar.gz: 3fdaf3fcd7413022e98b1e322c4ded53296bc0005547a49aa492dac0a12ad9bf41e91f27a9cb922633ec60b9ff547c6b2ddab9995dcae2de7e3e0c0952396bdd
data/README.md CHANGED
@@ -53,6 +53,24 @@ will parse as follows:
53
53
  greet greeting: "howdy" # error missing required argument :target"
54
54
  ```
55
55
 
56
+ ## Reading from yaml files
57
+
58
+ ```ruby
59
+ options = Lab42::Options.new greeting: "hello", target: :required
60
+ options.read_from "./options.yml"
61
+ # or
62
+ options.read_from :load # read from file indicated by load: <file>
63
+ # or
64
+ options.read_from load: "./.default_options.yml" # read from file indicated by load: <file>
65
+ # defaulting to "./.default_options.yml"
66
+ ```
67
+
68
+ Although this allows for nested parameters, defaults and requirements for deeper levels are not implemented right now (and are maybe not in the scope of a parameter parser).
69
+
70
+ Existence of the yaml file is not reenforced either but that might be a good enhancement for the near
71
+ future.
72
+
73
+
56
74
  ## Missing Features
57
75
 
58
76
  * Help Message Generation
@@ -1,16 +1,31 @@
1
+ require 'yaml'
1
2
  module Lab42
2
3
  class Options
3
4
  class Parser
4
- attr_accessor :data, :kwds, :positionals
5
- def parse args
5
+ attr_accessor :data, :defaults, :kwds, :positionals, :yaml_file
6
+ def parse options, args
7
+ self.yaml_file = options.yaml_file
6
8
  self.data = {to_a: args}
7
9
  self.kwds = {}
8
10
  self.positionals = []
9
11
  parse_all args
10
- data.merge kwds: OpenStruct.new(kwds), args: positionals
12
+ # read_yaml file might need the args parsed
13
+ defaults = read_yaml_file
14
+ data.merge kwds: OpenStruct.new(defaults.merge(kwds)), args: positionals
11
15
  end
12
16
 
13
17
  private
18
+ def convert_hash hs
19
+ return hs unless Hash === hs
20
+ hs.keys.inject Hash.new do |h, k|
21
+ if String === k
22
+ h.merge k.to_sym => convert_hash( hs[k] )
23
+ else
24
+ h.merge k => convert_hash( hs[k] )
25
+ end
26
+ end
27
+ end
28
+
14
29
  def parse_all args
15
30
  e = (args[0..-1] || []).enum_for :each
16
31
  loop do
@@ -25,6 +40,24 @@ module Lab42
25
40
  end
26
41
  end
27
42
 
43
+ def read_yaml_file
44
+ read_yaml_file!
45
+ rescue Errno::ENOENT
46
+ {}
47
+ end
48
+
49
+ def read_yaml_file!
50
+ case yaml_file
51
+ when Hash
52
+ file = kwds.fetch( yaml_file.keys.first, yaml_file.values.first )
53
+ file ? convert_hash( YAML.load File.read file ) : {}
54
+ when String
55
+ convert_hash YAML.load File.read yaml_file
56
+ else
57
+ {}
58
+ end
59
+ end
60
+
28
61
  def update_value key, val
29
62
  kwds.merge! key => val do |k, oldv, newv|
30
63
  Array( oldv ) << newv
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  class Options
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
data/lib/lab42/options.rb CHANGED
@@ -4,9 +4,11 @@ require_relative './options/forwarder'
4
4
 
5
5
  module Lab42
6
6
  class Options
7
+ attr_reader :yaml_file
8
+
7
9
  def parse *args
8
10
  args = args.first if Array === args.first
9
- @parsed = Lab42::Options::Parser.new.parse( args )
11
+ @parsed = Lab42::Options::Parser.new.parse( self, args )
10
12
  set_defaults
11
13
  check_required
12
14
  issue_errors!
@@ -14,6 +16,17 @@ module Lab42
14
16
  result.forwarding_to :kwds
15
17
  end
16
18
 
19
+ def read_from file_sym_or_hash
20
+ case file_sym_or_hash
21
+ when String
22
+ read_from_file file_sym_or_hash
23
+ when Symbol
24
+ read_from_parameterized_file file_sym_or_hash => nil
25
+ else
26
+ read_from_parameterized_file file_sym_or_hash
27
+ end
28
+ end
29
+
17
30
  private
18
31
  def initialize options={}
19
32
  @registered = {}
@@ -42,6 +55,15 @@ module Lab42
42
55
  @registered[k] = v
43
56
  end
44
57
 
58
+ def read_from_file file
59
+ @yaml_file = file
60
+ end
61
+
62
+ def read_from_parameterized_file params
63
+ raise ArgumentError, "#{params} is not a Hash" unless Hash === params
64
+ @yaml_file = params
65
+ end
66
+
45
67
  def required_options
46
68
  @__required_options__ ||=
47
69
  @registered.select{|_,v| v == :required}.keys
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_options
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-08 00:00:00.000000000 Z
11
+ date: 2013-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry