command_kit 0.3.0 → 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 +4 -4
- data/.github/workflows/ruby.yml +6 -6
- data/.rubocop.yml +16 -0
- data/ChangeLog.md +30 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +1 -1
- data/README.md +18 -9
- data/command_kit.gemspec +0 -1
- data/examples/printing/tables.rb +141 -0
- data/examples/subcommands/cli/config/get.rb +47 -0
- data/examples/subcommands/cli/config/set.rb +44 -0
- data/examples/subcommands/cli/config.rb +23 -0
- data/examples/subcommands/cli/list.rb +35 -0
- data/examples/subcommands/cli/update.rb +47 -0
- data/examples/subcommands/cli.rb +55 -0
- data/gemspec.yml +3 -3
- data/lib/command_kit/bug_report.rb +105 -0
- data/lib/command_kit/colors.rb +4 -4
- data/lib/command_kit/edit.rb +54 -0
- data/lib/command_kit/env/home.rb +1 -1
- data/lib/command_kit/env.rb +1 -1
- data/lib/command_kit/inflector.rb +1 -1
- data/lib/command_kit/options/option.rb +5 -1
- data/lib/command_kit/options/option_value.rb +2 -2
- data/lib/command_kit/options/parser.rb +2 -2
- data/lib/command_kit/options/quiet.rb +1 -1
- data/lib/command_kit/options/verbose.rb +2 -2
- data/lib/command_kit/options/version.rb +10 -0
- data/lib/command_kit/options.rb +1 -1
- data/lib/command_kit/os/linux.rb +1 -1
- data/lib/command_kit/os.rb +2 -2
- data/lib/command_kit/printing/fields.rb +56 -0
- data/lib/command_kit/printing/indent.rb +1 -1
- data/lib/command_kit/printing/lists.rb +91 -0
- data/lib/command_kit/printing/tables/border_style.rb +169 -0
- data/lib/command_kit/printing/tables/cell_builder.rb +93 -0
- data/lib/command_kit/printing/tables/row_builder.rb +111 -0
- data/lib/command_kit/printing/tables/style.rb +198 -0
- data/lib/command_kit/printing/tables/table_builder.rb +145 -0
- data/lib/command_kit/printing/tables/table_formatter.rb +254 -0
- data/lib/command_kit/printing/tables.rb +208 -0
- data/lib/command_kit/stdio.rb +5 -1
- data/lib/command_kit/version.rb +1 -1
- data/lib/command_kit/xdg.rb +1 -1
- data/spec/bug_report_spec.rb +266 -0
- data/spec/colors_spec.rb +6 -0
- data/spec/command_name_spec.rb +1 -1
- data/spec/commands_spec.rb +26 -0
- data/spec/edit_spec.rb +72 -0
- data/spec/options/option_spec.rb +12 -2
- data/spec/options/parser_spec.rb +19 -0
- data/spec/options/quiet_spec.rb +51 -0
- data/spec/options/verbose_spec.rb +51 -0
- data/spec/options/version_spec.rb +146 -0
- data/spec/pager_spec.rb +1 -1
- data/spec/printing/fields_spec.rb +167 -0
- data/spec/printing/lists_spec.rb +99 -0
- data/spec/printing/tables/border_style.rb +43 -0
- data/spec/printing/tables/cell_builer_spec.rb +135 -0
- data/spec/printing/tables/row_builder_spec.rb +165 -0
- data/spec/printing/tables/style_spec.rb +377 -0
- data/spec/printing/tables/table_builder_spec.rb +252 -0
- data/spec/printing/tables/table_formatter_spec.rb +1190 -0
- data/spec/printing/tables_spec.rb +1069 -0
- metadata +39 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a35e836c936c91aa916f33e691260a591d2e363de6a0fd3d9259150b86add15
|
4
|
+
data.tar.gz: fe2e2cc87a8a0fe889cba23b5567b307bb088fdf3ee4eff7d681694a981b5143
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da2676ab635edc9898457b1c532af6ac4c478c6f3a0ab19dfc42f0abc4ddd73ef4f473fe7699f8bed3ac5abec3381a7a9ca9c50736c4cc3697612ef36f3d6fea
|
7
|
+
data.tar.gz: d30a04ab5274f489ef3e534950301669d47112ee56d43b59e51c5e816f7de21fba72197d43f3362331c40f3e75a70e0c316d53fe12bf8960daf79efec270a372
|
data/.github/workflows/ruby.yml
CHANGED
@@ -10,13 +10,13 @@ jobs:
|
|
10
10
|
fail-fast: false
|
11
11
|
matrix:
|
12
12
|
ruby:
|
13
|
-
-
|
14
|
-
- 3.
|
15
|
-
- 3.
|
13
|
+
- '3.0'
|
14
|
+
- '3.1'
|
15
|
+
- '3.2'
|
16
|
+
- '3.3'
|
16
17
|
# TODO: uncomment when jruby supports ruby >= 2.7
|
17
18
|
# - jruby
|
18
|
-
|
19
|
-
# - truffleruby
|
19
|
+
- truffleruby
|
20
20
|
name: Ruby ${{ matrix.ruby }}
|
21
21
|
steps:
|
22
22
|
- uses: actions/checkout@v2
|
@@ -37,7 +37,7 @@ jobs:
|
|
37
37
|
- name: Set up Ruby
|
38
38
|
uses: ruby/setup-ruby@v1
|
39
39
|
with:
|
40
|
-
ruby-version:
|
40
|
+
ruby-version: 3.0
|
41
41
|
- name: Install dependencies
|
42
42
|
run: bundle install --jobs 4 --retry 3
|
43
43
|
- name: Run rubocop
|
data/.rubocop.yml
CHANGED
@@ -22,6 +22,7 @@ Style/PercentLiteralDelimiters:
|
|
22
22
|
'%I': '[]'
|
23
23
|
'%w': '[]'
|
24
24
|
'%W': '[]'
|
25
|
+
Style/UnlessElse: { Enabled: false }
|
25
26
|
|
26
27
|
#
|
27
28
|
# rules that are in flux
|
@@ -139,3 +140,18 @@ Style/TrailingCommaInHashLiteral: { Enabled: false } # Offense count: 2
|
|
139
140
|
|
140
141
|
# rubocop cannot tell that rubygems_mfa_required is enabled in gemspec.yml
|
141
142
|
Gemspec/RequireMFA: { Enabled: false }
|
143
|
+
|
144
|
+
# make an exception for our gemspec code
|
145
|
+
Gemspec/DuplicatedAssignment:
|
146
|
+
Exclude:
|
147
|
+
- 'command_kit.gemspec'
|
148
|
+
|
149
|
+
Bundler/OrderedGems: { Enabled: false }
|
150
|
+
|
151
|
+
# rubocup is not aware of vertical alignment of matrixes (Array of Arrays).
|
152
|
+
Layout/SpaceInsideArrayLiteralBrackets: { Enabled: false }
|
153
|
+
|
154
|
+
Naming/HeredocDelimiterNaming: { Enabled: false }
|
155
|
+
|
156
|
+
# I prefer to use explicit parenthesis for compound logical statements
|
157
|
+
Style/RedundantParentheses: { Enabled: false }
|
data/ChangeLog.md
CHANGED
@@ -1,3 +1,33 @@
|
|
1
|
+
### 0.4.1 / 2024-01-03
|
2
|
+
|
3
|
+
* Added more examples of how to define sub-commands and sub-sub-commands.
|
4
|
+
|
5
|
+
#### CommandKit::Options::Parser
|
6
|
+
|
7
|
+
* Do not override the command's `usage` if it's already been set.
|
8
|
+
|
9
|
+
#### CommandKit::Printing::Tables
|
10
|
+
|
11
|
+
* Format the table output as UTF-8 to allow UTF-8 data in the formatted table.
|
12
|
+
|
13
|
+
### 0.4.0 / 2022-11-11
|
14
|
+
|
15
|
+
* Added {CommandKit::BugReport}.
|
16
|
+
* Added {CommandKit::Edit}.
|
17
|
+
* Added {CommandKit::Printing::Fields}.
|
18
|
+
* Added {CommandKit::Printing::Lists}.
|
19
|
+
* Added {CommandKit::Printing::Tables}.
|
20
|
+
|
21
|
+
#### CommandKit::Colors
|
22
|
+
|
23
|
+
* Support disabling ANSI color output if the `NO_COLOR` environment variable is
|
24
|
+
set.
|
25
|
+
|
26
|
+
#### CommandKit::Options
|
27
|
+
|
28
|
+
* Correct the option usage for long option flags that have optional values
|
29
|
+
(ex: `--longopt[=VALUE]`).
|
30
|
+
|
1
31
|
### 0.3.0 / 2021-12-26
|
2
32
|
|
3
33
|
* Added {CommandKit::FileUtils}.
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -11,30 +11,33 @@
|
|
11
11
|
|
12
12
|
## Description
|
13
13
|
|
14
|
-
A Ruby toolkit for building clean, correct, and robust CLI commands as
|
14
|
+
A modular Ruby toolkit for building clean, correct, and robust CLI commands as
|
15
15
|
plain-old Ruby classes.
|
16
16
|
|
17
17
|
## Features
|
18
18
|
|
19
19
|
* **Simple** - Commands are plain-old ruby classes, with options and
|
20
|
-
arguments declared as attributes. All features are
|
20
|
+
arguments declared as attributes. All features are Ruby modules that can be
|
21
21
|
included into command classes.
|
22
22
|
* **Correct** - CommandKit behaves like a standard UNIX command.
|
23
23
|
* Safely handles Ctrl^C / SIGINT interrupts and [exits with 130](https://tldp.org/LDP/abs/html/exitcodes.html).
|
24
24
|
* Safely handles broken pipes (aka `mycmd | head`).
|
25
|
-
* Respects common environment variables (ex: `TERM=dumb`).
|
25
|
+
* Respects common environment variables (ex: `TERM=dumb` and `NO_COLOR`).
|
26
26
|
* Uses [OptionParser][optparse] for POSIX option parsing.
|
27
|
-
* Disables ANSI color when output is redirected to a file
|
27
|
+
* Disables ANSI color when output is redirected to a file or when `NO_COLOR`
|
28
|
+
is set.
|
28
29
|
* **Complete** - Provides many additional CLI features.
|
29
30
|
* OS detection.
|
30
31
|
* Terminal size detection.
|
31
32
|
* ANSI coloring support.
|
32
33
|
* Interactive input.
|
34
|
+
* Rich text printing support (fields, lists, and tables).
|
33
35
|
* Subcommands (explicit or lazy-loaded) and command aliases.
|
34
36
|
* Displaying man pages for `--help`/`help`.
|
35
37
|
* Using the pager (aka `less`).
|
36
38
|
* [XDG directories](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) (aka `~/.config/`, `~/.local/share/`, `~/.cache/`).
|
37
|
-
*
|
39
|
+
* Exception handling / Bug reporting.
|
40
|
+
* **Testable** - Since commands are plain-old Ruby classes, it's easy to
|
38
41
|
initialize them and call `#main` or `#run`.
|
39
42
|
|
40
43
|
## Anti-Features
|
@@ -45,7 +48,7 @@ plain-old Ruby classes.
|
|
45
48
|
|
46
49
|
## Requirements
|
47
50
|
|
48
|
-
* [ruby] >=
|
51
|
+
* [ruby] >= 3.0.0
|
49
52
|
|
50
53
|
## Install
|
51
54
|
|
@@ -56,13 +59,13 @@ $ gem install command_kit
|
|
56
59
|
### gemspec
|
57
60
|
|
58
61
|
```ruby
|
59
|
-
gem.add_dependency 'command_kit', '~> 0.
|
62
|
+
gem.add_dependency 'command_kit', '~> 0.3'
|
60
63
|
```
|
61
64
|
|
62
65
|
### Gemfile
|
63
66
|
|
64
67
|
```ruby
|
65
|
-
gem 'command_kit', '~> 0.
|
68
|
+
gem 'command_kit', '~> 0.3'
|
66
69
|
```
|
67
70
|
|
68
71
|
## Examples
|
@@ -204,6 +207,7 @@ end
|
|
204
207
|
### Reference
|
205
208
|
|
206
209
|
* [CommandKit::Arguments](https://rubydoc.info/gems/command_kit/CommandKit/Arguments)
|
210
|
+
* [CommandKit::BugReport](https://rubydoc.info/gems/command_kit/CommandKit/BugReport)
|
207
211
|
* [CommandKit::Colors](https://rubydoc.info/gems/command_kit/CommandKit/Colors)
|
208
212
|
* [CommandKit::Command](https://rubydoc.info/gems/command_kit/CommandKit/Command)
|
209
213
|
* [CommandKit::CommandName](https://rubydoc.info/gems/command_kit/CommandKit/CommandName)
|
@@ -211,11 +215,13 @@ end
|
|
211
215
|
* [CommandKit::Commands::AutoLoad](https://rubydoc.info/gems/command_kit/CommandKit/Commands/AutoLoad)
|
212
216
|
* [CommandKit::Commands::AutoRequire](https://rubydoc.info/gems/command_kit/CommandKit/Commands/AutoRequire)
|
213
217
|
* [CommandKit::Description](https://rubydoc.info/gems/command_kit/CommandKit/Description)
|
218
|
+
* [CommandKit::Edit](https://rubydoc.info/gems/command_kit/CommandKit/Edit)
|
214
219
|
* [CommandKit::Env](https://rubydoc.info/gems/command_kit/CommandKit/Env)
|
215
220
|
* [CommandKit::Env::Home](https://rubydoc.info/gems/command_kit/CommandKit/Env/Home)
|
216
221
|
* [CommandKit::Env::Path](https://rubydoc.info/gems/command_kit/CommandKit/Env/Path)
|
217
222
|
* [CommandKit::Examples](https://rubydoc.info/gems/command_kit/CommandKit/Examples)
|
218
223
|
* [CommandKit::ExceptionHandler](https://rubydoc.info/gems/command_kit/CommandKit/ExceptionHandler)
|
224
|
+
* [CommandKit::FileUtils](https://rubydoc.info/gems/command_kit/CommandKit/FileUtils)
|
219
225
|
* [CommandKit::Help](https://rubydoc.info/gems/command_kit/CommandKit/Help)
|
220
226
|
* [CommandKit::Help::Man](https://rubydoc.info/gems/command_kit/CommandKit/Help/Man)
|
221
227
|
* [CommandKit::Interactive](https://rubydoc.info/gems/command_kit/CommandKit/Interactive)
|
@@ -225,7 +231,10 @@ end
|
|
225
231
|
* [CommandKit::Options::Verbose](https://rubydoc.info/gems/command_kit/CommandKit/Options/Verbose)
|
226
232
|
* [CommandKit::Pager](https://rubydoc.info/gems/command_kit/CommandKit/Pager)
|
227
233
|
* [CommandKit::Printing](https://rubydoc.info/gems/command_kit/CommandKit/Printing)
|
234
|
+
* [CommandKit::Printing::Fields](https://rubydoc.info/gems/command_kit/CommandKit/Printing/Fields)
|
228
235
|
* [CommandKit::Printing::Indent](https://rubydoc.info/gems/command_kit/CommandKit/Printing/Indent)
|
236
|
+
* [CommandKit::Printing::Lists](https://rubydoc.info/gems/command_kit/CommandKit/Printing/Lists)
|
237
|
+
* [CommandKit::Printing::Tables](https://rubydoc.info/gems/command_kit/CommandKit/Printing/Tables)
|
229
238
|
* [CommandKit::ProgramName](https://rubydoc.info/gems/command_kit/CommandKit/ProgramName)
|
230
239
|
* [CommandKit::Stdio](https://rubydoc.info/gems/command_kit/CommandKit/Stdio)
|
231
240
|
* [CommandKit::Terminal](https://rubydoc.info/gems/command_kit/CommandKit/Terminal)
|
@@ -244,7 +253,7 @@ Twitter.
|
|
244
253
|
|
245
254
|
## Copyright
|
246
255
|
|
247
|
-
Copyright (c) 2021 Hal Brodigan
|
256
|
+
Copyright (c) 2021-2024 Hal Brodigan
|
248
257
|
|
249
258
|
See {file:LICENSE.txt} for details.
|
250
259
|
|
data/command_kit.gemspec
CHANGED
@@ -32,7 +32,6 @@ Gem::Specification.new do |gem|
|
|
32
32
|
gem.default_executable = gem.executables.first if Gem::VERSION < '1.7.'
|
33
33
|
|
34
34
|
gem.extensions = glob[gemspec['extensions'] || 'ext/**/extconf.rb']
|
35
|
-
gem.test_files = glob[gemspec['test_files'] || '{test/{**/}*_test.rb']
|
36
35
|
gem.extra_rdoc_files = glob[gemspec['extra_doc_files'] || '*.{txt,md}']
|
37
36
|
|
38
37
|
gem.require_paths = Array(gemspec.fetch('require_paths') {
|
@@ -0,0 +1,141 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.expand_path('../../../lib',__FILE__))
|
4
|
+
require 'command_kit/command'
|
5
|
+
require 'command_kit/printing/tables'
|
6
|
+
|
7
|
+
class TablesCmd < CommandKit::Command
|
8
|
+
|
9
|
+
include CommandKit::Printing::Tables
|
10
|
+
|
11
|
+
def run
|
12
|
+
header = ['A', 'B', 'C']
|
13
|
+
table = [
|
14
|
+
['AAAA', 'BBBB', 'CCCC'],
|
15
|
+
['AAAA', 'BBBB', 'CCCC'],
|
16
|
+
['AAAA', 'BBBB', 'CCCC']
|
17
|
+
]
|
18
|
+
|
19
|
+
puts
|
20
|
+
puts "Table:"
|
21
|
+
puts
|
22
|
+
print_table table
|
23
|
+
|
24
|
+
puts
|
25
|
+
puts "Table with header:"
|
26
|
+
puts
|
27
|
+
print_table table, header: header
|
28
|
+
|
29
|
+
puts
|
30
|
+
puts "ASCII border:"
|
31
|
+
puts
|
32
|
+
|
33
|
+
print_table table, header: header,
|
34
|
+
border: :ascii
|
35
|
+
|
36
|
+
puts
|
37
|
+
puts "Border + Separated rows:"
|
38
|
+
puts
|
39
|
+
|
40
|
+
print_table table, header: header,
|
41
|
+
border: :ascii,
|
42
|
+
separate_rows: true
|
43
|
+
|
44
|
+
puts
|
45
|
+
puts "Lined border:"
|
46
|
+
puts
|
47
|
+
|
48
|
+
print_table table, header: header,
|
49
|
+
border: :line
|
50
|
+
|
51
|
+
puts
|
52
|
+
puts "Double line border:"
|
53
|
+
puts
|
54
|
+
|
55
|
+
print_table table, header: header,
|
56
|
+
border: :double_line
|
57
|
+
|
58
|
+
uneven_table = [
|
59
|
+
['AAAAAA', 'B', 'CCCCCCC'],
|
60
|
+
['AAA', 'BBBB', 'CCC' ],
|
61
|
+
['A', 'BBBBBBB', 'C' ]
|
62
|
+
]
|
63
|
+
|
64
|
+
puts
|
65
|
+
puts "Left-justified:"
|
66
|
+
puts
|
67
|
+
|
68
|
+
print_table uneven_table, header: header,
|
69
|
+
justify: :left,
|
70
|
+
justify_header: :left,
|
71
|
+
border: :line
|
72
|
+
|
73
|
+
puts
|
74
|
+
puts "Right-justified:"
|
75
|
+
puts
|
76
|
+
|
77
|
+
print_table uneven_table, header: header,
|
78
|
+
justify: :right,
|
79
|
+
justify_header: :right,
|
80
|
+
border: :line
|
81
|
+
|
82
|
+
puts
|
83
|
+
puts "Center-justified:"
|
84
|
+
puts
|
85
|
+
|
86
|
+
print_table uneven_table, header: header,
|
87
|
+
justify: :center,
|
88
|
+
justify_header: :center,
|
89
|
+
border: :line
|
90
|
+
|
91
|
+
puts
|
92
|
+
puts "Table with empty cells:"
|
93
|
+
puts
|
94
|
+
|
95
|
+
table_with_empty_cells = [
|
96
|
+
['AAAA', 'BBBB', 'CCCC'],
|
97
|
+
['AAAA', nil, 'CCCC'],
|
98
|
+
['AAAA', 'BBBB']
|
99
|
+
]
|
100
|
+
|
101
|
+
print_table table_with_empty_cells, header: header,
|
102
|
+
justify: :left,
|
103
|
+
border: :line
|
104
|
+
|
105
|
+
puts
|
106
|
+
puts "Multi-line table:"
|
107
|
+
puts
|
108
|
+
|
109
|
+
multi_line_table = [
|
110
|
+
['AAAA', 'BBBB', "CCCC\nCC"],
|
111
|
+
['AAAA', "BBBB\nB", 'CCCC'],
|
112
|
+
["AAAA\nAA\nA", "BBBB", "CCCC"]
|
113
|
+
]
|
114
|
+
|
115
|
+
print_table multi_line_table, header: header,
|
116
|
+
justify: :left,
|
117
|
+
border: :line
|
118
|
+
|
119
|
+
puts
|
120
|
+
puts "Indent aware:"
|
121
|
+
puts
|
122
|
+
|
123
|
+
puts "* Item 1"
|
124
|
+
indent do
|
125
|
+
puts "* Item 2"
|
126
|
+
puts
|
127
|
+
|
128
|
+
indent do
|
129
|
+
print_table table, header: header,
|
130
|
+
border: :line
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
puts
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
if __FILE__ == $0
|
140
|
+
TablesCmd.start
|
141
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'command_kit/command'
|
2
|
+
|
3
|
+
module Foo
|
4
|
+
class CLI
|
5
|
+
class Config < CommandKit::Command
|
6
|
+
#
|
7
|
+
# The `config get` sub-command.
|
8
|
+
#
|
9
|
+
class Get < CommandKit::Command
|
10
|
+
|
11
|
+
usage '[options] NAME'
|
12
|
+
|
13
|
+
argument :name, required: false,
|
14
|
+
desc: 'Configuration variable name'
|
15
|
+
|
16
|
+
description 'Gets a configuration variable'
|
17
|
+
|
18
|
+
CONFIG = {
|
19
|
+
'name' => 'John Smith',
|
20
|
+
'email' => 'john.smith@example.com'
|
21
|
+
}
|
22
|
+
|
23
|
+
#
|
24
|
+
# Runs the `config get` sub-command.
|
25
|
+
#
|
26
|
+
# @param [String, nil] name
|
27
|
+
# The optional name argument.
|
28
|
+
#
|
29
|
+
def run(name=nil)
|
30
|
+
if name
|
31
|
+
unless CONFIG.has_key?(name)
|
32
|
+
print_error "unknown config variable: #{name}"
|
33
|
+
exit(1)
|
34
|
+
end
|
35
|
+
|
36
|
+
puts CONFIG.fetch(name)
|
37
|
+
else
|
38
|
+
CONFIG.each do |name,value|
|
39
|
+
puts "#{name}:\t#{value}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'command_kit/command'
|
2
|
+
|
3
|
+
module Foo
|
4
|
+
class CLI
|
5
|
+
class Config < CommandKit::Command
|
6
|
+
#
|
7
|
+
# The `config set` sub-command.
|
8
|
+
#
|
9
|
+
class Set < CommandKit::Command
|
10
|
+
|
11
|
+
usage '[options] NAME'
|
12
|
+
|
13
|
+
argument :name, required: true,
|
14
|
+
desc: 'Configuration variable name to set'
|
15
|
+
|
16
|
+
argument :value, required: true,
|
17
|
+
desc: 'Configuration variable value to set'
|
18
|
+
|
19
|
+
description 'Sets a configuration variable'
|
20
|
+
|
21
|
+
CONFIG = {
|
22
|
+
'name' => 'John Smith',
|
23
|
+
'email' => 'john.smith@example.com'
|
24
|
+
}
|
25
|
+
|
26
|
+
#
|
27
|
+
# Runs the `config get` sub-command.
|
28
|
+
#
|
29
|
+
# @param [String] name
|
30
|
+
# The name argument.
|
31
|
+
#
|
32
|
+
def run(name,value)
|
33
|
+
unless CONFIG.has_key?(name)
|
34
|
+
print_error "unknown config variable: #{name}"
|
35
|
+
exit(1)
|
36
|
+
end
|
37
|
+
|
38
|
+
puts "Configuration variable #{name} was #{CONFIG.fetch(name)}, but is now #{value}"
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'command_kit/command'
|
2
|
+
require 'command_kit/commands'
|
3
|
+
|
4
|
+
require_relative 'config/get'
|
5
|
+
require_relative 'config/set'
|
6
|
+
|
7
|
+
module Foo
|
8
|
+
class CLI
|
9
|
+
#
|
10
|
+
# The `config` sub-command.
|
11
|
+
#
|
12
|
+
class Config < CommandKit::Command
|
13
|
+
|
14
|
+
include CommandKit::Commands
|
15
|
+
|
16
|
+
command Get
|
17
|
+
command Set
|
18
|
+
|
19
|
+
description 'Get or set the configuration'
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'command_kit/command'
|
2
|
+
|
3
|
+
module Foo
|
4
|
+
class CLI
|
5
|
+
#
|
6
|
+
# The `list` sub-command.
|
7
|
+
#
|
8
|
+
class List < CommandKit::Command
|
9
|
+
|
10
|
+
usage '[options] [NAME]'
|
11
|
+
|
12
|
+
argument :name, required: false,
|
13
|
+
desc: 'Optional name to list'
|
14
|
+
|
15
|
+
description 'Lists the contents'
|
16
|
+
|
17
|
+
ITEMS = %w[foo bar baz]
|
18
|
+
|
19
|
+
#
|
20
|
+
# Runs the `list` sub-command.
|
21
|
+
#
|
22
|
+
# @param [String, nil] name
|
23
|
+
# The optional name argument.
|
24
|
+
#
|
25
|
+
def run(name=nil)
|
26
|
+
if name
|
27
|
+
puts ITEMS.grep(name)
|
28
|
+
else
|
29
|
+
puts ITEMS
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'command_kit/command'
|
2
|
+
|
3
|
+
module Foo
|
4
|
+
class CLI
|
5
|
+
#
|
6
|
+
# The `update` sub-command.
|
7
|
+
#
|
8
|
+
class Update < CommandKit::Command
|
9
|
+
|
10
|
+
usage '[options] [NAME]'
|
11
|
+
|
12
|
+
option :quiet, short: '-q',
|
13
|
+
desc: 'Suppresses logging messages'
|
14
|
+
|
15
|
+
argument :name, required: false,
|
16
|
+
desc: 'Optional name to update'
|
17
|
+
|
18
|
+
description 'Updates an item or all items'
|
19
|
+
|
20
|
+
ITEMS = %w[foo bar baz]
|
21
|
+
|
22
|
+
#
|
23
|
+
# Runs the `update` sub-command.
|
24
|
+
#
|
25
|
+
# @param [String, nil] name
|
26
|
+
# The optional name argument.
|
27
|
+
#
|
28
|
+
def run(name=nil)
|
29
|
+
if name
|
30
|
+
unless ITEMS.include?(name)
|
31
|
+
print_error "unknown item: #{name}"
|
32
|
+
exit(1)
|
33
|
+
end
|
34
|
+
|
35
|
+
puts "Updating #{name} ..." unless options[:quiet]
|
36
|
+
sleep 1
|
37
|
+
puts "Item #{name} updated." unless options[:quiet]
|
38
|
+
else
|
39
|
+
puts "Updating ..." unless options[:quiet]
|
40
|
+
sleep 2
|
41
|
+
puts "All items updated." unless options[:quiet]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.expand_path('../../../lib',__FILE__))
|
4
|
+
|
5
|
+
require 'command_kit/commands'
|
6
|
+
|
7
|
+
require_relative 'cli/config'
|
8
|
+
require_relative 'cli/list'
|
9
|
+
require_relative 'cli/update'
|
10
|
+
|
11
|
+
module Foo
|
12
|
+
#
|
13
|
+
# The main CLI command.
|
14
|
+
#
|
15
|
+
class CLI
|
16
|
+
|
17
|
+
include CommandKit::Commands
|
18
|
+
|
19
|
+
class << self
|
20
|
+
# The global configuration file setting.
|
21
|
+
#
|
22
|
+
# @return [String, nil]
|
23
|
+
attr_accessor :config_file
|
24
|
+
end
|
25
|
+
|
26
|
+
command_name 'foo'
|
27
|
+
|
28
|
+
# Commands must be explicitly registered, unless
|
29
|
+
# CommandKit::Commands::AutoLoad.new(...) is included.
|
30
|
+
command Config
|
31
|
+
command List
|
32
|
+
command Update
|
33
|
+
|
34
|
+
# Commands may have aliases
|
35
|
+
command_aliases['ls'] = 'list'
|
36
|
+
command_aliases['up'] = 'update'
|
37
|
+
|
38
|
+
# Global options may be defined which are parsed before the sub-command's
|
39
|
+
# options are parsed and the sub-command is executed.
|
40
|
+
option :config_file, short: '-C',
|
41
|
+
value: {
|
42
|
+
type: String,
|
43
|
+
usage: 'FILE'
|
44
|
+
},
|
45
|
+
desc: 'Global option to set the config file' do |file|
|
46
|
+
CLI.config_file = file
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
if $0 == __FILE__
|
53
|
+
# Normally you would invoke Foo::CLI.start from a bin/ script.
|
54
|
+
Foo::CLI.start
|
55
|
+
end
|
data/gemspec.yml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
name: command_kit
|
2
|
-
summary:
|
2
|
+
summary: An all-in-one modular Ruby CLI toolkit
|
3
3
|
description:
|
4
|
-
A Ruby toolkit for building clean, correct, and robust CLI commands as
|
4
|
+
A modular Ruby toolkit for building clean, correct, and robust CLI commands as
|
5
5
|
plain-old Ruby classes.
|
6
6
|
|
7
7
|
license: MIT
|
@@ -16,7 +16,7 @@ metadata:
|
|
16
16
|
changelog_uri: https://github.com/postmodern/command_kit.rb/blob/main/ChangeLog.md
|
17
17
|
rubygems_mfa_required: 'true'
|
18
18
|
|
19
|
-
required_ruby_version: ">=
|
19
|
+
required_ruby_version: ">= 3.0.0"
|
20
20
|
|
21
21
|
development_dependencies:
|
22
22
|
bundler: ~> 2.0
|