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 +4 -4
- data/README.md +19 -0
- data/lib/optparse2/fixes.rb +19 -0
- data/lib/optparse2/globals.rb +13 -0
- data/lib/optparse2/version.rb +2 -2
- data/lib/optparse2.rb +73 -22
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dda7dfaa9390f8a4e8cf1cc33c382b31e12caa3aa3e2c8d66684dc14bca5381a
|
|
4
|
+
data.tar.gz: bc3ddf03d60a82c084fece437072cdb8e3aada7cab0abed5ae5f73bdbcab077e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
data/lib/optparse2/version.rb
CHANGED
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
def on_head(...) define_head(...); self end
|
|
35
|
+
@default_description = description
|
|
36
|
+
end
|
|
14
37
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
24
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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.
|
|
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:
|