cli-kit 5.1.0 → 5.2.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/.rubocop.sorbet.yml +1 -0
- data/.rubocop.yml +1 -2
- data/Gemfile +1 -1
- data/Gemfile.lock +22 -24
- data/Rakefile +0 -1
- data/bin/testunit +0 -1
- data/examples/minimal/example.rb +1 -1
- data/examples/single-file/example.rb +4 -3
- data/gen/lib/gen/commands/help.rb +1 -3
- data/gen/lib/gen/commands/new.rb +2 -6
- data/gen/lib/gen/commands.rb +1 -7
- data/gen/lib/gen/entry_point.rb +1 -5
- data/gen/lib/gen/generator.rb +15 -19
- data/gen/lib/gen/help.rb +3 -7
- data/lib/cli/kit/args/definition.rb +37 -95
- data/lib/cli/kit/args/evaluation.rb +40 -59
- data/lib/cli/kit/args/parser/node.rb +19 -23
- data/lib/cli/kit/args/parser.rb +12 -16
- data/lib/cli/kit/args/tokenizer.rb +15 -18
- data/lib/cli/kit/base_command.rb +4 -8
- data/lib/cli/kit/command_help.rb +24 -27
- data/lib/cli/kit/command_registry.rb +40 -36
- data/lib/cli/kit/config.rb +14 -15
- data/lib/cli/kit/core_ext.rb +4 -6
- data/lib/cli/kit/error_handler.rb +17 -33
- data/lib/cli/kit/executor.rb +7 -16
- data/lib/cli/kit/ini.rb +8 -12
- data/lib/cli/kit/levenshtein.rb +7 -5
- data/lib/cli/kit/logger.rb +8 -10
- data/lib/cli/kit/opts.rb +26 -86
- data/lib/cli/kit/parse_args.rb +5 -10
- data/lib/cli/kit/resolver.rb +4 -6
- data/lib/cli/kit/support/test_helper.rb +10 -5
- data/lib/cli/kit/system.rb +25 -114
- data/lib/cli/kit/util.rb +15 -31
- data/lib/cli/kit/version.rb +1 -1
- data/lib/cli/kit.rb +17 -35
- metadata +1 -2
- data/lib/cli/kit/sorbet_runtime_stub.rb +0 -154
data/lib/cli/kit/util.rb
CHANGED
@@ -6,21 +6,17 @@ module CLI
|
|
6
6
|
module Kit
|
7
7
|
module Util
|
8
8
|
class << self
|
9
|
-
extend T::Sig
|
10
9
|
#
|
11
10
|
# Converts an integer representing bytes into a human readable format
|
12
11
|
#
|
13
|
-
|
12
|
+
#: (Integer bytes, ?precision: Integer, ?space: bool) -> String
|
14
13
|
def to_filesize(bytes, precision: 2, space: false)
|
15
14
|
to_si_scale(bytes, 'B', precision: precision, space: space, factor: 1024)
|
16
15
|
end
|
17
16
|
|
18
17
|
# Converts a number to a human readable format on the SI scale
|
19
18
|
#
|
20
|
-
|
21
|
-
params(number: Numeric, unit: String, factor: Integer, precision: Integer, space: T::Boolean)
|
22
|
-
.returns(String)
|
23
|
-
end
|
19
|
+
#: (Numeric number, ?String unit, ?factor: Integer, ?precision: Integer, ?space: bool) -> String
|
24
20
|
def to_si_scale(number, unit = '', factor: 1000, precision: 2, space: false)
|
25
21
|
raise ArgumentError, 'factor should only be 1000 or 1024' unless [1000, 1024].include?(factor)
|
26
22
|
|
@@ -37,11 +33,11 @@ module CLI
|
|
37
33
|
if number < 1
|
38
34
|
index = [-scale - 1, small_scale.length].min
|
39
35
|
scale = -(index + 1)
|
40
|
-
prefix =
|
36
|
+
prefix = small_scale[index] #: as !nil
|
41
37
|
else
|
42
38
|
index = [scale - 1, big_scale.length].min
|
43
39
|
scale = index + 1
|
44
|
-
prefix =
|
40
|
+
prefix = big_scale[index] #: as !nil
|
45
41
|
end
|
46
42
|
end
|
47
43
|
|
@@ -62,10 +58,7 @@ module CLI
|
|
62
58
|
# Dir.chdir, when invoked in block form, complains when we call chdir
|
63
59
|
# again recursively. There's no apparent good reason for this, so we
|
64
60
|
# simply implement our own block form of Dir.chdir here.
|
65
|
-
|
66
|
-
type_parameters(:T).params(dir: String, block: T.proc.returns(T.type_parameter(:T)))
|
67
|
-
.returns(T.type_parameter(:T))
|
68
|
-
end
|
61
|
+
#: [T] (String dir) { -> T } -> T
|
69
62
|
def with_dir(dir, &block)
|
70
63
|
prev = Dir.pwd
|
71
64
|
begin
|
@@ -85,44 +78,35 @@ module CLI
|
|
85
78
|
# end.retry_after(ExpectedError) do
|
86
79
|
# costly_prep()
|
87
80
|
# end
|
88
|
-
|
89
|
-
type_parameters(:T).params(block_that_might_raise: T.proc.returns(T.type_parameter(:T)))
|
90
|
-
.returns(Retrier[T.type_parameter(:T)])
|
91
|
-
end
|
81
|
+
#: [T] { -> T } -> Retrier[T]
|
92
82
|
def begin(&block_that_might_raise)
|
93
83
|
Retrier.new(block_that_might_raise)
|
94
84
|
end
|
95
85
|
end
|
96
86
|
|
87
|
+
#: [BlockReturnType]
|
97
88
|
class Retrier
|
98
|
-
|
99
|
-
extend T::Generic
|
100
|
-
|
101
|
-
BlockReturnType = type_member
|
102
|
-
|
103
|
-
sig { params(block_that_might_raise: T.proc.returns(BlockReturnType)).void }
|
89
|
+
#: (^-> BlockReturnType block_that_might_raise) -> void
|
104
90
|
def initialize(block_that_might_raise)
|
105
91
|
@block_that_might_raise = block_that_might_raise
|
106
92
|
end
|
107
93
|
|
108
|
-
|
109
|
-
params(
|
110
|
-
exception: T.class_of(Exception),
|
111
|
-
retries: Integer,
|
112
|
-
before_retry: T.nilable(T.proc.params(e: Exception).void),
|
113
|
-
).returns(BlockReturnType)
|
114
|
-
end
|
94
|
+
#: (?singleton(Exception) exception, ?retries: Integer) ?{ (Exception e) -> void } -> BlockReturnType
|
115
95
|
def retry_after(exception = StandardError, retries: 1, &before_retry)
|
116
96
|
@block_that_might_raise.call
|
117
97
|
rescue exception => e
|
118
98
|
raise if (retries -= 1) < 0
|
119
99
|
|
120
100
|
if before_retry
|
101
|
+
# rubocop:disable Style/IdenticalConditionalBranches
|
121
102
|
if before_retry.arity == 0
|
122
|
-
|
103
|
+
prc = before_retry #: as ^() -> void
|
104
|
+
prc.call
|
123
105
|
else
|
124
|
-
|
106
|
+
prc = before_retry #: as ^(Exception) -> void
|
107
|
+
prc.call(e)
|
125
108
|
end
|
109
|
+
# rubocop:enable Style/IdenticalConditionalBranches
|
126
110
|
end
|
127
111
|
retry
|
128
112
|
end
|
data/lib/cli/kit/version.rb
CHANGED
data/lib/cli/kit.rb
CHANGED
@@ -1,17 +1,10 @@
|
|
1
1
|
# typed: true
|
2
2
|
|
3
3
|
require 'cli/ui'
|
4
|
-
|
5
|
-
unless defined?(T)
|
6
|
-
require('cli/kit/sorbet_runtime_stub')
|
7
|
-
end
|
8
|
-
|
9
4
|
require 'cli/kit/core_ext'
|
10
5
|
|
11
6
|
module CLI
|
12
7
|
module Kit
|
13
|
-
extend T::Sig
|
14
|
-
|
15
8
|
autoload :Args, 'cli/kit/args'
|
16
9
|
autoload :BaseCommand, 'cli/kit/base_command'
|
17
10
|
autoload :CommandRegistry, 'cli/kit/command_registry'
|
@@ -33,6 +26,9 @@ module CLI
|
|
33
26
|
EXIT_BUG = 1
|
34
27
|
EXIT_SUCCESS = 0
|
35
28
|
|
29
|
+
UNTYPED_NIL = nil #: untyped
|
30
|
+
private_constant(:UNTYPED_NIL)
|
31
|
+
|
36
32
|
# Abort, Bug, AbortSilent, and BugSilent are four ways of immediately bailing
|
37
33
|
# on command-line execution when an unrecoverable error occurs.
|
38
34
|
#
|
@@ -76,9 +72,7 @@ module CLI
|
|
76
72
|
GenericAbort = Class.new(Exception) # rubocop:disable Lint/InheritException
|
77
73
|
|
78
74
|
class Abort < GenericAbort # bug:false; silent: false
|
79
|
-
|
80
|
-
|
81
|
-
sig { returns(T::Boolean) }
|
75
|
+
#: -> bool
|
82
76
|
def bug?
|
83
77
|
false
|
84
78
|
end
|
@@ -88,31 +82,25 @@ module CLI
|
|
88
82
|
end
|
89
83
|
|
90
84
|
class BugSilent < GenericAbort # bug:true; silent:true
|
91
|
-
|
92
|
-
|
93
|
-
sig { returns(T::Boolean) }
|
85
|
+
#: -> bool
|
94
86
|
def silent?
|
95
87
|
true
|
96
88
|
end
|
97
89
|
end
|
98
90
|
|
99
91
|
class AbortSilent < GenericAbort # bug:false; silent:true
|
100
|
-
|
101
|
-
|
102
|
-
sig { returns(T::Boolean) }
|
92
|
+
#: -> bool
|
103
93
|
def bug?
|
104
94
|
false
|
105
95
|
end
|
106
96
|
|
107
|
-
|
97
|
+
#: -> bool
|
108
98
|
def silent?
|
109
99
|
true
|
110
100
|
end
|
111
101
|
end
|
112
102
|
|
113
103
|
class << self
|
114
|
-
extend T::Sig
|
115
|
-
|
116
104
|
# Mirrors the API of Kernel#raise, but with the addition of a few new
|
117
105
|
# optional keyword arguments. `bug` and `silent` attach metadata to the
|
118
106
|
# exception being raised, which is interpreted later in the ErrorHandler to
|
@@ -121,31 +109,25 @@ module CLI
|
|
121
109
|
# `depth` is used to trim leading elements of the backtrace. If you wrap
|
122
110
|
# this method in your own wrapper, you'll want to pass `depth: 2`, for
|
123
111
|
# example.
|
124
|
-
|
125
|
-
params(
|
126
|
-
exception: T.any(Class, String, Exception),
|
127
|
-
string: T.untyped,
|
128
|
-
array: T.nilable(T::Array[String]),
|
129
|
-
cause: T.nilable(Exception),
|
130
|
-
bug: T.nilable(T::Boolean),
|
131
|
-
silent: T.nilable(T::Boolean),
|
132
|
-
depth: Integer,
|
133
|
-
).returns(T.noreturn)
|
134
|
-
end
|
112
|
+
#: (?(Class | String | Exception) exception, ?untyped string, ?Array[String]? array, ?cause: Exception?, ?bug: bool?, ?silent: bool?, ?depth: Integer) -> bot
|
135
113
|
def raise(
|
136
114
|
# default arguments
|
137
|
-
exception =
|
115
|
+
exception = UNTYPED_NIL,
|
116
|
+
string = UNTYPED_NIL,
|
117
|
+
array = UNTYPED_NIL,
|
118
|
+
cause: $ERROR_INFO,
|
138
119
|
# new arguments
|
139
120
|
bug: nil, silent: nil, depth: 1
|
140
121
|
)
|
122
|
+
k = Kernel #: as untyped
|
141
123
|
if array
|
142
|
-
|
124
|
+
k.raise(exception, string, array, cause: cause)
|
143
125
|
elsif string
|
144
|
-
|
126
|
+
k.raise(exception, string, Kernel.caller(depth), cause: cause)
|
145
127
|
elsif exception.is_a?(String)
|
146
|
-
|
128
|
+
k.raise(RuntimeError, exception, Kernel.caller(depth), cause: cause)
|
147
129
|
else
|
148
|
-
|
130
|
+
k.raise(exception, exception.message, Kernel.caller(depth), cause: cause)
|
149
131
|
end
|
150
132
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
151
133
|
e.bug!(bug) unless bug.nil?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cli-kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Burke Libbey
|
@@ -148,7 +148,6 @@ files:
|
|
148
148
|
- lib/cli/kit/opts.rb
|
149
149
|
- lib/cli/kit/parse_args.rb
|
150
150
|
- lib/cli/kit/resolver.rb
|
151
|
-
- lib/cli/kit/sorbet_runtime_stub.rb
|
152
151
|
- lib/cli/kit/support.rb
|
153
152
|
- lib/cli/kit/support/test_helper.rb
|
154
153
|
- lib/cli/kit/system.rb
|
@@ -1,154 +0,0 @@
|
|
1
|
-
# typed: ignore
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module T
|
5
|
-
class << self
|
6
|
-
def absurd(value); end
|
7
|
-
def all(type_a, type_b, *types); end
|
8
|
-
def any(type_a, type_b, *types); end
|
9
|
-
def attached_class; end
|
10
|
-
def class_of(klass); end
|
11
|
-
def enum(values); end
|
12
|
-
def nilable(type); end
|
13
|
-
def noreturn; end
|
14
|
-
def self_type; end
|
15
|
-
def type_alias(type = nil, &_blk); end
|
16
|
-
def type_parameter(name); end
|
17
|
-
def untyped; end
|
18
|
-
|
19
|
-
def assert_type!(value, _type, _checked: true)
|
20
|
-
value
|
21
|
-
end
|
22
|
-
|
23
|
-
def cast(value, _type, _checked: true)
|
24
|
-
value
|
25
|
-
end
|
26
|
-
|
27
|
-
def let(value, _type, _checked: true)
|
28
|
-
value
|
29
|
-
end
|
30
|
-
|
31
|
-
def must(arg, _msg = nil)
|
32
|
-
arg
|
33
|
-
end
|
34
|
-
|
35
|
-
def proc
|
36
|
-
T::Proc.new
|
37
|
-
end
|
38
|
-
|
39
|
-
def reveal_type(value)
|
40
|
-
value
|
41
|
-
end
|
42
|
-
|
43
|
-
def unsafe(value)
|
44
|
-
value
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
module Sig
|
49
|
-
def sig(arg0 = nil, &blk); end
|
50
|
-
end
|
51
|
-
|
52
|
-
module Helpers
|
53
|
-
def abstract!; end
|
54
|
-
def interface!; end
|
55
|
-
def final!; end
|
56
|
-
def sealed!; end
|
57
|
-
def mixes_in_class_methods(mod); end
|
58
|
-
end
|
59
|
-
|
60
|
-
module Generic
|
61
|
-
include(T::Helpers)
|
62
|
-
|
63
|
-
def type_parameters(*params); end
|
64
|
-
def type_member(variance = :invariant, fixed: nil, lower: nil, upper: BasicObject); end
|
65
|
-
def type_template(variance = :invariant, fixed: nil, lower: nil, upper: BasicObject); end
|
66
|
-
|
67
|
-
def [](*types)
|
68
|
-
self
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
module Array
|
73
|
-
class << self
|
74
|
-
def [](type); end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
Boolean = Object.new.freeze
|
79
|
-
|
80
|
-
module Configuration
|
81
|
-
class << self
|
82
|
-
def call_validation_error_handler(signature, opts); end
|
83
|
-
def call_validation_error_handler=(value); end
|
84
|
-
def default_checked_level=(default_checked_level); end
|
85
|
-
def enable_checking_for_sigs_marked_checked_tests; end
|
86
|
-
def enable_final_checks_on_hooks; end
|
87
|
-
def enable_legacy_t_enum_migration_mode; end
|
88
|
-
def reset_final_checks_on_hooks; end
|
89
|
-
def hard_assert_handler(str, extra); end
|
90
|
-
def hard_assert_handler=(value); end
|
91
|
-
def inline_type_error_handler(error); end
|
92
|
-
def inline_type_error_handler=(value); end
|
93
|
-
def log_info_handler(str, extra); end
|
94
|
-
def log_info_handler=(value); end
|
95
|
-
def scalar_types; end
|
96
|
-
def scalar_types=(values); end
|
97
|
-
def sealed_violation_whitelist; end
|
98
|
-
def sealed_violation_whitelist=(sealed_violation_whitelist); end
|
99
|
-
def sig_builder_error_handler=(value); end
|
100
|
-
def sig_validation_error_handler(error, opts); end
|
101
|
-
def sig_validation_error_handler=(value); end
|
102
|
-
def soft_assert_handler(str, extra); end
|
103
|
-
def soft_assert_handler=(value); end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
module Enumerable
|
108
|
-
class << self
|
109
|
-
def [](type); end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
module Enumerator
|
114
|
-
class << self
|
115
|
-
def [](type); end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
module Hash
|
120
|
-
class << self
|
121
|
-
def [](keys, values); end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
class Proc
|
126
|
-
def bind(*_)
|
127
|
-
self
|
128
|
-
end
|
129
|
-
|
130
|
-
def params(*_param)
|
131
|
-
self
|
132
|
-
end
|
133
|
-
|
134
|
-
def void
|
135
|
-
self
|
136
|
-
end
|
137
|
-
|
138
|
-
def returns(_type)
|
139
|
-
self
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
module Range
|
144
|
-
class << self
|
145
|
-
def [](type); end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
module Set
|
150
|
-
class << self
|
151
|
-
def [](type); end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|