cmd.rb 0.3.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff8ad36671b624aa1d94446930520fa7f61a5d88731328fa2c7f6bbf95d848a8
4
- data.tar.gz: 93c450c987bbd04f0b20a068580f00e75cf41016edcc12d32dc2559eedfef6e8
3
+ metadata.gz: 34d12924cba02103e5b13c1581d948f3fc862a588b452aef90b9797ab7bd605b
4
+ data.tar.gz: c7a4aa7d0266f6c2ed91c6268189310462135b465d4d6a8cfcbb5b170b360480
5
5
  SHA512:
6
- metadata.gz: c0cc266b825f3fda55713aa5967a03d239db5cbb445081d074b4293453e429aad3c2a59fe570b9fca513112ef35c34db820bb8b4a26d03a89be65d1c37d5f538
7
- data.tar.gz: bf3ca72be9bb7b40ca34f5497dd33b0c7b80834756c5dc13add3963c4a3c5667117dc047ee7e6234f19b42a6baf47a8fef4cfb12ae30946b912f39c4b5397c88
6
+ metadata.gz: d98a6f7bcd51310076ec57fc8947f7d6922b5402a5e0aac5432174fb2db6cfc44e19e07b7e5f11f87eaffb31eb050774891f9f46cd1ab70a9102e36407c5abea
7
+ data.tar.gz: 7b4620b67fd778a0f01d61dffb5b5dfde52aa10e92fdf15feb655f132638a3a26d0339840543d50665ec128c8a4a28c5d50508c672d59dfb636ef738175a37fe
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_PATH: ".gems"
data/.projectile CHANGED
@@ -1,2 +1,2 @@
1
- -.localgems/
1
+ -.gems/
2
2
  -doc/
data/Gemfile CHANGED
@@ -1,6 +1,3 @@
1
1
  source "https://rubygems.org"
2
2
  gemspec
3
- gem "optparse", github: "ruby/optparse"
4
- gem "ryo.rb", "~> 0.4", github: "0x1eef/ryo.rb", tag: "v0.4.2"
5
- gem "test-cmd.rb", "~> 0.2", github: "0x1eef/test-cmd.rb"
6
3
  gem "webrick"
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
 
@@ -70,19 +71,25 @@ Options:
70
71
 
71
72
  ## Install
72
73
 
74
+ **Git**
75
+
73
76
  cmd.rb is distributed as a RubyGem through its git repositories. <br>
74
77
  [GitHub](https://github.com/0x1eef/cmd.rb),
75
78
  and
76
79
  [GitLab](https://gitlab.com/0x1eef/cmd.rb)
77
80
  are available as sources.
78
81
 
79
- **Gemfile**
80
-
81
82
  ```ruby
83
+ # Gemfile
82
84
  gem "cmd.rb", github: "0x1eef/cmd.rb"
83
- gem "ryo.rb", github: "0x1eef/ryo.rb"
84
85
  ```
85
86
 
87
+ **Rubygems.org**
88
+
89
+ cmd.rb can also be installed via rubygems.org.
90
+
91
+ gem install cmd.rb
92
+
86
93
  ## <a id="license"> License </a>
87
94
 
88
95
  [BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
data/Rakefile.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "bundler/setup"
1
2
  require "rake/testtask"
2
3
 
3
4
  Rake::TestTask.new do |t|
data/VERSION CHANGED
@@ -1 +1 @@
1
- v0.3.3
1
+ v0.4.1
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 "optparse", "~> 0.3"
16
+ gem.add_runtime_dependency "cli-option_parser.rb", "~> 0.5"
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"
@@ -1,11 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  ##
4
- # The {Cmd::ARGV} module provides an initialize method, and an
5
- # attribute reader for an array of strings (usually ARGV). Classes
6
- # who include the {Cmd Cmd} module indirectly include this module as
7
- # well.
8
- module Cmd::ARGV
4
+ # The {Cmd::Mixin::ARGV Cmd::Mixin::ARGV} module provides
5
+ # an initialize method, and an attribute reader for an
6
+ # array of strings (usually ARGV).
7
+ module Cmd::Mixin::ARGV
9
8
  ##
10
9
  # @return [Array<String>]
11
10
  # Returns an array of strings.
@@ -1,5 +1,5 @@
1
1
 
2
- module Cmd::Help
2
+ module Cmd::Mixin::Help
3
3
  def show_help(io = $stdout)
4
4
  banner = self.class.banner
5
5
  io.puts "Usage: #{banner.usage}\n\n" \
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "optparse"
4
- require "ryo"
5
-
6
3
  ##
7
- # The {Cmd::OptionParser Cmd::OptionParser} module provides a light wrapper
8
- # around Ruby's
4
+ # The {Cmd::Mixin::OptionParser Cmd::Mixin::OptionParser} module enhances
5
+ # the functionality of Ruby's
9
6
  # [OptionParser](https://docs.ruby-lang.org/en/3.2/OptionParser.html)
10
7
  # class.
11
- module Cmd::OptionParser
8
+ module Cmd::Mixin::OptionParser
9
+ require "cli-option_parser"
10
+ require "ryo"
11
+
12
12
  def self.included(klass)
13
13
  klass.extend(ClassMethods)
14
14
  end
@@ -49,8 +49,15 @@ module Cmd::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
- set_default({ short => default, long => default })
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,13 +67,17 @@ module Cmd::OptionParser
60
67
  #
61
68
  # @return [void]
62
69
  def set_default(defaults)
63
- @defaults = Ryo.from(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
  ##
67
- # (see Cmd::OptionParser#option_parser)
78
+ # (see Cmd::Mixin::OptionParser#option_parser)
68
79
  def option_parser
69
- @option_parser ||= ::OptionParser.new(nil, 26, " " * 2).tap do
80
+ @option_parser ||= CLI::OptionParser.new(nil, 26, " " * 2).tap do
70
81
  _1.banner = ""
71
82
  _1.on("-h", "--help", "Show help")
72
83
  end
@@ -83,12 +94,31 @@ module Cmd::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
  ##
89
118
  # Parses an array of strings with
90
119
  # [OptionParser#parse](https://docs.ruby-lang.org/en/3.2/OptionParser.html#method-i-parse).
91
120
  #
121
+
92
122
  # @param [Array<String>] argv
93
123
  # An array of strings
94
124
  #
data/lib/cmd/mixin.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Cmd::Mixin
2
+ require_relative "mixin/help"
3
+ require_relative "mixin/argv"
4
+ require_relative "mixin/option_parser"
5
+ end
data/lib/cmd.rb CHANGED
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Cmd
4
- require_relative "cmd/mixins/argv"
5
- require_relative "cmd/mixins/help"
6
- require_relative "cmd/mixins/option_parser"
4
+ require_relative "cmd/mixin"
7
5
 
8
- include ARGV
9
- include Help
6
+ include Mixin::ARGV
7
+ include Mixin::Help
10
8
 
11
9
  def self.inherited(klass)
12
- klass.class_eval { include(OptionParser) }
10
+ klass.class_eval { include(Mixin::OptionParser) }
13
11
  end
14
12
  end
data/test/cmd_test.rb ADDED
@@ -0,0 +1,66 @@
1
+ require_relative "setup"
2
+
3
+ class CmdTest < Test::Unit::TestCase
4
+ class Command < Cmd
5
+ set_banner usage: "test [OPTIONS]",
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'
10
+
11
+ def run
12
+ parse_options(argv)
13
+ end
14
+ end
15
+
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
23
+ end
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
59
+ end
60
+
61
+ def test_zarg_missing_argument
62
+ options = Command.new(['-z']).run
63
+ assert_equal nil, options.z
64
+ assert_equal nil, options.zarg
65
+ end
66
+ end
@@ -3,8 +3,6 @@
3
3
  require_relative "setup"
4
4
 
5
5
  class READMETest < Test::Unit::TestCase
6
- include Test::Cmd
7
-
8
6
  def test_ls_command
9
7
  assert_equal "dev\netc\nhome\n",
10
8
  run_example("ls --directory test/fakefs/ --grep [^.gitkeep]").stdout
data/test/setup.rb CHANGED
@@ -3,3 +3,4 @@
3
3
  require "bundler/setup"
4
4
  require "test-unit"
5
5
  require "test-cmd"
6
+ require "cmd"
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.3.3
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: 2023-12-13 00:00:00.000000000 Z
11
+ date: 2024-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ryo.rb
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.4'
27
27
  - !ruby/object:Gem::Dependency
28
- name: optparse
28
+ name: cli-option_parser.rb
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.3'
33
+ version: '0.5'
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.3'
40
+ version: '0.5'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: test-unit
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -129,13 +129,11 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
+ - ".bundle/config"
132
133
  - ".github/workflows/tests.yml"
133
134
  - ".gitignore"
134
135
  - ".projectile"
135
136
  - ".rubocop.yml"
136
- - ".yardoc-template/default/fulldoc/html/css/0x1eef.css"
137
- - ".yardoc-template/default/layout/html/setup.rb"
138
- - ".yardoc-template/default/module/setup.rb"
139
137
  - ".yardopts"
140
138
  - Gemfile
141
139
  - LICENSE
@@ -144,11 +142,13 @@ files:
144
142
  - VERSION
145
143
  - cmd.rb.gemspec
146
144
  - lib/cmd.rb
147
- - lib/cmd/mixins/argv.rb
148
- - lib/cmd/mixins/help.rb
149
- - lib/cmd/mixins/option_parser.rb
145
+ - lib/cmd/mixin.rb
146
+ - lib/cmd/mixin/argv.rb
147
+ - lib/cmd/mixin/help.rb
148
+ - lib/cmd/mixin/option_parser.rb
150
149
  - share/examples/cmd.rb/ls
151
150
  - share/examples/cmd.rb/setup.rb
151
+ - test/cmd_test.rb
152
152
  - test/fakefs/.gitkeep
153
153
  - test/fakefs/dev/.gitkeep
154
154
  - test/fakefs/etc/.gitkeep
@@ -174,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  - !ruby/object:Gem::Version
175
175
  version: '0'
176
176
  requirements: []
177
- rubygems_version: 3.4.10
177
+ rubygems_version: 3.5.3
178
178
  signing_key:
179
179
  specification_version: 4
180
180
  summary: A library for building command-line applications, in Ruby.
@@ -1,15 +0,0 @@
1
- #main #content #filecontents p, .discussion p {
2
- max-width: 768px;
3
- }
4
-
5
- #toc {
6
- position: fixed;
7
- right: 25px;
8
- display: none;
9
- }
10
-
11
- @media screen and (min-width: 1280px) {
12
- #toc {
13
- display: block;
14
- }
15
- }
@@ -1,5 +0,0 @@
1
- def stylesheets
2
- s = super
3
- s << "css/0x1eef.css"
4
- s
5
- end
@@ -1,7 +0,0 @@
1
- ##
2
- # Sort methods in ascending order based
3
- # on the line number where a method is
4
- # defined.
5
- def sort_listing(listing)
6
- listing.sort_by { _1.files[1] }
7
- end