lab42_options 0.2.0 → 0.3.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: fc16f86cc55f71e19a7c41cd024131a53e2968db
4
- data.tar.gz: 47887a14848a7d6c666f48a1d908c93577e152f9
3
+ metadata.gz: d60b9c90244ebc315786a02ad9273611b52169a9
4
+ data.tar.gz: 4c27dddfa798d3765a7edba5463c055cbcc9a88a
5
5
  SHA512:
6
- metadata.gz: 777cf27a5727f4ce0484f72c3e2364a697b47732e748b2d6d1e6960305d4e237afeecdff4c500a4ee8ba420084a1842d6ef1d55d4868c4e4635f41212b556e25
7
- data.tar.gz: e9f5f84c8b68bcc8cd44dbd9efd24517a198af1c6057f562df62ee34dff0571dc5f2bd3e2296cfadf9643d8637732612725924c091e54e972dc02eb1ca6ff257
6
+ metadata.gz: c75abdec7ecb175a6cbf31ea4c1b64b5e3450a4702f419a9cc1abad731d508d4bfe63663f596a8c46d90ecbb63b15f320a5a7188934cbaec39a85686f08e3581
7
+ data.tar.gz: 5f1f185f3203293be14cb4cb2a9c3c989d57c77875c00a3bd21408baaee517ef2d591c3e072c092e0fde33118dd18abdc8aa5f5792c57ec2b8538763ccaeb9b1
data/README.md CHANGED
@@ -39,18 +39,27 @@ Will yield
39
39
  options.mixed # --> [true, "pickels"]
40
40
  ```
41
41
 
42
+ ## Required and Default Arguments
43
+
44
+ ```ruby
45
+ Lab42::Options.new greeting: "hello", target: :required
46
+ ```
47
+
48
+ will parse as follows:
49
+
50
+ ```sh
51
+ greet target: "world" # target = "world", greeting = "hello"
52
+ greet target: "world" greeting: "cheerio" # target = "world", greeting = "cheerio"
53
+ greet greeting: "howdy" # error missing required argument :target"
54
+ ```
55
+
42
56
  ## Missing Features
43
57
 
44
58
  * Help Message Generation
45
59
 
46
60
  https://github.com/RobertDober/lab42_options/issues/3
47
61
 
48
- * Required Arguments
49
-
50
- https://github.com/RobertDober/lab42_options/issues/2
51
-
52
- * Default Arguments
62
+ * Typed Arguments
53
63
 
54
- https://github.com/RobertDober/lab42_options/issues/2
64
+ https://github.com/RobertDober/lab42_options/issues/4
55
65
 
56
- * Typed Arguments
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  class Options
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
data/lib/lab42/options.rb CHANGED
@@ -6,11 +6,51 @@ module Lab42
6
6
  class Options
7
7
  def parse *args
8
8
  args = args.first if Array === args.first
9
- result = OpenStruct.new Lab42::Options::Parser.new.parse( args )
9
+ @parsed = Lab42::Options::Parser.new.parse( args )
10
+ set_defaults
11
+ check_required
12
+ issue_errors!
13
+ result = OpenStruct.new @parsed
10
14
  result.forwarding_to :kwds
11
15
  end
16
+
12
17
  private
13
- def intialize *args
18
+ def initialize options={}
19
+ @registered = {}
20
+ @errors = []
21
+ options.each do | k, v |
22
+ register_option k, v
23
+ end
24
+ end
25
+
26
+ def check_required
27
+ required_options.each do |ro|
28
+ @errors << "Required option #{ro} was not provided" unless @parsed[:kwds].to_h.has_key? ro
29
+ end
30
+ end
31
+
32
+ def defaults
33
+ @__defaults__ =
34
+ @registered.select{|_,v| v != :required}
35
+ end
36
+
37
+ def issue_errors!
38
+ return if @errors.empty?
39
+ raise ArgumentError, @errors.join("\n")
40
+ end
41
+ def register_option k, v
42
+ @registered[k] = v
43
+ end
44
+
45
+ def required_options
46
+ @__required_options__ ||=
47
+ @registered.select{|_,v| v == :required}.keys
48
+ end
49
+
50
+ def set_defaults
51
+ defaults.each do |k, dv|
52
+ @parsed[:kwds][k] = dv unless @parsed[:kwds].to_h.has_key? k
53
+ end
14
54
  end
15
55
  end
16
56
  end
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.2.0
4
+ version: 0.3.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-06 00:00:00.000000000 Z
11
+ date: 2013-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry