lino 1.7.0 → 1.8.0.pre.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/Gemfile.lock +2 -2
- data/README.md +30 -14
- data/lib/lino/command_line_builder.rb +50 -43
- data/lib/lino/option_flag_mixin.rb +36 -0
- data/lib/lino/subcommand_builder.rb +14 -27
- data/lib/lino/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9451d4d5a1cac65706c718503cc44813da8e4d3aa2397c0469c7d6474c96336
|
4
|
+
data.tar.gz: 39aacdb97179f720a9bb440a3e79a9114ef3678428e2b3a7b4611bcf2cd999bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a588ae04311413b5d1aeecc166977927f8c1761949b0ede6674b0ffad10b4fc61455c0cbedfe66d35ef126e507594138276da8c14154dde1c8e67c571855059e
|
7
|
+
data.tar.gz: 172aa6befaf57bbecf308a02e0ea01bdf3f43421badbbd34337ca098d7975939d981ecfb6199127297a894107ef120a2556dd4f1c91988cf5957658f100a54f6
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -49,37 +49,53 @@ Lino::CommandLineBuilder.for_command('ls')
|
|
49
49
|
.with_flag('-a')
|
50
50
|
.build
|
51
51
|
.to_s
|
52
|
-
|
52
|
+
|
53
53
|
# => ls -l -a
|
54
|
-
|
54
|
+
|
55
55
|
# commands with options
|
56
56
|
Lino::CommandLineBuilder.for_command('gpg')
|
57
57
|
.with_option('--recipient', 'tobyclemson@gmail.com')
|
58
58
|
.with_option('--sign', './doc.txt')
|
59
59
|
.build
|
60
|
-
.to_s
|
61
|
-
|
60
|
+
.to_s
|
61
|
+
|
62
62
|
# => gpg --recipient tobyclemson@gmail.com --sign ./doc.txt
|
63
|
-
|
64
|
-
# commands with
|
63
|
+
|
64
|
+
# commands with an option repeated multiple times (nil and empty option values are ignored)
|
65
|
+
Lino::CommandLineBuilder.for_command('example.sh')
|
66
|
+
.with_repeated_option('--opt', ['file1.txt', nil, '', 'file2.txt'])
|
67
|
+
.build
|
68
|
+
.to_s
|
69
|
+
|
70
|
+
# => example.sh --opt file1.txt --opt file2.txt
|
71
|
+
|
72
|
+
# commands with arguments
|
65
73
|
Lino::CommandLineBuilder.for_command('diff')
|
66
74
|
.with_argument('./file1.txt')
|
67
75
|
.with_argument('./file2.txt')
|
68
76
|
.build
|
69
|
-
.to_s
|
70
|
-
|
77
|
+
.to_s
|
78
|
+
|
71
79
|
# => diff ./file1.txt ./file2.txt
|
72
|
-
|
80
|
+
|
81
|
+
# commands with an array of arguments (nil and empty arguments are ignored)
|
82
|
+
Lino::CommandLineBuilder.for_command('diff')
|
83
|
+
.with_arguments(['./file1.txt', nil, '', './file2.txt'])
|
84
|
+
.build
|
85
|
+
.to_s
|
86
|
+
|
87
|
+
# => diff ./file1.txt ./file2.txt
|
88
|
+
|
73
89
|
# commands with custom option separator
|
74
90
|
Lino::CommandLineBuilder.for_command('java')
|
75
91
|
.with_option_separator(':')
|
76
92
|
.with_option('-splash', './images/splash.jpg')
|
77
93
|
.with_argument('./application.jar')
|
78
94
|
.build
|
79
|
-
.to_s
|
80
|
-
|
95
|
+
.to_s
|
96
|
+
|
81
97
|
# => java -splash:./images/splash.jpg ./application.jar
|
82
|
-
|
98
|
+
|
83
99
|
# commands using a subcommand style
|
84
100
|
Lino::CommandLineBuilder.for_command('git')
|
85
101
|
.with_flag('--no-pager')
|
@@ -88,9 +104,9 @@ Lino::CommandLineBuilder.for_command('git')
|
|
88
104
|
end
|
89
105
|
.build
|
90
106
|
.to_s
|
91
|
-
|
107
|
+
|
92
108
|
# => git --no-pager log --since 2016-01-01
|
93
|
-
|
109
|
+
|
94
110
|
# commands with multiple levels of subcommand
|
95
111
|
Lino::CommandLineBuilder.for_command('gcloud')
|
96
112
|
.with_subcommand('sql')
|
@@ -2,10 +2,12 @@ require 'hamster'
|
|
2
2
|
require_relative 'utilities'
|
3
3
|
require_relative 'command_line'
|
4
4
|
require_relative 'subcommand_builder'
|
5
|
+
require_relative 'option_flag_mixin'
|
5
6
|
|
6
7
|
module Lino
|
7
8
|
class CommandLineBuilder
|
8
9
|
include Lino::Utilities
|
10
|
+
include Lino::OptionFlagMixin
|
9
11
|
|
10
12
|
class <<self
|
11
13
|
def for_command(command)
|
@@ -14,13 +16,14 @@ module Lino
|
|
14
16
|
end
|
15
17
|
|
16
18
|
def initialize(
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
command: nil,
|
20
|
+
subcommands: [],
|
21
|
+
switches: [],
|
22
|
+
arguments: [],
|
23
|
+
environment_variables: [],
|
24
|
+
option_separator: ' ',
|
25
|
+
option_quoting: nil
|
26
|
+
)
|
24
27
|
@command = command
|
25
28
|
@subcommands = Hamster::Vector.new(subcommands)
|
26
29
|
@switches = Hamster::Vector.new(switches)
|
@@ -31,16 +34,13 @@ module Lino
|
|
31
34
|
end
|
32
35
|
|
33
36
|
def with_subcommand(subcommand, &block)
|
34
|
-
with(
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
separator: separator,
|
42
|
-
quoting: quoting
|
43
|
-
}))
|
37
|
+
with(
|
38
|
+
subcommands: @subcommands.add(
|
39
|
+
(block || ->(sub) { sub }).call(
|
40
|
+
SubcommandBuilder.for_subcommand(subcommand)
|
41
|
+
)
|
42
|
+
)
|
43
|
+
)
|
44
44
|
end
|
45
45
|
|
46
46
|
def with_option_separator(option_separator)
|
@@ -51,12 +51,13 @@ module Lino
|
|
51
51
|
with(option_quoting: character)
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
55
|
-
with(
|
54
|
+
def with_argument(argument)
|
55
|
+
with(arguments: add_argument(argument))
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
59
|
-
|
58
|
+
def with_arguments(arguments)
|
59
|
+
arguments.each { |argument| add_argument(argument) }
|
60
|
+
with({})
|
60
61
|
end
|
61
62
|
|
62
63
|
def with_environment_variable(environment_variable, value)
|
@@ -65,41 +66,47 @@ module Lino
|
|
65
66
|
|
66
67
|
def build
|
67
68
|
components = [
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
69
|
+
map_and_join(@environment_variables) do |var|
|
70
|
+
"#{var[0]}=\"#{var[1].to_s.gsub(/"/, '\\"')}\""
|
71
|
+
end,
|
72
|
+
@command,
|
73
|
+
map_and_join(
|
74
|
+
@switches,
|
75
|
+
&(quote_with(@option_quoting) >> join_with(@option_separator))
|
76
|
+
),
|
77
|
+
map_and_join(@subcommands) do |sub|
|
78
|
+
sub.build(@option_separator, @option_quoting)
|
79
|
+
end,
|
80
|
+
map_and_join(@arguments, &join_with(' '))
|
78
81
|
]
|
79
82
|
|
80
|
-
command_string = components
|
81
|
-
.reject { |item| item.empty? }
|
82
|
-
.join(' ')
|
83
|
+
command_string = components.reject(&:empty?).join(' ')
|
83
84
|
|
84
85
|
CommandLine.new(command_string)
|
85
86
|
end
|
86
87
|
|
87
88
|
private
|
88
89
|
|
89
|
-
def with
|
90
|
+
def with(**replacements)
|
90
91
|
CommandLineBuilder.new(**state.merge(replacements))
|
91
92
|
end
|
92
93
|
|
93
94
|
def state
|
94
95
|
{
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
96
|
+
command: @command,
|
97
|
+
subcommands: @subcommands,
|
98
|
+
switches: @switches,
|
99
|
+
arguments: @arguments,
|
100
|
+
environment_variables: @environment_variables,
|
101
|
+
option_separator: @option_separator,
|
102
|
+
option_quoting: @option_quoting
|
102
103
|
}
|
103
104
|
end
|
105
|
+
|
106
|
+
def add_argument(argument)
|
107
|
+
return @arguments if missing?(argument)
|
108
|
+
|
109
|
+
@arguments = @arguments.add({ components: [argument] })
|
110
|
+
end
|
104
111
|
end
|
105
|
-
end
|
112
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Lino
|
2
|
+
module OptionFlagMixin
|
3
|
+
def with_option(switch, value, separator: nil, quoting: nil)
|
4
|
+
with(switches: add_option(switch, value, separator, quoting))
|
5
|
+
end
|
6
|
+
|
7
|
+
def with_repeated_option(switch, values, separator: nil, quoting: nil)
|
8
|
+
values.each do |value|
|
9
|
+
add_option(switch, value, separator, quoting)
|
10
|
+
end
|
11
|
+
with({})
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_flag(flag)
|
15
|
+
with(switches: @switches.add({ components: [flag] }))
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def add_option(switch, value, separator, quoting)
|
21
|
+
return @switches if missing?(value)
|
22
|
+
|
23
|
+
@switches = @switches.add(
|
24
|
+
{
|
25
|
+
components: [switch, value],
|
26
|
+
separator: separator,
|
27
|
+
quoting: quoting
|
28
|
+
}
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
def missing?(value)
|
33
|
+
value.nil? || (value.respond_to?(:empty?) && value.empty?)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,58 +1,45 @@
|
|
1
1
|
require 'hamster'
|
2
2
|
require_relative 'utilities'
|
3
|
+
require_relative 'option_flag_mixin'
|
3
4
|
|
4
5
|
module Lino
|
5
6
|
class SubcommandBuilder
|
6
7
|
include Lino::Utilities
|
8
|
+
include Lino::OptionFlagMixin
|
7
9
|
|
8
10
|
class <<self
|
9
|
-
def for_subcommand
|
11
|
+
def for_subcommand(subcommand)
|
10
12
|
SubcommandBuilder.new(subcommand: subcommand)
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
def initialize(
|
15
|
-
subcommand: nil,
|
16
|
-
switches: [])
|
16
|
+
def initialize(subcommand: nil, switches: [])
|
17
17
|
@subcommand = subcommand
|
18
18
|
@switches = Hamster::Vector.new(switches)
|
19
19
|
end
|
20
20
|
|
21
|
-
def with_option(switch, value, separator: nil, quoting: nil)
|
22
|
-
with(switches: @switches.add({
|
23
|
-
components: [switch, value],
|
24
|
-
separator: separator,
|
25
|
-
quoting: quoting
|
26
|
-
}))
|
27
|
-
end
|
28
|
-
|
29
|
-
def with_flag(flag)
|
30
|
-
with(switches: @switches.add({components: [flag]}))
|
31
|
-
end
|
32
|
-
|
33
21
|
def build(option_separator, option_quoting)
|
34
22
|
components = [
|
35
|
-
|
36
|
-
|
37
|
-
|
23
|
+
@subcommand,
|
24
|
+
map_and_join(
|
25
|
+
@switches,
|
26
|
+
&(quote_with(option_quoting) >> join_with(option_separator))
|
27
|
+
)
|
38
28
|
]
|
39
|
-
|
40
|
-
components
|
41
|
-
.reject { |item| item.empty? }
|
42
|
-
.join(' ')
|
29
|
+
components.reject(&:empty?).join(' ')
|
43
30
|
end
|
44
31
|
|
45
32
|
private
|
46
33
|
|
47
|
-
def with
|
34
|
+
def with(**replacements)
|
48
35
|
SubcommandBuilder.new(**state.merge(replacements))
|
49
36
|
end
|
50
37
|
|
51
38
|
def state
|
52
39
|
{
|
53
|
-
|
54
|
-
|
40
|
+
subcommand: @subcommand,
|
41
|
+
switches: @switches
|
55
42
|
}
|
56
43
|
end
|
57
44
|
end
|
58
|
-
end
|
45
|
+
end
|
data/lib/lino/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lino
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0.pre.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toby Clemson
|
@@ -182,6 +182,7 @@ files:
|
|
182
182
|
- lib/lino.rb
|
183
183
|
- lib/lino/command_line.rb
|
184
184
|
- lib/lino/command_line_builder.rb
|
185
|
+
- lib/lino/option_flag_mixin.rb
|
185
186
|
- lib/lino/subcommand_builder.rb
|
186
187
|
- lib/lino/utilities.rb
|
187
188
|
- lib/lino/version.rb
|
@@ -200,9 +201,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
200
201
|
version: '2.6'
|
201
202
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
202
203
|
requirements:
|
203
|
-
- - "
|
204
|
+
- - ">"
|
204
205
|
- !ruby/object:Gem::Version
|
205
|
-
version:
|
206
|
+
version: 1.3.1
|
206
207
|
requirements: []
|
207
208
|
rubygems_version: 3.0.1
|
208
209
|
signing_key:
|