autoargs 0.1.0 → 0.1.1

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: a4904378daa3039b791b8631f164adbc182f3c5a
4
- data.tar.gz: 2731046da2d3e254361f1a4f88337f790afc612d
3
+ metadata.gz: b692c49b540de544d7f3d738aa3187acf9b15026
4
+ data.tar.gz: ff8053961c2f3cedd9a0f16fa24feb7b422eb859
5
5
  SHA512:
6
- metadata.gz: 719b82c31a0028421217aa024c2408132f32aa1f89ee25314c570d969a364630b3835b48fee0c58d01c30ce7a41f26df1341c40a8d646b5fd51d06326b45ff3a
7
- data.tar.gz: 0524db0755ffc9473352329221343ec42235da886172a531787a052293fe6182af5338e296cca25330505c6ea02e5638fd0c106202cfa5ff898afc2055cf5414
6
+ metadata.gz: 8f9213af79aa58eac52516eaff977e3dac62825637db6df82f2a2b41cc821ba6d5568367e02c03f455f099fa0fa2b3c9375822385ec2b4648e803353caec6f40
7
+ data.tar.gz: 22baba0e680bf8ca688e811709cb6d0ee0a0674c5a0fbabecb8de49fbf528c110b2a1ccb22ae868529122031daaa3f0fc90ab4fab070c53f28dd7fe4d9680d37
@@ -1,3 +1,3 @@
1
1
  module Autoargs
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/autoargs.rb CHANGED
@@ -31,28 +31,41 @@ module Autoargs
31
31
  .map do |arg| [arg.children[0], arg.children[1].children[0]] end
32
32
  ]
33
33
 
34
+ usage = [[caller_file],
35
+ args.map do |arg| "<" + arg.to_s + ">" end,
36
+ optargs.map do |optarg| "[" + optarg[:name].to_s + "=" + optarg[:default].inspect + "]" end,
37
+ kwoptargs.map do |name, default| "[--" + name.to_s + " " + default.inspect + "]" end
38
+ ].flatten(1).join(" ")
39
+
34
40
  argv_args = ARGV[0 ... args.length]
35
41
  argv_optargs = ARGV[args.length ... args.length + optargs.length]
36
42
  argv_kwoptargs = Hash[
37
43
  (ARGV[args.length + optargs.length .. -1] || [])
38
44
  .each_slice(2)
39
45
  .map do |name_with_prefix, value|
40
- abort("Expected argument " + name_with_prefix + " to start with --.") unless name_with_prefix.start_with?("--")
41
- name = name_with_prefix[2 .. -1]
42
- abort(name + " is not an option.") unless kwoptargs.has_key?(name.to_sym)
43
- [name.to_sym, value]
46
+ begin
47
+ raise "Expected argument " + name_with_prefix + " to start with --." unless name_with_prefix.start_with?("--")
48
+ raise "Expected value for argument " + name_with_prefix if value.nil?
49
+ name = name_with_prefix[2 .. -1]
50
+ raise name + " is not an option." unless kwoptargs.has_key?(name.to_sym)
51
+ [name.to_sym, value]
52
+ rescue Exception => e
53
+ puts(e.message)
54
+ puts
55
+ puts(usage)
56
+ abort
57
+ end
44
58
  end
45
59
  ]
46
60
 
47
- if argv_args.length < args.length
48
- abort([[caller_file],
49
- args.map do |arg| "<" + arg.to_s + ">" end,
50
- optargs.map do |optarg| "[" + optarg[:name].to_s + "=" + optarg[:default].inspect + "]" end,
51
- kwoptargs.map do |name, default| "[--" + name.to_s + " " + default.inspect + "]" end
52
- ].flatten(1).join(" "))
53
- end
61
+ abort(usage) if argv_args.length < args.length
54
62
 
55
- method.call(*ARGV.slice(0, args.length + optargs.length), **argv_kwoptargs)
63
+ positionals = ARGV.slice(0, args.length + optargs.length)
64
+ if argv_kwoptargs.keys.length != 0
65
+ method.call(*positionals, **argv_kwoptargs)
66
+ else
67
+ method.call(*positionals)
68
+ end
56
69
  end
57
70
  end
58
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autoargs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wendt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-24 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser