cmd.rb 0.4.0 → 0.4.1
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 +3 -2
- data/VERSION +1 -1
- data/lib/cmd/mixin/option_parser.rb +32 -3
- data/test/cmd_test.rb +49 -15
- data/test/readme_examples_test.rb +0 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34d12924cba02103e5b13c1581d948f3fc862a588b452aef90b9797ab7bd605b
|
4
|
+
data.tar.gz: c7a4aa7d0266f6c2ed91c6268189310462135b465d4d6a8cfcbb5b170b360480
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d98a6f7bcd51310076ec57fc8947f7d6922b5402a5e0aac5432174fb2db6cfc44e19e07b7e5f11f87eaffb31eb050774891f9f46cd1ab70a9102e36407c5abea
|
7
|
+
data.tar.gz: 7b4620b67fd778a0f01d61dffb5b5dfde52aa10e92fdf15feb655f132638a3a26d0339840543d50665ec128c8a4a28c5d50508c672d59dfb636ef738175a37fe
|
data/README.md
CHANGED
@@ -12,7 +12,8 @@ option parsing implementation is delegated to
|
|
12
12
|
|
13
13
|
The following example demonstrates a simple command that is
|
14
14
|
implemented with `Dir.entries`. The command accepts two options
|
15
|
-
that have fallback default values set
|
15
|
+
that have fallback default values set for when an option is not
|
16
|
+
given:
|
16
17
|
|
17
18
|
**Definition**
|
18
19
|
|
@@ -80,7 +81,7 @@ are available as sources.
|
|
80
81
|
|
81
82
|
```ruby
|
82
83
|
# Gemfile
|
83
|
-
gem "cmd.rb", github: "0x1eef/cmd.rb"
|
84
|
+
gem "cmd.rb", github: "0x1eef/cmd.rb"
|
84
85
|
```
|
85
86
|
|
86
87
|
**Rubygems.org**
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
v0.4.
|
1
|
+
v0.4.1
|
@@ -49,8 +49,15 @@ module Cmd::Mixin::OptionParser
|
|
49
49
|
#
|
50
50
|
# @return [void]
|
51
51
|
def set_option(short, long, desc, as: String, default: nil)
|
52
|
-
option_parser.on(short, long, desc, as)
|
53
|
-
|
52
|
+
option_parser.on(short, long, desc, as) do |v|
|
53
|
+
switch = __switch_search(short, long)
|
54
|
+
v || __optional_switches.any? { _1 === switch } ? v : true
|
55
|
+
end
|
56
|
+
switch = option_parser.top.list[-1]
|
57
|
+
set_default({
|
58
|
+
switch.short[0][1..] => default,
|
59
|
+
switch.long[0][2..] => default
|
60
|
+
})
|
54
61
|
end
|
55
62
|
|
56
63
|
##
|
@@ -60,7 +67,11 @@ module Cmd::Mixin::OptionParser
|
|
60
67
|
#
|
61
68
|
# @return [void]
|
62
69
|
def set_default(defaults)
|
63
|
-
@defaults
|
70
|
+
if @defaults
|
71
|
+
Ryo.assign(@defaults, defaults)
|
72
|
+
else
|
73
|
+
@defaults = Ryo.from(defaults)
|
74
|
+
end
|
64
75
|
end
|
65
76
|
|
66
77
|
##
|
@@ -83,6 +94,24 @@ module Cmd::Mixin::OptionParser
|
|
83
94
|
def defaults
|
84
95
|
@defaults ||= Ryo({})
|
85
96
|
end
|
97
|
+
|
98
|
+
##
|
99
|
+
# @private
|
100
|
+
def __switch_search(short, long)
|
101
|
+
option_parser.top.list.find do |s|
|
102
|
+
s.short[0] == short[/[^\s]*/] &&
|
103
|
+
s.long[0] == long[/[^\s*]*/]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
##
|
108
|
+
# @private
|
109
|
+
def __optional_switches
|
110
|
+
[
|
111
|
+
CLI::OptionParser::Switch::PlacedArgument,
|
112
|
+
CLI::OptionParser::Switch::OptionalArgument
|
113
|
+
]
|
114
|
+
end
|
86
115
|
end
|
87
116
|
|
88
117
|
##
|
data/test/cmd_test.rb
CHANGED
@@ -3,30 +3,64 @@ require_relative "setup"
|
|
3
3
|
class CmdTest < Test::Unit::TestCase
|
4
4
|
class Command < Cmd
|
5
5
|
set_banner usage: "test [OPTIONS]",
|
6
|
-
description: "
|
7
|
-
set_option '-
|
8
|
-
set_option '-
|
6
|
+
description: "A test program"
|
7
|
+
set_option '-x', '--xarg', 'An option without an argument'
|
8
|
+
set_option '-y ARG', '--yarg ARG', 'An option with a required argument'
|
9
|
+
set_option '-z [ARG]', '--zarg [ARG]', 'An option with an optional argument'
|
9
10
|
|
10
11
|
def run
|
11
12
|
parse_options(argv)
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
|
-
def
|
16
|
-
options = Command.new([
|
17
|
-
|
18
|
-
|
16
|
+
def test_none_given
|
17
|
+
options = Command.new([]).run
|
18
|
+
[options.x, options.xarg,
|
19
|
+
options.y, options.yarg,
|
20
|
+
options.z, options.zarg].each do
|
21
|
+
assert_equal nil, _1
|
22
|
+
end
|
19
23
|
end
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
##
|
26
|
+
# No argument option
|
27
|
+
# -x, --xarg
|
28
|
+
|
29
|
+
def test_xarg_given
|
30
|
+
options = Command.new(['-x']).run
|
31
|
+
assert_equal true, options.x
|
32
|
+
assert_equal true, options.xarg
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Required argument option
|
37
|
+
# -y ARG, --yarg ARG
|
38
|
+
|
39
|
+
def test_yarg_given
|
40
|
+
options = Command.new(['-y', 'required-argument']).run
|
41
|
+
assert_equal 'required-argument', options.y
|
42
|
+
assert_equal 'required-argument', options.yarg
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_yarg_missing_argument
|
46
|
+
assert_raises(CLI::OptionParser::MissingArgument) do
|
47
|
+
Command.new(['-y']).run
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# Optional argument option
|
53
|
+
# -z [ARG], --zarg [ARG]
|
54
|
+
|
55
|
+
def test_zarg_given
|
56
|
+
options = Command.new(['-z', 'optional-argument']).run
|
57
|
+
assert_equal 'optional-argument', options.z
|
58
|
+
assert_equal 'optional-argument', options.zarg
|
25
59
|
end
|
26
60
|
|
27
|
-
def
|
28
|
-
options = Command.new(['-
|
29
|
-
assert_equal
|
30
|
-
assert_equal
|
61
|
+
def test_zarg_missing_argument
|
62
|
+
options = Command.new(['-z']).run
|
63
|
+
assert_equal nil, options.z
|
64
|
+
assert_equal nil, options.zarg
|
31
65
|
end
|
32
66
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmd.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- '0x1eef'
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ryo.rb
|