cri 2.10.1 → 2.11.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.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'helper'
2
4
 
3
5
  module Cri
@@ -39,12 +41,12 @@ module Cri
39
41
  expected_option_definitions =
40
42
  Set.new(
41
43
  [
42
- { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil },
43
- { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil },
44
- { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil },
45
- { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
46
- { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
47
- { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil },
44
+ { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
45
+ { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
46
+ { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
47
+ { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
48
+ { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
49
+ { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
48
50
  ],
49
51
  )
50
52
  actual_option_definitions = Set.new(command.option_definitions)
@@ -78,8 +80,8 @@ module Cri
78
80
  expected_option_definitions =
79
81
  Set.new(
80
82
  [
81
- { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
82
- { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
83
+ { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
84
+ { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
83
85
  ],
84
86
  )
85
87
  actual_option_definitions = Set.new(command.option_definitions)
@@ -102,9 +104,9 @@ module Cri
102
104
  expected_option_definitions =
103
105
  Set.new(
104
106
  [
105
- { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: nil },
106
- { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil },
107
- { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil },
107
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
108
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
109
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
108
110
  ],
109
111
  )
110
112
  actual_option_definitions = Set.new(command.option_definitions)
@@ -127,9 +129,9 @@ module Cri
127
129
  expected_option_definitions =
128
130
  Set.new(
129
131
  [
130
- { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil },
131
- { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil },
132
- { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil },
132
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
133
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
134
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
133
135
  ],
134
136
  )
135
137
  actual_option_definitions = Set.new(command.option_definitions)
@@ -222,7 +224,7 @@ module Cri
222
224
  def test_runner
223
225
  # Define
224
226
  dsl = Cri::CommandDSL.new
225
- dsl.instance_eval <<-EOS
227
+ dsl.instance_eval(<<-CMD, __FILE__, __LINE__ + 1)
226
228
  class Cri::CommandDSLTestCaseCommandRunner < Cri::CommandRunner
227
229
  def run
228
230
  $did_it_work = arguments[0]
@@ -230,7 +232,7 @@ module Cri
230
232
  end
231
233
 
232
234
  runner Cri::CommandDSLTestCaseCommandRunner
233
- EOS
235
+ CMD
234
236
  command = dsl.command
235
237
 
236
238
  # Check
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'helper'
2
4
 
3
5
  module Cri
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'helper'
2
4
 
3
5
  module Cri
@@ -10,22 +12,22 @@ module Cri
10
12
  end
11
13
 
12
14
  def test_simple
13
- expected = <<EOS
14
- NAME
15
- help - show help
16
-
17
- USAGE
18
- help [command_name]
19
-
20
- DESCRIPTION
21
- Show help for the given command, or show general help. When no command is
22
- given, a list of available commands is displayed, as well as a list of
23
- global command-line options. When a command is given, a command
24
- description, as well as command-specific command-line options, are shown.
25
-
26
- OPTIONS
27
- -v --verbose show more detailed help
28
- EOS
15
+ expected = <<~HELP
16
+ NAME
17
+ help - show help
18
+
19
+ USAGE
20
+ help [command_name]
21
+
22
+ DESCRIPTION
23
+ Show help for the given command, or show general help. When no command is
24
+ given, a list of available commands is displayed, as well as a list of
25
+ global command-line options. When a command is given, a command
26
+ description, as well as command-specific command-line options, are shown.
27
+
28
+ OPTIONS
29
+ -v --verbose show more detailed help
30
+ HELP
29
31
 
30
32
  cmd = Cri::Command.new_basic_help
31
33
  assert_equal(expected, help_for(cmd))
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'helper'
2
4
 
3
5
  module Cri
@@ -255,8 +257,7 @@ module Cri
255
257
  parser = Cri::OptionParser.parse(input, definitions)
256
258
 
257
259
  assert_equal({}, parser.options)
258
- assert_equal(['foo', 'bar', '-x', '--yyy', '-abc'], parser.arguments)
259
- assert_equal(['foo', 'bar', '--', '-x', '--yyy', '-abc'], parser.arguments.raw)
260
+ assert_equal(['foo', 'bar', '-x', '--yyy', '-abc'], parser.arguments)
260
261
  end
261
262
 
262
263
  def test_parse_with_end_marker_between_option_key_and_value
@@ -406,5 +407,78 @@ module Cri
406
407
  assert_equal({ aaa: true, bbb: 'xxx', ccc: 'c default' }, parser.options)
407
408
  assert_equal(%w[foo], parser.arguments)
408
409
  end
410
+
411
+ def test_parse_with_transform_proc
412
+ input = %w[--port 123]
413
+ definitions = [
414
+ { long: 'port', short: 'p', argument: :required, transform: ->(x) { Integer(x) } },
415
+ ]
416
+
417
+ parser = Cri::OptionParser.parse(input, definitions)
418
+
419
+ assert_equal({ port: 123 }, parser.options)
420
+ assert_equal([], parser.arguments)
421
+ end
422
+
423
+ def test_parse_with_transform_method
424
+ input = %w[--port 123]
425
+ definitions = [
426
+ { long: 'port', short: 'p', argument: :required, transform: method(:Integer) },
427
+ ]
428
+
429
+ parser = Cri::OptionParser.parse(input, definitions)
430
+
431
+ assert_equal({ port: 123 }, parser.options)
432
+ assert_equal([], parser.arguments)
433
+ end
434
+
435
+ def test_parse_with_transform_object
436
+ port = Class.new do
437
+ def call(str)
438
+ Integer(str)
439
+ end
440
+ end.new
441
+
442
+ input = %w[--port 123]
443
+ definitions = [
444
+ { long: 'port', short: 'p', argument: :required, transform: port },
445
+ ]
446
+
447
+ parser = Cri::OptionParser.parse(input, definitions)
448
+
449
+ assert_equal({ port: 123 }, parser.options)
450
+ assert_equal([], parser.arguments)
451
+ end
452
+
453
+ def test_parse_with_transform_default
454
+ port = Class.new do
455
+ def call(str)
456
+ raise unless str.is_a?(String)
457
+ Integer(str)
458
+ end
459
+ end.new
460
+
461
+ input = %w[]
462
+ definitions = [
463
+ { long: 'port', short: 'p', argument: :required, default: 8080, transform: port },
464
+ ]
465
+
466
+ parser = Cri::OptionParser.parse(input, definitions)
467
+
468
+ assert_equal({ port: 8080 }, parser.options)
469
+ assert_equal([], parser.arguments)
470
+ end
471
+
472
+ def test_parse_with_transform_exception
473
+ input = %w[--port one_hundred_and_twenty_three]
474
+ definitions = [
475
+ { long: 'port', short: 'p', argument: :required, transform: method(:Integer) },
476
+ ]
477
+
478
+ exception = assert_raises(Cri::OptionParser::IllegalOptionValueError) do
479
+ Cri::OptionParser.parse(input, definitions)
480
+ end
481
+ assert_equal('invalid value "one_hundred_and_twenty_three" for --port option', exception.message)
482
+ end
409
483
  end
410
484
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'helper'
2
4
 
3
5
  module Cri
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cri
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.1
4
+ version: 2.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Defreyne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-28 00:00:00.000000000 Z
11
+ date: 2018-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colored
@@ -45,7 +45,7 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files:
47
47
  - LICENSE
48
- - README.adoc
48
+ - README.md
49
49
  - NEWS.md
50
50
  files:
51
51
  - CODE_OF_CONDUCT.md
@@ -53,25 +53,21 @@ files:
53
53
  - Gemfile.lock
54
54
  - LICENSE
55
55
  - NEWS.md
56
- - README.adoc
56
+ - README.md
57
57
  - Rakefile
58
58
  - cri.gemspec
59
59
  - lib/cri.rb
60
- - lib/cri/argument_array.rb
61
60
  - lib/cri/command.rb
62
61
  - lib/cri/command_dsl.rb
63
62
  - lib/cri/command_runner.rb
64
63
  - lib/cri/commands/basic_help.rb
65
64
  - lib/cri/commands/basic_root.rb
66
- - lib/cri/core_ext.rb
67
- - lib/cri/core_ext/string.rb
68
65
  - lib/cri/help_renderer.rb
69
66
  - lib/cri/option_parser.rb
70
67
  - lib/cri/platform.rb
71
68
  - lib/cri/string_formatter.rb
72
69
  - lib/cri/version.rb
73
70
  - test/helper.rb
74
- - test/test_argument_array.rb
75
71
  - test/test_base.rb
76
72
  - test/test_basic_help.rb
77
73
  - test/test_basic_root.rb
@@ -88,14 +84,14 @@ metadata: {}
88
84
  post_install_message:
89
85
  rdoc_options:
90
86
  - "--main"
91
- - README.adoc
87
+ - README.md
92
88
  require_paths:
93
89
  - lib
94
90
  required_ruby_version: !ruby/object:Gem::Requirement
95
91
  requirements:
96
92
  - - "~>"
97
93
  - !ruby/object:Gem::Version
98
- version: '2.1'
94
+ version: '2.3'
99
95
  required_rubygems_version: !ruby/object:Gem::Requirement
100
96
  requirements:
101
97
  - - ">="
@@ -103,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
99
  version: '0'
104
100
  requirements: []
105
101
  rubyforge_project:
106
- rubygems_version: 2.6.14
102
+ rubygems_version: 2.7.7
107
103
  signing_key:
108
104
  specification_version: 4
109
105
  summary: a library for building easy-to-use command-line tools
@@ -1,21 +0,0 @@
1
- module Cri
2
- # Represents an array of arguments. It is an array that strips separator
3
- # arguments (`--`) but provides a `#raw` method to get the raw arguments
4
- # array, i.e. an array that includes the separator `--` arguments.
5
- class ArgumentArray < Array
6
- # Initializes the array using the given raw arguments.
7
- #
8
- # @param [Array<String>] raw_arguments A list of raw arguments, i.e.
9
- # including any separator arguments (`--`).
10
- def initialize(raw_arguments)
11
- super(raw_arguments.reject { |a| a == '--' })
12
- @raw_arguments = raw_arguments
13
- end
14
-
15
- # @return [Array<String>] The arguments, including any separator arguments
16
- # (`--`)
17
- def raw
18
- @raw_arguments
19
- end
20
- end
21
- end
@@ -1,10 +0,0 @@
1
- module Cri
2
- module CoreExtensions
3
- end
4
- end
5
-
6
- require 'cri/core_ext/string'
7
-
8
- class String
9
- include Cri::CoreExtensions::String
10
- end
@@ -1,33 +0,0 @@
1
- require 'colored'
2
-
3
- module Cri
4
- module CoreExtensions
5
- # @deprecated
6
- module String
7
- # @see Cri::StringFormatter#to_paragraphs
8
- def to_paragraphs
9
- Cri::StringFormatter.new.to_paragraphs(self)
10
- end
11
-
12
- # @see Cri::StringFormatter#to_paragraphs
13
- def wrap_and_indent(width, indentation)
14
- Cri::StringFormatter.new.wrap_and_indent(self, width, indentation)
15
- end
16
-
17
- # @see Cri::StringFormatter#format_as_title
18
- def formatted_as_title
19
- Cri::StringFormatter.new.format_as_title(self)
20
- end
21
-
22
- # @see Cri::StringFormatter#format_as_command
23
- def formatted_as_command
24
- Cri::StringFormatter.new.format_as_command(self)
25
- end
26
-
27
- # @see Cri::StringFormatter#format_as_option
28
- def formatted_as_option
29
- Cri::StringFormatter.new.format_as_option(self)
30
- end
31
- end
32
- end
33
- end
@@ -1,11 +0,0 @@
1
- require 'helper'
2
-
3
- module Cri
4
- class ArgumentArrayTestCase < Cri::TestCase
5
- def test_initialize
6
- arr = Cri::ArgumentArray.new(['foo', 'bar', '--', 'baz'])
7
- assert_equal %w[foo bar baz], arr
8
- assert_equal ['foo', 'bar', '--', 'baz'], arr.raw
9
- end
10
- end
11
- end