argument_parser 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/argument_parser/version.rb +1 -1
- data/lib/argument_parser.rb +15 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 931b98bf4d4b68602911f58b5ab1a4d1a6e02ac35a38669a59914bb60bdd4b40
|
4
|
+
data.tar.gz: 0051147620fc860c547e4969032a73447983ea3e8231aa7c8a932b14b375f349
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ada16d5274305072a34ba5bd99b665dc79b8c5eb98273d7ba7def24fa926ee6cb911a7b4d56dc423351bceae3bb3d81c5ec25e8496428a57b7963003f6bb2fb9
|
7
|
+
data.tar.gz: 54b4ae6d257c3fee950f616c95ab453a1fa95dbc606412ae1608c90ba3697213aa5242b235f45d85775092e36a23c8f47db8874b9425d2148e5bc5dab42210ff
|
data/lib/argument_parser.rb
CHANGED
@@ -7,8 +7,9 @@ module ArgumentParser
|
|
7
7
|
|
8
8
|
Error = Class.new(StandardError)
|
9
9
|
SchemaError = Class.new(Error)
|
10
|
-
|
11
|
-
|
10
|
+
ParseError = Class.new(Error)
|
11
|
+
MissingArgument = Class.new(ParseError)
|
12
|
+
InvalidArgument = Class.new(ParseError)
|
12
13
|
|
13
14
|
class Parser < Data.define(:schema)
|
14
15
|
def parse!(argv = ARGV)
|
@@ -46,10 +47,13 @@ module ArgumentParser
|
|
46
47
|
private
|
47
48
|
|
48
49
|
def validate_pattern!(arg, options)
|
49
|
-
coerced_arg = coerce!(arg, options)
|
50
|
-
|
50
|
+
coerced_arg = coerce!(arg, options[:type])
|
51
|
+
pattern = options[:pattern]
|
52
|
+
return coerced_arg unless pattern
|
51
53
|
return coerced_arg unless arg
|
52
|
-
return coerced_arg if
|
54
|
+
return coerced_arg if pattern === coerced_arg
|
55
|
+
return pattern[coerced_arg] if pattern.respond_to?(:key?) && pattern.key?(coerced_arg)
|
56
|
+
return coerced_arg if pattern.respond_to?(:include?) && pattern.include?(coerced_arg)
|
53
57
|
|
54
58
|
raise InvalidArgument, "invalid argument: #{arg}"
|
55
59
|
end
|
@@ -64,18 +68,18 @@ module ArgumentParser
|
|
64
68
|
end
|
65
69
|
end
|
66
70
|
|
67
|
-
def coerce!(arg,
|
71
|
+
def coerce!(arg, type)
|
68
72
|
return arg unless arg
|
69
|
-
return arg unless
|
73
|
+
return arg unless type
|
70
74
|
|
71
|
-
if
|
75
|
+
if type == Integer
|
72
76
|
Integer(arg)
|
73
|
-
elsif
|
77
|
+
elsif type == Float
|
74
78
|
Float(arg)
|
75
|
-
elsif
|
79
|
+
elsif type == String
|
76
80
|
arg
|
77
81
|
else
|
78
|
-
raise SchemaError, "type not supported: #{
|
82
|
+
raise SchemaError, "type not supported: #{type}"
|
79
83
|
end
|
80
84
|
rescue ArgumentError
|
81
85
|
raise InvalidArgument, "invalid argument: #{arg}"
|