Class: Lino::CommandLineBuilder
Overview
rubocop:disable Metrics/ClassLength
Constant Summary
collapse
- SELECTORS =
{
after_command:
i[environment_variables command options subcommands arguments],
after_subcommands:
i[environment_variables command subcommands options arguments],
after_arguments:
i[environment_variables command subcommands arguments options]
}.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
-
#build ⇒ Object
-
#initialize(command: nil, subcommands: [], options: [], arguments: [], environment_variables: [], option_separator: ' ', option_quoting: nil, option_placement: :after_command) ⇒ CommandLineBuilder
constructor
rubocop:disable Metrics/ParameterLists.
-
#with_argument(argument) ⇒ Object
-
#with_arguments(arguments) ⇒ Object
-
#with_environment_variable(environment_variable, value) ⇒ Object
-
#with_environment_variables(environment_variables) ⇒ Object
-
#with_option_quoting(character) ⇒ Object
-
#with_option_separator(option_separator) ⇒ Object
-
#with_options_after_arguments ⇒ Object
-
#with_options_after_command ⇒ Object
-
#with_options_after_subcommands ⇒ Object
-
#with_subcommand(subcommand, &block) ⇒ Object
rubocop:enable Metrics/ParameterLists.
-
#with_subcommands(subcommands, &block) ⇒ Object
Methods included from Appliables
#with_appliable, #with_appliables
Methods included from Utilities
#join_with, #map_and_join, #missing?, #quote_with
Methods included from Options
#with_flag, #with_flags, #with_option, #with_options, #with_repeated_option
Constructor Details
#initialize(command: nil, subcommands: [], options: [], arguments: [], environment_variables: [], option_separator: ' ', option_quoting: nil, option_placement: :after_command) ⇒ CommandLineBuilder
rubocop:disable Metrics/ParameterLists
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/lino/command_line_builder.rb', line 33
def initialize(
command: nil,
subcommands: [],
options: [],
arguments: [],
environment_variables: [],
option_separator: ' ',
option_quoting: nil,
option_placement: :after_command
)
@command = command
@subcommands = Hamster::Vector.new(subcommands)
@options = Hamster::Vector.new(options)
@arguments = Hamster::Vector.new(arguments)
@environment_variables = Hamster::Vector.new(environment_variables)
@option_separator = option_separator
@option_quoting = option_quoting
@option_placement = option_placement
end
|
Class Method Details
.for_command(command) ⇒ Object
27
28
29
|
# File 'lib/lino/command_line_builder.rb', line 27
def for_command(command)
CommandLineBuilder.new(command: command)
end
|
Instance Method Details
#build ⇒ Object
131
132
133
134
135
136
137
138
139
|
# File 'lib/lino/command_line_builder.rb', line 131
def build
components = formatted_components
command_line = SELECTORS[@option_placement]
.inject([]) { |c, key| c << components[key] }
.reject(&:empty?)
.join(' ')
CommandLine.new(command_line)
end
|
#with_argument(argument) ⇒ Object
97
98
99
100
101
|
# File 'lib/lino/command_line_builder.rb', line 97
def with_argument(argument)
return self if missing?(argument)
with(arguments: @arguments.add({ components: [argument] }))
end
|
#with_arguments(arguments) ⇒ Object
103
104
105
106
107
|
# File 'lib/lino/command_line_builder.rb', line 103
def with_arguments(arguments)
return self if missing?(arguments)
arguments.inject(self) { |s, argument| s.with_argument(argument) }
end
|
#with_environment_variable(environment_variable, value) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/lino/command_line_builder.rb', line 109
def with_environment_variable(environment_variable, value)
with(
environment_variables:
@environment_variables.add(
[
environment_variable, value
]
)
)
end
|
#with_environment_variables(environment_variables) ⇒ Object
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/lino/command_line_builder.rb', line 120
def with_environment_variables(environment_variables)
return self if missing?(environment_variables)
environment_variables.entries.inject(self) do |s, var|
s.with_environment_variable(
var.include?(:name) ? var[:name] : var[0],
var.include?(:value) ? var[:value] : var[1]
)
end
end
|
#with_option_quoting(character) ⇒ Object
81
82
83
|
# File 'lib/lino/command_line_builder.rb', line 81
def with_option_quoting(character)
with(option_quoting: character)
end
|
#with_option_separator(option_separator) ⇒ Object
77
78
79
|
# File 'lib/lino/command_line_builder.rb', line 77
def with_option_separator(option_separator)
with(option_separator: option_separator)
end
|
#with_options_after_arguments ⇒ Object
93
94
95
|
# File 'lib/lino/command_line_builder.rb', line 93
def with_options_after_arguments
with(option_placement: :after_arguments)
end
|
#with_options_after_command ⇒ Object
85
86
87
|
# File 'lib/lino/command_line_builder.rb', line 85
def with_options_after_command
with(option_placement: :after_command)
end
|
#with_options_after_subcommands ⇒ Object
89
90
91
|
# File 'lib/lino/command_line_builder.rb', line 89
def with_options_after_subcommands
with(option_placement: :after_subcommands)
end
|
#with_subcommand(subcommand, &block) ⇒ Object
rubocop:enable Metrics/ParameterLists
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/lino/command_line_builder.rb', line 54
def with_subcommand(subcommand, &block)
return self if missing?(subcommand)
with(
subcommands: @subcommands.add(
(block || ->(sub) { sub }).call(
SubcommandBuilder.for_subcommand(subcommand)
)
)
)
end
|
#with_subcommands(subcommands, &block) ⇒ Object
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/lino/command_line_builder.rb', line 66
def with_subcommands(subcommands, &block)
return self if missing?(subcommands)
without_block = subcommands[0...-1]
with_block = subcommands.last
without_block
.inject(self) { |s, sc| s.with_subcommand(sc) }
.with_subcommand(with_block, &block)
end
|