command_kit 0.1.0.rc1 → 0.1.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/ChangeLog.md +1 -1
- data/README.md +7 -4
- data/gemspec.yml +1 -1
- data/lib/command_kit.rb +1 -0
- data/lib/command_kit/arguments.rb +16 -1
- data/lib/command_kit/arguments/argument.rb +2 -0
- data/lib/command_kit/arguments/argument_value.rb +2 -0
- data/lib/command_kit/colors.rb +32 -0
- data/lib/command_kit/command.rb +5 -0
- data/lib/command_kit/command_name.rb +9 -0
- data/lib/command_kit/commands.rb +30 -0
- data/lib/command_kit/commands/auto_load.rb +16 -0
- data/lib/command_kit/commands/auto_require.rb +16 -0
- data/lib/command_kit/commands/command.rb +3 -0
- data/lib/command_kit/commands/help.rb +2 -0
- data/lib/command_kit/commands/parent_command.rb +7 -0
- data/lib/command_kit/commands/subcommand.rb +12 -0
- data/lib/command_kit/description.rb +12 -1
- data/lib/command_kit/env.rb +4 -0
- data/lib/command_kit/env/home.rb +9 -0
- data/lib/command_kit/env/path.rb +15 -0
- data/lib/command_kit/examples.rb +12 -1
- data/lib/command_kit/exception_handler.rb +4 -0
- data/lib/command_kit/help.rb +7 -1
- data/lib/command_kit/help/man.rb +13 -0
- data/lib/command_kit/inflector.rb +2 -0
- data/lib/command_kit/interactive.rb +62 -1
- data/lib/command_kit/main.rb +11 -0
- data/lib/command_kit/options.rb +12 -0
- data/lib/command_kit/options/option.rb +2 -0
- data/lib/command_kit/options/option_value.rb +2 -0
- data/lib/command_kit/options/parser.rb +29 -0
- data/lib/command_kit/options/quiet.rb +3 -0
- data/lib/command_kit/options/verbose.rb +5 -0
- data/lib/command_kit/options/version.rb +6 -0
- data/lib/command_kit/os.rb +6 -0
- data/lib/command_kit/pager.rb +27 -0
- data/lib/command_kit/printing.rb +23 -0
- data/lib/command_kit/printing/indent.rb +23 -0
- data/lib/command_kit/program_name.rb +7 -0
- data/lib/command_kit/stdio.rb +24 -0
- data/lib/command_kit/terminal.rb +12 -0
- data/lib/command_kit/usage.rb +14 -0
- data/lib/command_kit/version.rb +1 -1
- data/lib/command_kit/xdg.rb +13 -0
- data/spec/arguments/argument_spec.rb +1 -1
- data/spec/arguments_spec.rb +3 -27
- data/spec/colors_spec.rb +21 -13
- data/spec/command_name_spec.rb +1 -1
- data/spec/command_spec.rb +4 -1
- data/spec/commands/auto_load/subcommand_spec.rb +1 -1
- data/spec/commands/auto_load_spec.rb +1 -1
- data/spec/commands/auto_require_spec.rb +2 -2
- data/spec/commands/help_spec.rb +1 -1
- data/spec/commands/parent_command_spec.rb +1 -1
- data/spec/commands/subcommand_spec.rb +1 -1
- data/spec/commands_spec.rb +1 -1
- data/spec/description_spec.rb +1 -25
- data/spec/env/home_spec.rb +1 -1
- data/spec/env/path_spec.rb +1 -1
- data/spec/examples_spec.rb +1 -25
- data/spec/help/man_spec.rb +1 -1
- data/spec/help_spec.rb +0 -25
- data/spec/inflector_spec.rb +1 -1
- data/spec/main_spec.rb +7 -7
- data/spec/options/option_spec.rb +3 -3
- data/spec/options/option_value_spec.rb +1 -1
- data/spec/options_spec.rb +1 -1
- data/spec/os_spec.rb +1 -1
- data/spec/pager_spec.rb +1 -1
- data/spec/printing/indent_spec.rb +1 -1
- data/spec/printing_spec.rb +10 -2
- data/spec/program_name_spec.rb +1 -1
- data/spec/spec_helper.rb +0 -3
- data/spec/terminal_spec.rb +1 -1
- data/spec/usage_spec.rb +1 -1
- data/spec/xdg_spec.rb +1 -1
- metadata +3 -3
data/lib/command_kit/main.rb
CHANGED
@@ -12,6 +12,9 @@ module CommandKit
|
|
12
12
|
# end
|
13
13
|
#
|
14
14
|
module Main
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
#
|
15
18
|
module ModuleMethods
|
16
19
|
#
|
17
20
|
# Extends {ClassMethods} or {ModuleMethods}, depending on whether {Main}
|
@@ -43,6 +46,8 @@ module CommandKit
|
|
43
46
|
# @param [Array<String>] argv
|
44
47
|
# The Array of command-line arguments.
|
45
48
|
#
|
49
|
+
# @api public
|
50
|
+
#
|
46
51
|
def start(argv=ARGV, **kwargs)
|
47
52
|
begin
|
48
53
|
exit main(argv, **kwargs)
|
@@ -68,6 +73,8 @@ module CommandKit
|
|
68
73
|
# @return [Integer]
|
69
74
|
# The exit status of the command.
|
70
75
|
#
|
76
|
+
# @api public
|
77
|
+
#
|
71
78
|
def main(argv=[], **kwargs)
|
72
79
|
new(**kwargs).main(argv)
|
73
80
|
end
|
@@ -84,6 +91,8 @@ module CommandKit
|
|
84
91
|
#
|
85
92
|
# @note `argv` is splatted into {#run}.
|
86
93
|
#
|
94
|
+
# @api public
|
95
|
+
#
|
87
96
|
def main(argv=[])
|
88
97
|
run(*argv)
|
89
98
|
return 0
|
@@ -99,6 +108,8 @@ module CommandKit
|
|
99
108
|
#
|
100
109
|
# @abstract
|
101
110
|
#
|
111
|
+
# @api public
|
112
|
+
#
|
102
113
|
def run(*args)
|
103
114
|
end
|
104
115
|
end
|
data/lib/command_kit/options.rb
CHANGED
@@ -38,6 +38,9 @@ module CommandKit
|
|
38
38
|
module Options
|
39
39
|
include Parser
|
40
40
|
|
41
|
+
#
|
42
|
+
# @api private
|
43
|
+
#
|
41
44
|
module ModuleMethods
|
42
45
|
#
|
43
46
|
# Extends {ClassMethods} or {ModuleMethods}, depending on whether
|
@@ -68,6 +71,8 @@ module CommandKit
|
|
68
71
|
#
|
69
72
|
# @return [Hash{Symbol => Option}]
|
70
73
|
#
|
74
|
+
# @api semipublic
|
75
|
+
#
|
71
76
|
def options
|
72
77
|
@options ||= if superclass.kind_of?(ClassMethods)
|
73
78
|
superclass.options.dup
|
@@ -172,6 +177,8 @@ module CommandKit
|
|
172
177
|
# # ...
|
173
178
|
# end
|
174
179
|
#
|
180
|
+
# @api public
|
181
|
+
#
|
175
182
|
def option(name,**kwargs,&block)
|
176
183
|
options[name] = Option.new(name,**kwargs,&block)
|
177
184
|
end
|
@@ -180,6 +187,9 @@ module CommandKit
|
|
180
187
|
# Hash of parsed option values.
|
181
188
|
#
|
182
189
|
# @return [Hash{Symbol => Object}]
|
190
|
+
#
|
191
|
+
# @api semipublic
|
192
|
+
#
|
183
193
|
attr_reader :options
|
184
194
|
|
185
195
|
#
|
@@ -193,6 +203,8 @@ module CommandKit
|
|
193
203
|
# The {#option_parser} will populate {#options} and also call any
|
194
204
|
# {ClassMethods#option option} blocks with the parsed option values.
|
195
205
|
#
|
206
|
+
# @api public
|
207
|
+
#
|
196
208
|
def initialize(options: {}, **kwargs)
|
197
209
|
@options = options
|
198
210
|
|
@@ -33,6 +33,9 @@ module CommandKit
|
|
33
33
|
include Main
|
34
34
|
include Printing
|
35
35
|
|
36
|
+
#
|
37
|
+
# @api private
|
38
|
+
#
|
36
39
|
module ModuleMethods
|
37
40
|
#
|
38
41
|
# Sets {CommandKit::Usage::ClassMethods#usage .usage} or extends
|
@@ -58,6 +61,8 @@ module CommandKit
|
|
58
61
|
# The option parser.
|
59
62
|
#
|
60
63
|
# @return [OptionParser]
|
64
|
+
#
|
65
|
+
# @api semipublic
|
61
66
|
attr_reader :option_parser
|
62
67
|
|
63
68
|
#
|
@@ -65,6 +70,8 @@ module CommandKit
|
|
65
70
|
#
|
66
71
|
# @return [OptionParser]
|
67
72
|
#
|
73
|
+
# @api public
|
74
|
+
#
|
68
75
|
def initialize(**kwargs)
|
69
76
|
super(**kwargs)
|
70
77
|
|
@@ -91,6 +98,8 @@ module CommandKit
|
|
91
98
|
# @return [Integer]
|
92
99
|
# The exit status code.
|
93
100
|
#
|
101
|
+
# @api public
|
102
|
+
#
|
94
103
|
def main(argv=[])
|
95
104
|
super(parse_options(argv))
|
96
105
|
rescue SystemExit => system_exit
|
@@ -106,6 +115,8 @@ module CommandKit
|
|
106
115
|
# @return [Array<String>]
|
107
116
|
# The remaining non-option arguments.
|
108
117
|
#
|
118
|
+
# @api semipublic
|
119
|
+
#
|
109
120
|
def parse_options(argv)
|
110
121
|
begin
|
111
122
|
option_parser.parse(argv)
|
@@ -132,6 +143,8 @@ module CommandKit
|
|
132
143
|
# @param [OptionParser::ParseError] error
|
133
144
|
# The error from `OptionParser`.
|
134
145
|
#
|
146
|
+
# @api semipublic
|
147
|
+
#
|
135
148
|
def on_parse_error(error)
|
136
149
|
print_error("#{command_name}: #{error.message}")
|
137
150
|
print_error("Try '#{command_name} --help' for more information.")
|
@@ -145,6 +158,8 @@ module CommandKit
|
|
145
158
|
#
|
146
159
|
# @see on_parse_error
|
147
160
|
#
|
161
|
+
# @api semipublic
|
162
|
+
#
|
148
163
|
def on_invalid_option(error)
|
149
164
|
on_parse_error(error)
|
150
165
|
end
|
@@ -157,6 +172,8 @@ module CommandKit
|
|
157
172
|
#
|
158
173
|
# @see on_parse_error
|
159
174
|
#
|
175
|
+
# @api semipublic
|
176
|
+
#
|
160
177
|
def on_ambiguous_option(error)
|
161
178
|
on_parse_error(error)
|
162
179
|
end
|
@@ -169,6 +186,8 @@ module CommandKit
|
|
169
186
|
#
|
170
187
|
# @see on_parse_error
|
171
188
|
#
|
189
|
+
# @api semipublic
|
190
|
+
#
|
172
191
|
def on_invalid_argument(error)
|
173
192
|
on_parse_error(error)
|
174
193
|
end
|
@@ -181,6 +200,8 @@ module CommandKit
|
|
181
200
|
#
|
182
201
|
# @see on_parse_error
|
183
202
|
#
|
203
|
+
# @api semipublic
|
204
|
+
#
|
184
205
|
def on_missing_argument(error)
|
185
206
|
on_parse_error(error)
|
186
207
|
end
|
@@ -193,6 +214,8 @@ module CommandKit
|
|
193
214
|
#
|
194
215
|
# @see on_parse_error
|
195
216
|
#
|
217
|
+
# @api semipublic
|
218
|
+
#
|
196
219
|
def on_needless_argument(error)
|
197
220
|
on_parse_error(error)
|
198
221
|
end
|
@@ -205,6 +228,8 @@ module CommandKit
|
|
205
228
|
#
|
206
229
|
# @see on_parse_error
|
207
230
|
#
|
231
|
+
# @api semipublic
|
232
|
+
#
|
208
233
|
def on_ambiguous_argument(error)
|
209
234
|
on_parse_error(error)
|
210
235
|
end
|
@@ -212,6 +237,8 @@ module CommandKit
|
|
212
237
|
#
|
213
238
|
# Prints the `--help` output.
|
214
239
|
#
|
240
|
+
# @api semipublic
|
241
|
+
#
|
215
242
|
def help_options
|
216
243
|
puts option_parser
|
217
244
|
end
|
@@ -219,6 +246,8 @@ module CommandKit
|
|
219
246
|
#
|
220
247
|
# @see #help_options
|
221
248
|
#
|
249
|
+
# @api public
|
250
|
+
#
|
222
251
|
def help
|
223
252
|
help_options
|
224
253
|
end
|
@@ -18,6 +18,9 @@ module CommandKit
|
|
18
18
|
module Verbose
|
19
19
|
include Options
|
20
20
|
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
#
|
21
24
|
module ModuleMethods
|
22
25
|
#
|
23
26
|
# Defines a `-v, --verbose` option or extends {ModuleMethods}, depending
|
@@ -47,6 +50,8 @@ module CommandKit
|
|
47
50
|
#
|
48
51
|
# @return [Boolean]
|
49
52
|
#
|
53
|
+
# @api public
|
54
|
+
#
|
50
55
|
def verbose?
|
51
56
|
@verbose
|
52
57
|
end
|
@@ -35,6 +35,8 @@ module CommandKit
|
|
35
35
|
# @return [String, nil]
|
36
36
|
# The classes version string.
|
37
37
|
#
|
38
|
+
# @api public
|
39
|
+
#
|
38
40
|
def version(new_version=nil)
|
39
41
|
if new_version
|
40
42
|
@version = new_version
|
@@ -47,6 +49,8 @@ module CommandKit
|
|
47
49
|
#
|
48
50
|
# @see ClassMethods#version
|
49
51
|
#
|
52
|
+
# @api public
|
53
|
+
#
|
50
54
|
def version
|
51
55
|
self.class.version
|
52
56
|
end
|
@@ -54,6 +58,8 @@ module CommandKit
|
|
54
58
|
#
|
55
59
|
# Prints the version.
|
56
60
|
#
|
61
|
+
# @api public
|
62
|
+
#
|
57
63
|
def print_version
|
58
64
|
puts "#{command_name} #{version}"
|
59
65
|
end
|
data/lib/command_kit/os.rb
CHANGED
@@ -22,6 +22,8 @@ module CommandKit
|
|
22
22
|
#
|
23
23
|
# @return [Boolean]
|
24
24
|
#
|
25
|
+
# @api public
|
26
|
+
#
|
25
27
|
def linux?
|
26
28
|
RUBY_PLATFORM.include?('linux')
|
27
29
|
end
|
@@ -31,6 +33,8 @@ module CommandKit
|
|
31
33
|
#
|
32
34
|
# @return [Boolean]
|
33
35
|
#
|
36
|
+
# @api public
|
37
|
+
#
|
34
38
|
def macos?
|
35
39
|
RUBY_PLATFORM.include?('darwin')
|
36
40
|
end
|
@@ -40,6 +44,8 @@ module CommandKit
|
|
40
44
|
#
|
41
45
|
# @return [Boolean]
|
42
46
|
#
|
47
|
+
# @api public
|
48
|
+
#
|
43
49
|
def windows?
|
44
50
|
Gem.win_platform?
|
45
51
|
end
|
data/lib/command_kit/pager.rb
CHANGED
@@ -53,6 +53,8 @@ module CommandKit
|
|
53
53
|
# Respects the `PAGER` env variable, or attemps to find `less` or
|
54
54
|
# `more` by searching the `PATH` env variable.
|
55
55
|
#
|
56
|
+
# @api public
|
57
|
+
#
|
56
58
|
def initialize(**kwargs)
|
57
59
|
super(**kwargs)
|
58
60
|
|
@@ -74,6 +76,23 @@ module CommandKit
|
|
74
76
|
# @yieldparam [IO]
|
75
77
|
# The IO pipe to the pager.
|
76
78
|
#
|
79
|
+
# @example
|
80
|
+
# pager do |io|
|
81
|
+
# io.puts "Hello world"
|
82
|
+
# # ...
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# @example Piping a command into the pager:
|
86
|
+
# IO.peopn(["ping", ip]) do |ping|
|
87
|
+
# pager do |io|
|
88
|
+
# ping.each_line do |line|
|
89
|
+
# io.write line
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# @api public
|
95
|
+
#
|
77
96
|
def pager
|
78
97
|
if !stdout.tty? || @pager.nil?
|
79
98
|
# fallback to stdout if the process does not have a terminal or we could
|
@@ -105,6 +124,14 @@ module CommandKit
|
|
105
124
|
# @param [Array<String>, #to_s] data
|
106
125
|
# The data to print.
|
107
126
|
#
|
127
|
+
# @example
|
128
|
+
# print_or_page(data)
|
129
|
+
#
|
130
|
+
# @example Print or pages the contents of a file:
|
131
|
+
# print_or_page(File.read(file))
|
132
|
+
#
|
133
|
+
# @api public
|
134
|
+
#
|
108
135
|
def print_or_page(data)
|
109
136
|
line_count = case data
|
110
137
|
when Array then data.length
|
data/lib/command_kit/printing.rb
CHANGED
@@ -9,12 +9,24 @@ module CommandKit
|
|
9
9
|
module Printing
|
10
10
|
include Stdio
|
11
11
|
|
12
|
+
# Platform independency new-line constant
|
13
|
+
#
|
14
|
+
# @return [String]
|
15
|
+
#
|
16
|
+
# @api public
|
17
|
+
EOL = $/
|
18
|
+
|
12
19
|
#
|
13
20
|
# Prints the error message to {Stdio#stderr stderr}.
|
14
21
|
#
|
15
22
|
# @param [String] message
|
16
23
|
# The error message.
|
17
24
|
#
|
25
|
+
# @example
|
26
|
+
# print_error "Error: invalid input"
|
27
|
+
#
|
28
|
+
# @api public
|
29
|
+
#
|
18
30
|
def print_error(message)
|
19
31
|
stderr.puts message
|
20
32
|
end
|
@@ -25,6 +37,17 @@ module CommandKit
|
|
25
37
|
# @param [Exception] error
|
26
38
|
# The error to print.
|
27
39
|
#
|
40
|
+
# @example
|
41
|
+
# begin
|
42
|
+
# # ...
|
43
|
+
# rescue => error
|
44
|
+
# print_error "Error encountered"
|
45
|
+
# print_exception(error)
|
46
|
+
# exit(1)
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# @api public
|
50
|
+
#
|
28
51
|
def print_exception(error)
|
29
52
|
print_error error.full_message(highlight: stderr.tty?)
|
30
53
|
end
|
@@ -42,6 +42,27 @@ module CommandKit
|
|
42
42
|
# @return [Integer]
|
43
43
|
# If no block is given, the indententation level will be returned.
|
44
44
|
#
|
45
|
+
# @example
|
46
|
+
# puts "values:"
|
47
|
+
# indent do
|
48
|
+
# values.each do |key,value|
|
49
|
+
# puts "#{key}: #{value}"
|
50
|
+
# end
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# @example
|
54
|
+
# puts "Code:"
|
55
|
+
# puts
|
56
|
+
# puts "```"
|
57
|
+
# indent(4) do
|
58
|
+
# code.each_line do |line|
|
59
|
+
# puts line
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
# puts "```"
|
63
|
+
#
|
64
|
+
# @api public
|
65
|
+
#
|
45
66
|
def indent(n=2)
|
46
67
|
if block_given?
|
47
68
|
begin
|
@@ -63,6 +84,8 @@ module CommandKit
|
|
63
84
|
# @param [Array<String>] lines
|
64
85
|
# The lines to indent and print.
|
65
86
|
#
|
87
|
+
# @api public
|
88
|
+
#
|
66
89
|
def puts(*lines)
|
67
90
|
if (@indent > 0 && !lines.empty?)
|
68
91
|
padding = " " * @indent
|
@@ -3,6 +3,9 @@ module CommandKit
|
|
3
3
|
# Retrieves the current program name (`$PROGRAM_NAME`).
|
4
4
|
#
|
5
5
|
module ProgramName
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
#
|
6
9
|
module ModuleMethods
|
7
10
|
#
|
8
11
|
# Extends {ClassMethods} or {ModuleMethods}, depending on whether
|
@@ -42,6 +45,8 @@ module CommandKit
|
|
42
45
|
# The `$PROGRAM_NAME` or `nil` if the `$PROGRAM_NAME` is `-e`, `irb`,
|
43
46
|
# or `rspec`.
|
44
47
|
#
|
48
|
+
# @api semipublic
|
49
|
+
#
|
45
50
|
def program_name
|
46
51
|
$PROGRAM_NAME unless IGNORED_PROGRAM_NAMES.include?($PROGRAM_NAME)
|
47
52
|
end
|
@@ -50,6 +55,8 @@ module CommandKit
|
|
50
55
|
#
|
51
56
|
# @see ClassMethods#program_name
|
52
57
|
#
|
58
|
+
# @api public
|
59
|
+
#
|
53
60
|
def program_name
|
54
61
|
self.class.program_name
|
55
62
|
end
|