optparse2 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b65c3cbdeee5a8be4421cfb6690664c5a717493b21d27c7e2772796bbfc07c31
4
- data.tar.gz: 2635447e0c1d6aeb4b64d1d5cd85aa3f722f5d73d67a8d111983e6f1f230d2e6
3
+ metadata.gz: dda7dfaa9390f8a4e8cf1cc33c382b31e12caa3aa3e2c8d66684dc14bca5381a
4
+ data.tar.gz: bc3ddf03d60a82c084fece437072cdb8e3aada7cab0abed5ae5f73bdbcab077e
5
5
  SHA512:
6
- metadata.gz: 93e0b546543a5cd14e0c013c8f36031f1661ed8c5bed77eebf8b833533245fb1dbd4ced50ec9b56167a89fe4f0ab16096172a01b3e8723bc6b7766a0ddf5a6c8
7
- data.tar.gz: 9c2d74d86790e248f1a2fb660155b9514f36efd4edc8dd17eff4491dd2d184e246c51fbc0e8ce6ecab112c5bc13aa252dca74c823aebd1bc0638655f2b6b3832
6
+ metadata.gz: 2b86da17e14359336c501cfdeb5dd552915a1e069b68874854b098f662dd63bc78aa82cc226d0c75a2bb885a24db806800894f719a063e60941d7357cb31929c
7
+ data.tar.gz: 8718f37e9eb5d2a4f9d6674800682cd7b487608621d768fdadde9271404d0914e558f96e3efdcf66306cd4e943250ae9f0cc9725a738364bb98a59f41d355105
data/README.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # OptParse2
2
2
 
3
+ example:
4
+
5
+ ```ruby
6
+ OptParse2.new do |op|
7
+ op.on('-d', '--debug', hidden: true) do |dbg|
8
+ puts "debug = #{dbg}"
9
+ end
10
+
11
+ op.on('-b', '--branch=STRING')
12
+ op.on('-m', '--master', key: :branch) { :master }
13
+
14
+ puts op.help
15
+ op.parse! %w[-d --branch=foo --master], into: h={}
16
+ p h
17
+ end
18
+ ```
19
+
20
+
21
+
3
22
  TODO: Delete this and the text below, and describe your gem
4
23
 
5
24
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/optparse2`. To experiment with that code, run `bin/console` for an interactive prompt.
@@ -0,0 +1,19 @@
1
+ class OptParse2
2
+ # Provide support for passing keyword arguments into `make_switch`
3
+ def define(*opts, **, &block) top.append(*(sw = make_switch(opts, block, **))); sw[0] end
4
+ alias def_option define
5
+ def on(...) define(...); self end
6
+
7
+ def define_head(*opts, **, &block) top.prepend(*(sw = make_switch(opts, block, **))); sw[0] end
8
+ alias def_head_option define_head
9
+ def on_head(...) define_head(...); self end
10
+
11
+ def define_head(*opts, **, &block) base.append(*(sw = make_switch(opts, block, **))); sw[0] end
12
+ alias def_tail_option define_tail
13
+ def on_tail(...) define_tail(...); self end
14
+
15
+ def initialize(*)
16
+ @defaults = {}
17
+ super
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OptParse2::Globals
4
+ def self.[]=(key, value)
5
+ key = key.to_s.gsub('-', '_')
6
+
7
+ unless key.match? /\A[[:alpha:]_][[:alnum:]_]*\z/
8
+ raise "invalid global name: #{key}"
9
+ end
10
+
11
+ eval "$#{key} = value"
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module OptParse2
4
- VERSION = "0.1.0"
3
+ class OptParse2
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/optparse2.rb CHANGED
@@ -1,27 +1,66 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'optparse'
4
+ class OptParse2 < OptParse; end # Make sure it's a subclass
5
+ OptionParser2 = OptParse2 # Alias
6
+
3
7
  require_relative "optparse2/version"
8
+ require_relative "optparse2/fixes"
9
+ # require_relative "optparse2/helpers"
10
+
11
+ class OptParse2
12
+ def initialize(...)
13
+ @defaults = Set[]
14
+ super
15
+ end
16
+
17
+ ## Helpers is a mixin that contains methods to modify how the original `Switch` works
18
+ module Helpers
19
+ def set_hidden
20
+ def self.summarize(*) end
21
+ end
22
+
23
+ attr_writer :switch_name
24
+ def switch_name; defined?(@switch_name) ? @switch_name : super end
25
+
4
26
 
5
- class OptParse2 < OptParse
6
- # Provide support for passing keyword arguments into `make_switch`
7
- def define(*opts, **, &block) top.append(*(sw = make_switch(opts, block, **))); sw[0] end
8
- alias def_option define
9
- def on(...) define(...); self end
27
+ # requires `switch_name`, `desc` to work
28
+ def set_default(value, description)
29
+ if defined? value.call
30
+ @default = value
31
+ else
32
+ @default = proc { value }
33
+ end
10
34
 
11
- def define_head(*opts, **, &block) top.prepend(*(sw = make_switch(opts, block, **))); sw[0] end
12
- alias def_head_option define_head
13
- def on_head(...) define_head(...); self end
35
+ @default_description = description
36
+ end
14
37
 
15
- def define_head(*opts, **, &block) base.append(*(sw = make_switch(opts, block, **))); sw[0] end
16
- alias def_tail_option define_tail
17
- def on_tail(...) define_tail(...); self end
38
+ def default = @default.call(switch_name)
39
+ def default_description = @default_description || default.inspect
40
+ def desc
41
+ return super unless defined? @default
42
+ x = super
43
+ x << '' if x.empty?
44
+ x.last << " [default: #{default_description}]"
45
+ x
46
+ end
47
+ end
18
48
 
19
49
  # Update `make_switch` to support OptParse2's keyword arguments
20
- def make_switch(opts, block, hidden: false, key: nil)
50
+ def make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil)
21
51
  sw, *rest = super(opts, block)
22
52
 
23
- key and sw.define_singleton_method(:switch_name) { key }
24
- hidden and def sw.summarize(*) end
53
+ sw.extend Helpers
54
+
55
+ sw.switch_name = key if key
56
+ sw.set_hidden if hidden
57
+
58
+ if nodefault && default_description != nil
59
+ raise ArgumentError, "default: not supplied, but default_description: given"
60
+ elsif not nodefault
61
+ sw.set_default(default, default_description)
62
+ @defaults << sw
63
+ end
25
64
 
26
65
  [sw, *rest]
27
66
  end
@@ -31,14 +70,26 @@ class OptParse2 < OptParse
31
70
  on_tail("\n" + msg)
32
71
  end
33
72
 
34
- # def env(var, *opts, hidden: false, &)
35
- # fail if hidden
36
- # sw, = make_switch(['-_X', *opts])
73
+ def order!(argv = default_argv, into: nil, **keywords, &nonopt)
74
+ if into.nil? && !@defaults.empty?
75
+ raise "cannot call `order!` without an `into:` if there are default values"
76
+ end
37
77
 
38
- # p sw
39
- # exit
40
- # on_tail(var)
41
- # end
78
+ already_done = {}
79
+ already_done.define_singleton_method(:[]=) do |key, value|
80
+ key = key.to_s
81
+ super(key, value)
82
+ into[key] = value
83
+ end
42
84
 
43
- # def positional
85
+ result = super(argv, into: already_done, **keywords, &nonopt)
86
+
87
+ @defaults.each do |sw|
88
+ key = sw.switch_name
89
+ next if already_done.key? key
90
+ into[key] = sw.default()
91
+ end
92
+
93
+ result
94
+ end
44
95
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optparse2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - SamW
@@ -19,6 +19,8 @@ files:
19
19
  - README.md
20
20
  - Rakefile
21
21
  - lib/optparse2.rb
22
+ - lib/optparse2/fixes.rb
23
+ - lib/optparse2/globals.rb
22
24
  - lib/optparse2/version.rb
23
25
  - sig/optparse2.rbs
24
26
  licenses: