Class: Awaaz::Utils::ShellCommandBuilder
- Inherits:
-
Object
- Object
- Awaaz::Utils::ShellCommandBuilder
- Defined in:
- lib/awaaz/utils/shell_command_builder.rb
Overview
A utility class to construct shell commands in a safe and composable way.
This class supports chaining methods to add arguments, flags, and options, and finally output the full shell command string.
Instance Method Summary collapse
-
#add_arg(arg) ⇒ ShellCommandBuilder
(also: #add_flag)
Adds a positional argument to the command.
-
#add_multiple_args(args_array) ⇒ ShellCommandBuilder
Adds multiple arguments or options at once.
-
#add_option(option, value, with_colon: false) ⇒ ShellCommandBuilder
Adds an option with a value to the command.
-
#command ⇒ String
Builds and returns the complete shell command string.
-
#initialize(base = nil) ⇒ ShellCommandBuilder
constructor
Initializes a new shell command builder.
Constructor Details
#initialize(base = nil) ⇒ ShellCommandBuilder
Initializes a new shell command builder.
25 26 27 |
# File 'lib/awaaz/utils/shell_command_builder.rb', line 25 def initialize(base = nil) @args = [base.to_s] end |
Instance Method Details
#add_arg(arg) ⇒ ShellCommandBuilder Also known as: add_flag
Adds a positional argument to the command.
35 36 37 38 |
# File 'lib/awaaz/utils/shell_command_builder.rb', line 35 def add_arg(arg) @args << arg.to_s.strip self end |
#add_multiple_args(args_array) ⇒ ShellCommandBuilder
Adds multiple arguments or options at once.
85 86 87 88 89 90 91 92 |
# File 'lib/awaaz/utils/shell_command_builder.rb', line 85 def add_multiple_args(args_array) args_array.each do |shell_args| arg_type, args = shell_args arg_type = arg_type.to_sym i[arg flag].include?(arg_type) ? add_arg(*args) : add_option(*args) end self end |
#add_option(option, value, with_colon: false) ⇒ ShellCommandBuilder
Adds an option with a value to the command.
63 64 65 66 |
# File 'lib/awaaz/utils/shell_command_builder.rb', line 63 def add_option(option, value, with_colon: false) @args << "#{option}#{with_colon ? ":" : " "}#{value}" self end |
#command ⇒ String
Builds and returns the complete shell command string.
99 100 101 |
# File 'lib/awaaz/utils/shell_command_builder.rb', line 99 def command @args.join(" ") end |