cmd.rb 0.4.0 → 0.5.0
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 +4 -3
- data/VERSION +1 -1
- data/cmd.rb.gemspec +1 -1
- data/lib/cmd/mixin/option_parser/finder.rb +23 -0
- data/lib/cmd/mixin/option_parser.rb +16 -3
- data/lib/cmd.rb +9 -1
- data/share/examples/cmd.rb/ls +1 -1
- data/test/cmd_test.rb +49 -15
- data/test/readme_examples_test.rb +0 -2
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f520a73aaf3d0510069fb97555c4ec0510924906f7371bc205843ae27cdcdd27
|
4
|
+
data.tar.gz: 3e7b0ed7b8b0452ea9a1bab642aacd927fc1bfb3698e905b201020883ca3e364
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca15e636c297e708d37a669f5508014915295a9aabccbc9d383916fc2534f8b209e12f0530c502a2c4732fc3caebe8a92722c7f4771ce960b64385fe48ae797d
|
7
|
+
data.tar.gz: df656157cf58c6bbc2b2a1abb65b1b9ca73641db17a70c25506b4a36c1e47062d86aaf73f5a1116075ee7ac994da09a9f2c031da1c430ac92e865c5586975034
|
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
|
|
@@ -28,7 +29,7 @@ class Ls < Cmd
|
|
28
29
|
|
29
30
|
def run
|
30
31
|
options = parse_options(argv)
|
31
|
-
|
32
|
+
run_command(options)
|
32
33
|
end
|
33
34
|
|
34
35
|
private
|
@@ -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.
|
1
|
+
v0.5.0
|
data/cmd.rb.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.summary = "A library for building command-line applications, in Ruby."
|
14
14
|
gem.description = gem.summary
|
15
15
|
gem.add_runtime_dependency "ryo.rb", "~> 0.4"
|
16
|
-
gem.add_runtime_dependency "cli-option_parser.rb", "~> 0.
|
16
|
+
gem.add_runtime_dependency "cli-option_parser.rb", "~> 0.6"
|
17
17
|
gem.add_development_dependency "test-unit", "~> 3.5.7"
|
18
18
|
gem.add_development_dependency "yard", "~> 0.9"
|
19
19
|
gem.add_development_dependency "redcarpet", "~> 3.5"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Cmd::Mixin::OptionParser
|
2
|
+
module Finder
|
3
|
+
extend self
|
4
|
+
##
|
5
|
+
# @param [CLI::OptionParser] option_parser
|
6
|
+
# An instance of CLI::OptionParser.
|
7
|
+
#
|
8
|
+
# @param [String] short
|
9
|
+
# Short option.
|
10
|
+
#
|
11
|
+
# @param [String] long
|
12
|
+
# Long option.
|
13
|
+
#
|
14
|
+
# @return [CLI::OptionParser::Switch, nil]
|
15
|
+
# Returns a Switch, or nil.
|
16
|
+
def find(option_parser, short:, long:)
|
17
|
+
option_parser.top.list.find do |s|
|
18
|
+
s.short[0] == short[/[^\s]*/] &&
|
19
|
+
s.long[0] == long[/[^\s*]*/]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -6,6 +6,7 @@
|
|
6
6
|
# [OptionParser](https://docs.ruby-lang.org/en/3.2/OptionParser.html)
|
7
7
|
# class.
|
8
8
|
module Cmd::Mixin::OptionParser
|
9
|
+
require_relative "option_parser/finder"
|
9
10
|
require "cli-option_parser"
|
10
11
|
require "ryo"
|
11
12
|
|
@@ -49,8 +50,16 @@ module Cmd::Mixin::OptionParser
|
|
49
50
|
#
|
50
51
|
# @return [void]
|
51
52
|
def set_option(short, long, desc, as: String, default: nil)
|
52
|
-
option_parser.on(short, long, desc, as)
|
53
|
-
|
53
|
+
option_parser.on(short, long, desc, as) do |v|
|
54
|
+
switch = Finder.find(option_parser, short:, long:)
|
55
|
+
is_optional = option_parser.optional_switches.any? { _1 === switch }
|
56
|
+
v || is_optional ? v : true
|
57
|
+
end
|
58
|
+
switch = option_parser.top.list[-1]
|
59
|
+
set_default({
|
60
|
+
switch.short[0][1..] => default,
|
61
|
+
switch.long[0][2..] => default
|
62
|
+
})
|
54
63
|
end
|
55
64
|
|
56
65
|
##
|
@@ -60,7 +69,11 @@ module Cmd::Mixin::OptionParser
|
|
60
69
|
#
|
61
70
|
# @return [void]
|
62
71
|
def set_default(defaults)
|
63
|
-
@defaults
|
72
|
+
if @defaults
|
73
|
+
Ryo.assign(@defaults, defaults)
|
74
|
+
else
|
75
|
+
@defaults = Ryo.from(defaults)
|
76
|
+
end
|
64
77
|
end
|
65
78
|
|
66
79
|
##
|
data/lib/cmd.rb
CHANGED
@@ -7,6 +7,14 @@ class Cmd
|
|
7
7
|
include Mixin::Help
|
8
8
|
|
9
9
|
def self.inherited(klass)
|
10
|
-
klass.class_eval
|
10
|
+
klass.class_eval do
|
11
|
+
include Mixin::OptionParser
|
12
|
+
prepend Module.new {
|
13
|
+
def run(...)
|
14
|
+
options = parse_options(argv)
|
15
|
+
options.help ? show_help : super(...)
|
16
|
+
end
|
17
|
+
}
|
18
|
+
end
|
11
19
|
end
|
12
20
|
end
|
data/share/examples/cmd.rb/ls
CHANGED
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
|
+
version: 0.5.0
|
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-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ryo.rb
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.6'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.6'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: test-unit
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -146,6 +146,7 @@ files:
|
|
146
146
|
- lib/cmd/mixin/argv.rb
|
147
147
|
- lib/cmd/mixin/help.rb
|
148
148
|
- lib/cmd/mixin/option_parser.rb
|
149
|
+
- lib/cmd/mixin/option_parser/finder.rb
|
149
150
|
- share/examples/cmd.rb/ls
|
150
151
|
- share/examples/cmd.rb/setup.rb
|
151
152
|
- test/cmd_test.rb
|