command_kit 0.1.0.pre1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +15 -0
  3. data/.rubocop.yml +138 -0
  4. data/ChangeLog.md +34 -2
  5. data/Gemfile +3 -0
  6. data/README.md +135 -214
  7. data/Rakefile +3 -2
  8. data/command_kit.gemspec +4 -4
  9. data/examples/colors.rb +30 -0
  10. data/examples/command.rb +65 -0
  11. data/examples/pager.rb +30 -0
  12. data/gemspec.yml +10 -2
  13. data/lib/command_kit/arguments/argument.rb +16 -44
  14. data/lib/command_kit/arguments/argument_value.rb +3 -30
  15. data/lib/command_kit/arguments.rb +66 -20
  16. data/lib/command_kit/colors.rb +253 -45
  17. data/lib/command_kit/command.rb +50 -3
  18. data/lib/command_kit/command_name.rb +9 -0
  19. data/lib/command_kit/commands/auto_load/subcommand.rb +3 -0
  20. data/lib/command_kit/commands/auto_load.rb +16 -0
  21. data/lib/command_kit/commands/auto_require.rb +16 -0
  22. data/lib/command_kit/commands/command.rb +3 -0
  23. data/lib/command_kit/commands/help.rb +2 -0
  24. data/lib/command_kit/commands/parent_command.rb +7 -0
  25. data/lib/command_kit/commands/subcommand.rb +15 -0
  26. data/lib/command_kit/commands.rb +40 -4
  27. data/lib/command_kit/description.rb +15 -2
  28. data/lib/command_kit/env/home.rb +9 -0
  29. data/lib/command_kit/env/path.rb +15 -0
  30. data/lib/command_kit/env.rb +4 -0
  31. data/lib/command_kit/examples.rb +15 -2
  32. data/lib/command_kit/exception_handler.rb +4 -0
  33. data/lib/command_kit/help/man.rb +74 -47
  34. data/lib/command_kit/help.rb +10 -1
  35. data/lib/command_kit/inflector.rb +49 -17
  36. data/lib/command_kit/interactive.rb +239 -0
  37. data/lib/command_kit/main.rb +20 -9
  38. data/lib/command_kit/man.rb +44 -0
  39. data/lib/command_kit/open_app.rb +69 -0
  40. data/lib/command_kit/options/option.rb +36 -9
  41. data/lib/command_kit/options/option_value.rb +42 -3
  42. data/lib/command_kit/options/parser.rb +44 -17
  43. data/lib/command_kit/options/quiet.rb +3 -0
  44. data/lib/command_kit/options/verbose.rb +5 -0
  45. data/lib/command_kit/options/version.rb +6 -0
  46. data/lib/command_kit/options.rb +59 -10
  47. data/lib/command_kit/os/linux.rb +157 -0
  48. data/lib/command_kit/os.rb +165 -11
  49. data/lib/command_kit/package_manager.rb +200 -0
  50. data/lib/command_kit/pager.rb +84 -9
  51. data/lib/command_kit/printing/indent.rb +25 -2
  52. data/lib/command_kit/printing.rb +23 -0
  53. data/lib/command_kit/program_name.rb +7 -0
  54. data/lib/command_kit/stdio.rb +24 -0
  55. data/lib/command_kit/sudo.rb +40 -0
  56. data/lib/command_kit/terminal.rb +159 -0
  57. data/lib/command_kit/usage.rb +14 -0
  58. data/lib/command_kit/version.rb +1 -1
  59. data/lib/command_kit/xdg.rb +21 -1
  60. data/lib/command_kit.rb +1 -0
  61. data/spec/arguments/argument_spec.rb +5 -41
  62. data/spec/arguments/argument_value_spec.rb +1 -61
  63. data/spec/arguments_spec.rb +8 -25
  64. data/spec/colors_spec.rb +277 -13
  65. data/spec/command_name_spec.rb +1 -1
  66. data/spec/command_spec.rb +4 -1
  67. data/spec/commands/auto_load/subcommand_spec.rb +1 -1
  68. data/spec/commands/auto_load_spec.rb +1 -1
  69. data/spec/commands/auto_require_spec.rb +2 -2
  70. data/spec/commands/help_spec.rb +1 -1
  71. data/spec/commands/parent_command_spec.rb +1 -1
  72. data/spec/commands/subcommand_spec.rb +1 -1
  73. data/spec/commands_spec.rb +2 -2
  74. data/spec/description_spec.rb +1 -25
  75. data/spec/env/home_spec.rb +1 -1
  76. data/spec/env/path_spec.rb +1 -1
  77. data/spec/examples_spec.rb +1 -25
  78. data/spec/exception_handler_spec.rb +1 -1
  79. data/spec/help/man_spec.rb +316 -0
  80. data/spec/help_spec.rb +0 -25
  81. data/spec/inflector_spec.rb +71 -9
  82. data/spec/interactive_spec.rb +415 -0
  83. data/spec/main_spec.rb +7 -7
  84. data/spec/man_spec.rb +46 -0
  85. data/spec/open_app_spec.rb +85 -0
  86. data/spec/options/option_spec.rb +48 -9
  87. data/spec/options/option_value_spec.rb +53 -4
  88. data/spec/options_spec.rb +1 -1
  89. data/spec/os/linux_spec.rb +154 -0
  90. data/spec/os_spec.rb +201 -14
  91. data/spec/package_manager_spec.rb +806 -0
  92. data/spec/pager_spec.rb +78 -15
  93. data/spec/printing/indent_spec.rb +1 -1
  94. data/spec/printing_spec.rb +10 -2
  95. data/spec/program_name_spec.rb +1 -1
  96. data/spec/spec_helper.rb +0 -3
  97. data/spec/sudo_spec.rb +51 -0
  98. data/spec/{console_spec.rb → terminal_spec.rb} +65 -35
  99. data/spec/usage_spec.rb +2 -2
  100. data/spec/xdg_spec.rb +1 -1
  101. metadata +32 -13
  102. data/lib/command_kit/arguments/usage.rb +0 -6
  103. data/lib/command_kit/console.rb +0 -141
  104. data/lib/command_kit/options/usage.rb +0 -6
@@ -26,7 +26,7 @@ module CommandKit
26
26
  #
27
27
  # ## Alternatives
28
28
  #
29
- # * [ansi](http://rubyworks.github.io/ansi/)
29
+ # * [ansi](https://rubyworks.github.io/ansi/)
30
30
  # * [colorize](https://github.com/fazibear/colorize#readme)
31
31
  #
32
32
  # @see https://en.wikipedia.org/wiki/ANSI_escape_code
@@ -67,7 +67,7 @@ module CommandKit
67
67
  # ANSI color code for blue
68
68
  BLUE = "\e[34m"
69
69
 
70
- # ANSI color code for megenta
70
+ # ANSI color code for magenta
71
71
  MAGENTA = "\e[35m"
72
72
 
73
73
  # ANSI color code for cyan
@@ -79,6 +79,51 @@ module CommandKit
79
79
  # ANSI color for the default foreground color
80
80
  RESET_COLOR = "\e[39m"
81
81
 
82
+ # ANSI color code for background color black
83
+ #
84
+ # @since 0.2.0
85
+ ON_BLACK = "\e[40m"
86
+
87
+ # ANSI color code for background color red
88
+ #
89
+ # @since 0.2.0
90
+ ON_RED = "\e[41m"
91
+
92
+ # ANSI color code for background color green
93
+ #
94
+ # @since 0.2.0
95
+ ON_GREEN = "\e[42m"
96
+
97
+ # ANSI color code for background color yellow
98
+ #
99
+ # @since 0.2.0
100
+ ON_YELLOW = "\e[43m"
101
+
102
+ # ANSI color code for background color blue
103
+ #
104
+ # @since 0.2.0
105
+ ON_BLUE = "\e[44m"
106
+
107
+ # ANSI color code for background color megenta
108
+ #
109
+ # @since 0.2.0
110
+ ON_MAGENTA = "\e[45m"
111
+
112
+ # ANSI color code for background color cyan
113
+ #
114
+ # @since 0.2.0
115
+ ON_CYAN = "\e[46m"
116
+
117
+ # ANSI color code for background color white
118
+ #
119
+ # @since 0.2.0
120
+ ON_WHITE = "\e[47m"
121
+
122
+ # ANSI color for the default background color
123
+ #
124
+ # @since 0.2.0
125
+ RESET_BG = "\e[49m"
126
+
82
127
  module_function
83
128
 
84
129
  #
@@ -88,6 +133,8 @@ module CommandKit
88
133
  #
89
134
  # @see RESET
90
135
  #
136
+ # @api public
137
+ #
91
138
  def reset
92
139
  RESET
93
140
  end
@@ -95,6 +142,8 @@ module CommandKit
95
142
  #
96
143
  # @see reset
97
144
  #
145
+ # @api public
146
+ #
98
147
  def clear
99
148
  reset
100
149
  end
@@ -110,6 +159,8 @@ module CommandKit
110
159
  #
111
160
  # @see BOLD
112
161
  #
162
+ # @api public
163
+ #
113
164
  def bold(string=nil)
114
165
  if string then "#{BOLD}#{string}#{RESET_INTENSITY}"
115
166
  else BOLD
@@ -127,6 +178,8 @@ module CommandKit
127
178
  #
128
179
  # @see BLACK
129
180
  #
181
+ # @api public
182
+ #
130
183
  def black(string=nil)
131
184
  if string then "#{BLACK}#{string}#{RESET_COLOR}"
132
185
  else BLACK
@@ -144,6 +197,8 @@ module CommandKit
144
197
  #
145
198
  # @see RED
146
199
  #
200
+ # @api public
201
+ #
147
202
  def red(string=nil)
148
203
  if string then "#{RED}#{string}#{RESET_COLOR}"
149
204
  else RED
@@ -161,6 +216,8 @@ module CommandKit
161
216
  #
162
217
  # @see GREEN
163
218
  #
219
+ # @api public
220
+ #
164
221
  def green(string=nil)
165
222
  if string then "#{GREEN}#{string}#{RESET_COLOR}"
166
223
  else GREEN
@@ -178,6 +235,8 @@ module CommandKit
178
235
  #
179
236
  # @see YELLOW
180
237
  #
238
+ # @api public
239
+ #
181
240
  def yellow(string=nil)
182
241
  if string then "#{YELLOW}#{string}#{RESET_COLOR}"
183
242
  else YELLOW
@@ -195,6 +254,8 @@ module CommandKit
195
254
  #
196
255
  # @see BLUE
197
256
  #
257
+ # @api public
258
+ #
198
259
  def blue(string=nil)
199
260
  if string then "#{BLUE}#{string}#{RESET_COLOR}"
200
261
  else BLUE
@@ -212,6 +273,8 @@ module CommandKit
212
273
  #
213
274
  # @see MAGENTA
214
275
  #
276
+ # @api public
277
+ #
215
278
  def magenta(string=nil)
216
279
  if string then "#{MAGENTA}#{string}#{RESET_COLOR}"
217
280
  else MAGENTA
@@ -229,6 +292,8 @@ module CommandKit
229
292
  #
230
293
  # @see CYAN
231
294
  #
295
+ # @api public
296
+ #
232
297
  def cyan(string=nil)
233
298
  if string then "#{CYAN}#{string}#{RESET_COLOR}"
234
299
  else CYAN
@@ -246,76 +311,209 @@ module CommandKit
246
311
  #
247
312
  # @see WHITE
248
313
  #
314
+ # @api public
315
+ #
249
316
  def white(string=nil)
250
317
  if string then "#{WHITE}#{string}#{RESET_COLOR}"
251
318
  else WHITE
252
319
  end
253
320
  end
254
- end
255
321
 
256
- #
257
- # Dummy module with the same interface as {ANSI}, but for when ANSI is not
258
- # supported.
259
- #
260
- module PlainText
261
- RESET = \
262
- CLEAR = \
263
- BOLD = \
264
- RESET_INTENSITY = \
265
- BLACK = \
266
- RED = \
267
- GREEN = \
268
- YELLOW = \
269
- BLUE = \
270
- MAGENTA = \
271
- CYAN = \
272
- WHITE = \
273
- RESET_COLOR = ''
274
-
275
- module_function
322
+ #
323
+ # Sets the background color to black.
324
+ #
325
+ # @param [String, nil] string
326
+ # An optional string.
327
+ #
328
+ # @return [String, ON_BLACK]
329
+ # The colorized string or just {ON_BLACK} if no arguments were given.
330
+ #
331
+ # @see ON_BLACK
332
+ #
333
+ # @api public
334
+ #
335
+ # @since 0.2.0
336
+ #
337
+ def on_black(string=nil)
338
+ if string then "#{ON_BLACK}#{string}#{RESET_BG}"
339
+ else ON_BLACK
340
+ end
341
+ end
276
342
 
277
- def reset
278
- RESET
343
+ #
344
+ # Sets the background color to red.
345
+ #
346
+ # @param [String, nil] string
347
+ # An optional string.
348
+ #
349
+ # @return [String, ON_RED]
350
+ # The colorized string or just {ON_RED} if no arguments were given.
351
+ #
352
+ # @see ON_RED
353
+ #
354
+ # @api public
355
+ #
356
+ # @since 0.2.0
357
+ #
358
+ def on_red(string=nil)
359
+ if string then "#{ON_RED}#{string}#{RESET_BG}"
360
+ else ON_RED
361
+ end
279
362
  end
280
363
 
281
- def clear
282
- reset
364
+ #
365
+ # Sets the background color to green.
366
+ #
367
+ # @param [String, nil] string
368
+ # An optional string.
369
+ #
370
+ # @return [String, ON_GREEN]
371
+ # The colorized string or just {ON_GREEN} if no arguments were given.
372
+ #
373
+ # @see ON_GREEN
374
+ #
375
+ # @api public
376
+ #
377
+ # @since 0.2.0
378
+ #
379
+ def on_green(string=nil)
380
+ if string then "#{ON_GREEN}#{string}#{RESET_BG}"
381
+ else ON_GREEN
382
+ end
283
383
  end
284
384
 
285
- def bold(string=nil)
286
- string || ''
385
+ #
386
+ # Sets the background color to yellow.
387
+ #
388
+ # @param [String, nil] string
389
+ # An optional string.
390
+ #
391
+ # @return [String, ON_YELLOW]
392
+ # The colorized string or just {ON_YELLOW} if no arguments were given.
393
+ #
394
+ # @see ON_YELLOW
395
+ #
396
+ # @api public
397
+ #
398
+ # @since 0.2.0
399
+ #
400
+ def on_yellow(string=nil)
401
+ if string then "#{ON_YELLOW}#{string}#{RESET_BG}"
402
+ else ON_YELLOW
403
+ end
287
404
  end
288
405
 
289
- def black(string=nil)
290
- string || ''
406
+ #
407
+ # Sets the background color to blue.
408
+ #
409
+ # @param [String, nil] string
410
+ # An optional string.
411
+ #
412
+ # @return [String, ON_BLUE]
413
+ # The colorized string or just {ON_BLUE} if no arguments were given.
414
+ #
415
+ # @see ON_BLUE
416
+ #
417
+ # @api public
418
+ #
419
+ # @since 0.2.0
420
+ #
421
+ def on_blue(string=nil)
422
+ if string then "#{ON_BLUE}#{string}#{RESET_BG}"
423
+ else ON_BLUE
424
+ end
291
425
  end
292
426
 
293
- def red(string=nil)
294
- string || ''
427
+ #
428
+ # Sets the background color to magenta.
429
+ #
430
+ # @param [String, nil] string
431
+ # An optional string.
432
+ #
433
+ # @return [String, ON_MAGENTA]
434
+ # The colorized string or just {ON_MAGENTA} if no arguments were given.
435
+ #
436
+ # @see ON_MAGENTA
437
+ #
438
+ # @api public
439
+ #
440
+ # @since 0.2.0
441
+ #
442
+ def on_magenta(string=nil)
443
+ if string then "#{ON_MAGENTA}#{string}#{RESET_BG}"
444
+ else ON_MAGENTA
445
+ end
295
446
  end
296
447
 
297
- def green(string=nil)
298
- string || ''
448
+ #
449
+ # Sets the background color to cyan.
450
+ #
451
+ # @param [String, nil] string
452
+ # An optional string.
453
+ #
454
+ # @return [String, ON_CYAN]
455
+ # The colorized string or just {ON_CYAN} if no arguments were given.
456
+ #
457
+ # @see ON_CYAN
458
+ #
459
+ # @api public
460
+ #
461
+ # @since 0.2.0
462
+ #
463
+ def on_cyan(string=nil)
464
+ if string then "#{ON_CYAN}#{string}#{RESET_BG}"
465
+ else ON_CYAN
466
+ end
299
467
  end
300
468
 
301
- def yellow(string=nil)
302
- string || ''
469
+ #
470
+ # Sets the background color to white.
471
+ #
472
+ # @param [String, nil] string
473
+ # An optional string.
474
+ #
475
+ # @return [String, ON_WHITE]
476
+ # The colorized string or just {ON_WHITE} if no arguments were given.
477
+ #
478
+ # @see ON_WHITE
479
+ #
480
+ # @api public
481
+ #
482
+ # @since 0.2.0
483
+ #
484
+ def on_white(string=nil)
485
+ if string then "#{ON_WHITE}#{string}#{RESET_BG}"
486
+ else ON_WHITE
487
+ end
303
488
  end
489
+ end
304
490
 
305
- def blue(string=nil)
306
- string || ''
491
+ #
492
+ # Dummy module with the same interface as {ANSI}, but for when ANSI is not
493
+ # supported.
494
+ #
495
+ module PlainText
496
+ ANSI.constants(false).each do |name|
497
+ const_set(name,'')
307
498
  end
308
499
 
309
- def magenta(string=nil)
310
- string || ''
500
+ module_function
501
+
502
+ def reset
503
+ RESET
311
504
  end
312
505
 
313
- def cyan(string=nil)
314
- string || ''
506
+ def clear
507
+ reset
315
508
  end
316
509
 
317
- def white(string=nil)
318
- string || ''
510
+ [
511
+ :bold, :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white,
512
+ :on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white
513
+ ].each do |name|
514
+ define_method(name) do |string=nil|
515
+ string || ''
516
+ end
319
517
  end
320
518
  end
321
519
 
@@ -331,6 +529,8 @@ module CommandKit
331
529
  # output. Color output will also be disabled if the given stream is not
332
530
  # a TTY.
333
531
  #
532
+ # @api public
533
+ #
334
534
  def ansi?(stream=stdout)
335
535
  env['TERM'] != 'dumb' && stream.tty?
336
536
  end
@@ -343,6 +543,14 @@ module CommandKit
343
543
  # @return [ANSI, PlainText]
344
544
  # The ANSI module or PlainText dummy module.
345
545
  #
546
+ # @example
547
+ # puts colors.green("Hello world")
548
+ #
549
+ # @example Using colors with stderr output:
550
+ # stderr.puts colors(stderr).green("Hello world")
551
+ #
552
+ # @api public
553
+ #
346
554
  def colors(stream=stdout)
347
555
  color = if ansi?(stream) then ANSI
348
556
  else PlainText
@@ -9,20 +9,66 @@ require 'command_kit/examples'
9
9
  require 'command_kit/description'
10
10
  require 'command_kit/exception_handler'
11
11
 
12
+ require 'fileutils'
13
+
12
14
  module CommandKit
13
15
  #
14
16
  # The command class base-class.
15
17
  #
18
+ # @note
19
+ # Command classes are not required to inherit from {Command}. If you do not
20
+ # wish to inherit from the {Command} base class, you can instead include
21
+ # the individual modules. This class only exists as a convenience.
22
+ #
16
23
  # ## Examples
17
24
  #
18
25
  # class MyCmd < CommandKit::Command
19
26
  #
20
- # # ...
27
+ # usage '[OPTIONS] [-o OUTPUT] FILE'
28
+ #
29
+ # option :count, short: '-c',
30
+ # value: {
31
+ # type: Integer,
32
+ # default: 1
33
+ # },
34
+ # desc: "Number of times"
35
+ #
36
+ # option :output, short: '-o',
37
+ # value: {
38
+ # type: String,
39
+ # usage: 'FILE'
40
+ # },
41
+ # desc: "Optional output file"
42
+ #
43
+ # argument :file, required: true,
44
+ # usage: 'FILE',
45
+ # desc: "Input file"
46
+ #
47
+ # examples [
48
+ # '-o path/to/output.txt path/to/input.txt',
49
+ # '-v -c 2 -o path/to/output.txt path/to/input.txt',
50
+ # ]
51
+ #
52
+ # description 'Example command'
53
+ #
54
+ # def run(*files)
55
+ # # ...
56
+ # end
57
+ # end
58
+ #
59
+ # ### initialize and using instance variables
60
+ #
61
+ # option :verbose, short: '-v', desc: "Increase verbose level" do
62
+ # @verbose += 1
63
+ # end
64
+ #
65
+ # def initialize(**kwargs)
66
+ # super(**kwargs)
21
67
  #
68
+ # @verbose = 0
22
69
  # end
23
70
  #
24
- # @note Command classes are not required to inherit from {Command}. This class
25
- # only exists as a convenience.
71
+ # @api public
26
72
  #
27
73
  class Command
28
74
 
@@ -37,6 +83,7 @@ module CommandKit
37
83
  include Examples
38
84
  include Description
39
85
  include ExceptionHandler
86
+ include FileUtils
40
87
 
41
88
  end
42
89
  end