lino 2.2.0.pre.4 → 2.4.0.pre.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +266 -35
- data/lib/lino/appliables.rb +23 -0
- data/lib/lino/command_line_builder.rb +17 -0
- data/lib/lino/options.rb +4 -0
- data/lib/lino/subcommand_builder.rb +2 -0
- data/lib/lino/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 831f4578c9610aeb4a1b90a292c780cf5ac367df0f096a610251b7b8d32019a1
|
4
|
+
data.tar.gz: 1299a79ef3a734bbe2ed75fd8348487b184b1b8a4147f1e11364bd9600771900
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97e2e2bab67299de4c4e58e427078776e7637200c2078560a6938c1fdeba2c54026c57fe74480625e3a95fd9739fbe426b5c9ce0ec2be88ceecde7b9c6df8857
|
7
|
+
data.tar.gz: 34c6548cd462ff9f95b0f7f34fc1d5a05cb77b29fb4c2292123a653ff2dc41eaed1565dd14395a2f1b7875d95656c6b6805c4ec0dc950b29c129c706783d5cd6
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -41,10 +41,13 @@ command_line.execute
|
|
41
41
|
### `Lino::CommandLineBuilder`
|
42
42
|
|
43
43
|
The `CommandLineBuilder` allows a number of different styles of commands to be
|
44
|
-
built
|
44
|
+
built.
|
45
|
+
|
46
|
+
#### Flags
|
47
|
+
|
48
|
+
Flags can be added with `#with_flag`:
|
45
49
|
|
46
50
|
```ruby
|
47
|
-
# commands with flags
|
48
51
|
Lino::CommandLineBuilder.for_command('ls')
|
49
52
|
.with_flag('-l')
|
50
53
|
.with_flag('-a')
|
@@ -52,8 +55,24 @@ Lino::CommandLineBuilder.for_command('ls')
|
|
52
55
|
.to_s
|
53
56
|
|
54
57
|
# => ls -l -a
|
58
|
+
```
|
55
59
|
|
56
|
-
|
60
|
+
or `#with_flags`:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
Lino::CommandLineBuilder.for_command('ls')
|
64
|
+
.with_flags(%w[-l -a])
|
65
|
+
.build
|
66
|
+
.to_s
|
67
|
+
|
68
|
+
# => ls -l -a
|
69
|
+
```
|
70
|
+
|
71
|
+
#### Options
|
72
|
+
|
73
|
+
Options with values can be added with `#with_option`:
|
74
|
+
|
75
|
+
```ruby
|
57
76
|
Lino::CommandLineBuilder.for_command('gpg')
|
58
77
|
.with_option('--recipient', 'tobyclemson@gmail.com')
|
59
78
|
.with_option('--sign', './doc.txt')
|
@@ -61,8 +80,11 @@ Lino::CommandLineBuilder.for_command('gpg')
|
|
61
80
|
.to_s
|
62
81
|
|
63
82
|
# => gpg --recipient tobyclemson@gmail.com --sign ./doc.txt
|
83
|
+
```
|
64
84
|
|
65
|
-
|
85
|
+
or `#with_options`, either as a hash:
|
86
|
+
|
87
|
+
```ruby
|
66
88
|
Lino::CommandLineBuilder.for_command('gpg')
|
67
89
|
.with_options({
|
68
90
|
'--recipient' => 'tobyclemson@gmail.com',
|
@@ -72,8 +94,11 @@ Lino::CommandLineBuilder.for_command('gpg')
|
|
72
94
|
.to_s
|
73
95
|
|
74
96
|
# => gpg --recipient tobyclemson@gmail.com --sign ./doc.txt
|
97
|
+
```
|
75
98
|
|
76
|
-
|
99
|
+
or as an array:
|
100
|
+
|
101
|
+
```ruby
|
77
102
|
Lino::CommandLineBuilder.for_command('gpg')
|
78
103
|
.with_options(
|
79
104
|
[
|
@@ -85,16 +110,27 @@ Lino::CommandLineBuilder.for_command('gpg')
|
|
85
110
|
.to_s
|
86
111
|
|
87
112
|
# => gpg --recipient tobyclemson@gmail.com --sign ./doc.txt
|
113
|
+
```
|
114
|
+
|
115
|
+
Some commands allow options to be repeated:
|
88
116
|
|
89
|
-
|
117
|
+
```ruby
|
90
118
|
Lino::CommandLineBuilder.for_command('example.sh')
|
91
119
|
.with_repeated_option('--opt', ['file1.txt', nil, '', 'file2.txt'])
|
92
120
|
.build
|
93
121
|
.to_s
|
94
122
|
|
95
123
|
# => example.sh --opt file1.txt --opt file2.txt
|
124
|
+
```
|
125
|
+
|
126
|
+
> Note: `lino` ignores `nil` or empty option values in the resulting command
|
127
|
+
> line.
|
96
128
|
|
97
|
-
|
129
|
+
#### Arguments
|
130
|
+
|
131
|
+
Arguments can be added using `#with_argument`:
|
132
|
+
|
133
|
+
```ruby
|
98
134
|
Lino::CommandLineBuilder.for_command('diff')
|
99
135
|
.with_argument('./file1.txt')
|
100
136
|
.with_argument('./file2.txt')
|
@@ -102,16 +138,28 @@ Lino::CommandLineBuilder.for_command('diff')
|
|
102
138
|
.to_s
|
103
139
|
|
104
140
|
# => diff ./file1.txt ./file2.txt
|
141
|
+
```
|
105
142
|
|
106
|
-
|
143
|
+
or `#with_arguments`, as an array:
|
144
|
+
|
145
|
+
```ruby
|
107
146
|
Lino::CommandLineBuilder.for_command('diff')
|
108
147
|
.with_arguments(['./file1.txt', nil, '', './file2.txt'])
|
109
148
|
.build
|
110
149
|
.to_s
|
111
150
|
|
112
151
|
# => diff ./file1.txt ./file2.txt
|
152
|
+
```
|
153
|
+
|
154
|
+
> Note: `lino` ignores `nil` or empty argument values in the resulting command
|
155
|
+
> line.
|
156
|
+
|
157
|
+
#### Option Separators
|
113
158
|
|
114
|
-
|
159
|
+
By default, `lino` separates option values from the option by a space. This
|
160
|
+
can be overridden globally using `#with_option_separator`:
|
161
|
+
|
162
|
+
```ruby
|
115
163
|
Lino::CommandLineBuilder.for_command('java')
|
116
164
|
.with_option_separator(':')
|
117
165
|
.with_option('-splash', './images/splash.jpg')
|
@@ -120,44 +168,132 @@ Lino::CommandLineBuilder.for_command('java')
|
|
120
168
|
.to_s
|
121
169
|
|
122
170
|
# => java -splash:./images/splash.jpg ./application.jar
|
171
|
+
```
|
172
|
+
|
173
|
+
The option separator can be overridden on an option by option basis:
|
123
174
|
|
124
|
-
|
175
|
+
```ruby
|
176
|
+
Lino::CommandLineBuilder.for_command('java')
|
177
|
+
.with_option('-splash', './images/splash.jpg', separator: ':')
|
178
|
+
.with_argument('./application.jar')
|
179
|
+
.build
|
180
|
+
.to_s
|
181
|
+
|
182
|
+
# => java -splash:./images/splash.jpg ./application.jar
|
183
|
+
```
|
184
|
+
|
185
|
+
> Note: `#with_options` supports separator overriding when the options are
|
186
|
+
> passed as an array of hashes and a `separator` key is included in the
|
187
|
+
> hash.
|
188
|
+
|
189
|
+
> Note: `#with_repeated_option` also supports the `separator` named parameter.
|
190
|
+
|
191
|
+
> Note: option specific separators take precedence over the global option
|
192
|
+
> separator
|
193
|
+
|
194
|
+
#### Option Quoting
|
195
|
+
|
196
|
+
By default, `lino` does not quote option values. This can be overridden
|
197
|
+
globally using `#with_option_quoting`:
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
Lino::CommandLineBuilder.for_command('gpg')
|
201
|
+
.with_option_quoting('"')
|
202
|
+
.with_option('--sign', 'some file.txt')
|
203
|
+
.build
|
204
|
+
.to_s
|
205
|
+
|
206
|
+
# => gpg --sign "some file.txt"
|
207
|
+
```
|
208
|
+
|
209
|
+
The option quoting can be overridden on an option by option basis:
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
Lino::CommandLineBuilder.for_command('java')
|
213
|
+
.with_option('-splash', './images/splash.jpg', quoting: '"')
|
214
|
+
.with_argument('./application.jar')
|
215
|
+
.build
|
216
|
+
.to_s
|
217
|
+
|
218
|
+
# => java -splash "./images/splash.jpg" ./application.jar
|
219
|
+
```
|
220
|
+
|
221
|
+
> Note: `#with_options` supports quoting overriding when the options are
|
222
|
+
> passed as an array of hashes and a `quoting` key is included in the
|
223
|
+
> hash.
|
224
|
+
|
225
|
+
> Note: `#with_repeated_option` also supports the `quoting` named parameter.
|
226
|
+
|
227
|
+
> Note: option specific quoting take precedence over the global option
|
228
|
+
> quoting
|
229
|
+
|
230
|
+
#### Subcommands
|
231
|
+
|
232
|
+
Subcommands can be added using `#with_subcommand`:
|
233
|
+
|
234
|
+
```ruby
|
125
235
|
Lino::CommandLineBuilder.for_command('git')
|
126
236
|
.with_flag('--no-pager')
|
127
|
-
.with_subcommand('log')
|
128
|
-
sub.with_option('--since', '2016-01-01')
|
129
|
-
end
|
237
|
+
.with_subcommand('log')
|
130
238
|
.build
|
131
239
|
.to_s
|
132
240
|
|
133
|
-
# => git --no-pager log
|
241
|
+
# => git --no-pager log
|
242
|
+
```
|
134
243
|
|
135
|
-
|
244
|
+
Multi-level subcommands can be added using multiple `#with_subcommand`
|
245
|
+
invocations:
|
246
|
+
|
247
|
+
```ruby
|
136
248
|
Lino::CommandLineBuilder.for_command('gcloud')
|
137
249
|
.with_subcommand('sql')
|
138
250
|
.with_subcommand('instances')
|
139
251
|
.with_subcommand('set-root-password')
|
140
|
-
.with_subcommand('some-database')
|
141
|
-
sub.with_option('--password', 'super-secure')
|
142
|
-
end
|
252
|
+
.with_subcommand('some-database')
|
143
253
|
.build
|
144
254
|
.to_s
|
145
|
-
|
146
|
-
# => gcloud sql instances set-root-password some-database --password super-secure
|
147
255
|
|
148
|
-
#
|
256
|
+
# => gcloud sql instances set-root-password some-database
|
257
|
+
```
|
258
|
+
|
259
|
+
or using `#with_subcommands`:
|
260
|
+
|
261
|
+
```ruby
|
149
262
|
Lino::CommandLineBuilder.for_command('gcloud')
|
150
263
|
.with_subcommands(
|
151
264
|
%w[sql instances set-root-password some-database]
|
152
|
-
)
|
153
|
-
sub.with_option('--password', 'super-secure')
|
154
|
-
end
|
265
|
+
)
|
155
266
|
.build
|
156
267
|
.to_s
|
157
268
|
|
158
|
-
# => gcloud sql instances set-root-password some-database
|
269
|
+
# => gcloud sql instances set-root-password some-database
|
270
|
+
```
|
271
|
+
|
272
|
+
Subcommands also support options via `#with_flag`, `#with_flags`,
|
273
|
+
`#with_option`, `#with_options` and `#with_repeated_option` just like commands,
|
274
|
+
via a block, for example:
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
Lino::CommandLineBuilder.for_command('git')
|
278
|
+
.with_flag('--no-pager')
|
279
|
+
.with_subcommand('log') do |sub|
|
280
|
+
sub.with_option('--since', '2016-01-01')
|
281
|
+
end
|
282
|
+
.build
|
283
|
+
.to_s
|
284
|
+
|
285
|
+
# => git --no-pager log --since 2016-01-01
|
286
|
+
```
|
287
|
+
|
288
|
+
> Note: `#with_subcommands` also supports a block, which applies in the context
|
289
|
+
> of the last subcommand in the passed array.
|
290
|
+
|
291
|
+
#### Environment Variables
|
292
|
+
|
293
|
+
Command lines can be prefixed with environment variables using
|
294
|
+
`#with_environment_variable`:
|
159
295
|
|
160
|
-
|
296
|
+
```ruby
|
161
297
|
Lino::CommandLineBuilder.for_command('node')
|
162
298
|
.with_environment_variable('PORT', '3030')
|
163
299
|
.with_environment_variable('LOG_LEVEL', 'debug')
|
@@ -166,31 +302,73 @@ Lino::CommandLineBuilder.for_command('node')
|
|
166
302
|
.to_s
|
167
303
|
|
168
304
|
# => PORT=3030 LOG_LEVEL=debug node ./server.js
|
305
|
+
```
|
306
|
+
|
307
|
+
or `#with_environment_variables`, either as a hash:
|
308
|
+
|
309
|
+
```ruby
|
310
|
+
Lino::CommandLineBuilder.for_command('node')
|
311
|
+
.with_environment_variables({
|
312
|
+
'PORT' => '3030',
|
313
|
+
'LOG_LEVEL' => 'debug'
|
314
|
+
})
|
315
|
+
.build
|
316
|
+
.to_s
|
317
|
+
|
318
|
+
# => PORT=3030 LOG_LEVEL=debug node ./server.js
|
319
|
+
```
|
169
320
|
|
170
|
-
|
171
|
-
# and arguments
|
321
|
+
or as an array:
|
172
322
|
|
173
|
-
|
323
|
+
```ruby
|
324
|
+
Lino::CommandLineBuilder.for_command('node')
|
325
|
+
.with_environment_variables(
|
326
|
+
[
|
327
|
+
{ name: 'PORT', value: '3030' },
|
328
|
+
{ name: 'LOG_LEVEL', value: 'debug' }
|
329
|
+
]
|
330
|
+
)
|
331
|
+
.build
|
332
|
+
.to_s
|
333
|
+
|
334
|
+
# => PORT=3030 LOG_LEVEL=debug node ./server.js
|
335
|
+
```
|
336
|
+
|
337
|
+
#### Option Placement
|
338
|
+
|
339
|
+
By default, `lino` places top-level options after the command, before all
|
340
|
+
subcommands and arguments.
|
341
|
+
|
342
|
+
This is equivalent to calling `#with_options_after_command`:
|
343
|
+
|
344
|
+
```ruby
|
174
345
|
Lino::CommandLineBuilder.for_command('gcloud')
|
175
346
|
.with_options_after_command
|
176
347
|
.with_option('--password', 'super-secure')
|
177
|
-
.with_subcommands(%w[sql instances set-root-password
|
348
|
+
.with_subcommands(%w[sql instances set-root-password])
|
178
349
|
.build
|
179
350
|
.to_s
|
180
351
|
|
181
|
-
# => gcloud --password super-secure sql instances set-root-password
|
352
|
+
# => gcloud --password super-secure sql instances set-root-password
|
353
|
+
```
|
354
|
+
|
355
|
+
Alternatively, top-level options can be placed after all subcommands using
|
356
|
+
`#with_options_after_subcommands`:
|
182
357
|
|
183
|
-
|
358
|
+
```ruby
|
184
359
|
Lino::CommandLineBuilder.for_command('gcloud')
|
185
360
|
.with_options_after_subcommands
|
186
361
|
.with_option('--password', 'super-secure')
|
187
|
-
.with_subcommands(%w[sql instances set-root-password
|
362
|
+
.with_subcommands(%w[sql instances set-root-password])
|
188
363
|
.build
|
189
364
|
.to_s
|
190
365
|
|
191
|
-
# => gcloud sql instances set-root-password
|
366
|
+
# => gcloud sql instances set-root-password --password super-secure
|
367
|
+
```
|
368
|
+
|
369
|
+
or, after all arguments, using `#with_options_after_arguments`:
|
192
370
|
|
193
|
-
|
371
|
+
```ruby
|
194
372
|
Lino::CommandLineBuilder.for_command('ls')
|
195
373
|
.with_options_after_arguments
|
196
374
|
.with_flag('-l')
|
@@ -201,6 +379,59 @@ Lino::CommandLineBuilder.for_command('ls')
|
|
201
379
|
# => ls /some/directory -l
|
202
380
|
```
|
203
381
|
|
382
|
+
#### Appliables
|
383
|
+
|
384
|
+
Command and subcommand builders both support passing 'appliables' that are
|
385
|
+
applied to the builder allowing an operation to be encapsulated in an object.
|
386
|
+
|
387
|
+
Given an appliable type:
|
388
|
+
|
389
|
+
```ruby
|
390
|
+
class AppliableOption
|
391
|
+
def initialize(option, value)
|
392
|
+
@option = option
|
393
|
+
@value = value
|
394
|
+
end
|
395
|
+
|
396
|
+
def apply(builder)
|
397
|
+
builder.with_option(@option, @value)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
```
|
401
|
+
|
402
|
+
an instance of the appliable can be applied using `#with_appliable`:
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
Lino::CommandLineBuilder.for_command('gpg')
|
406
|
+
.with_appliable(AppliableOption.new('--recipient', 'tobyclemson@gmail.com'))
|
407
|
+
.with_flag('--sign')
|
408
|
+
.with_argument('/some/file.txt')
|
409
|
+
.build
|
410
|
+
.to_s
|
411
|
+
|
412
|
+
# => gpg --recipient tobyclemson@gmail.com --sign /some/file.txt
|
413
|
+
```
|
414
|
+
|
415
|
+
or multiple with `#with_appliables`:
|
416
|
+
|
417
|
+
```ruby
|
418
|
+
Lino::CommandLineBuilder.for_command('gpg')
|
419
|
+
.with_appliables([
|
420
|
+
AppliableOption.new('--recipient', 'user@example.com'),
|
421
|
+
AppliableOption.new('--output', '/signed.txt')
|
422
|
+
])
|
423
|
+
.with_flag('--sign')
|
424
|
+
.with_argument('/file.txt')
|
425
|
+
.build
|
426
|
+
.to_s
|
427
|
+
|
428
|
+
# => gpg --recipient user@example.com --output /signed.txt --sign /file.txt
|
429
|
+
```
|
430
|
+
|
431
|
+
> Note: an 'appliable' is any object that has an `#apply` method.
|
432
|
+
|
433
|
+
> Note: `lino` ignores `nil` or empty appliables in the resulting command line.
|
434
|
+
|
204
435
|
### `Lino::CommandLine`
|
205
436
|
|
206
437
|
A `CommandLine` can be executed using the `#execute` method:
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'utilities'
|
4
|
+
|
5
|
+
module Lino
|
6
|
+
module Appliables
|
7
|
+
include Lino::Utilities
|
8
|
+
|
9
|
+
def with_appliable(appliable)
|
10
|
+
return self if missing?(appliable)
|
11
|
+
|
12
|
+
appliable.apply(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
def with_appliables(appliables)
|
16
|
+
return self if missing?(appliables)
|
17
|
+
|
18
|
+
appliables.inject(self) do |s, appliable|
|
19
|
+
s.with_appliable(appliable)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -5,6 +5,7 @@ require_relative 'utilities'
|
|
5
5
|
require_relative 'command_line'
|
6
6
|
require_relative 'subcommand_builder'
|
7
7
|
require_relative 'options'
|
8
|
+
require_relative 'appliables'
|
8
9
|
|
9
10
|
module Lino
|
10
11
|
# rubocop:disable Metrics/ClassLength
|
@@ -20,6 +21,7 @@ module Lino
|
|
20
21
|
|
21
22
|
include Lino::Utilities
|
22
23
|
include Lino::Options
|
24
|
+
include Lino::Appliables
|
23
25
|
|
24
26
|
class << self
|
25
27
|
def for_command(command)
|
@@ -50,6 +52,8 @@ module Lino
|
|
50
52
|
# rubocop:enable Metrics/ParameterLists
|
51
53
|
|
52
54
|
def with_subcommand(subcommand, &block)
|
55
|
+
return self if missing?(subcommand)
|
56
|
+
|
53
57
|
with(
|
54
58
|
subcommands: @subcommands.add(
|
55
59
|
(block || ->(sub) { sub }).call(
|
@@ -60,6 +64,8 @@ module Lino
|
|
60
64
|
end
|
61
65
|
|
62
66
|
def with_subcommands(subcommands, &block)
|
67
|
+
return self if missing?(subcommands)
|
68
|
+
|
63
69
|
without_block = subcommands[0...-1]
|
64
70
|
with_block = subcommands.last
|
65
71
|
|
@@ -111,6 +117,17 @@ module Lino
|
|
111
117
|
)
|
112
118
|
end
|
113
119
|
|
120
|
+
def with_environment_variables(environment_variables)
|
121
|
+
return self if missing?(environment_variables)
|
122
|
+
|
123
|
+
environment_variables.entries.inject(self) do |s, var|
|
124
|
+
s.with_environment_variable(
|
125
|
+
var.include?(:name) ? var[:name] : var[0],
|
126
|
+
var.include?(:value) ? var[:value] : var[1]
|
127
|
+
)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
114
131
|
def build
|
115
132
|
components = formatted_components
|
116
133
|
command_line = SELECTORS[@option_placement]
|
data/lib/lino/options.rb
CHANGED
@@ -3,11 +3,13 @@
|
|
3
3
|
require 'hamster'
|
4
4
|
require_relative 'utilities'
|
5
5
|
require_relative 'options'
|
6
|
+
require_relative 'appliables'
|
6
7
|
|
7
8
|
module Lino
|
8
9
|
class SubcommandBuilder
|
9
10
|
include Lino::Utilities
|
10
11
|
include Lino::Options
|
12
|
+
include Lino::Appliables
|
11
13
|
|
12
14
|
class <<self
|
13
15
|
def for_subcommand(subcommand)
|
data/lib/lino/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lino
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0.pre.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toby Clemson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04-
|
11
|
+
date: 2021-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hamster
|
@@ -222,6 +222,7 @@ files:
|
|
222
222
|
- bin/console
|
223
223
|
- bin/setup
|
224
224
|
- lib/lino.rb
|
225
|
+
- lib/lino/appliables.rb
|
225
226
|
- lib/lino/command_line.rb
|
226
227
|
- lib/lino/command_line_builder.rb
|
227
228
|
- lib/lino/options.rb
|