Class: Appear::Util::CommandBuilder
- Inherits:
-
Object
- Object
- Appear::Util::CommandBuilder
- Defined in:
- lib/appear/util/command_builder.rb
Overview
Builds command strings.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Override the == method.
-
#args(*args) ⇒ self
Add arguments to this command.
-
#dup ⇒ CommandBuilder
Duplicate this CommandBuilder instance.
-
#flag(name, val) ⇒ self
Add a flag to this command.
-
#flags(flag_map) ⇒ self
Add a bunch of flags at once, using a map of flag => argument.
-
#initialize(command, opts = {}) ⇒ CommandBuilder
constructor
A new instance of CommandBuilder.
-
#subcommand(name, opts = {}) {|subc| ... } ⇒ self
Add a subcommand, with its own flags arguments, after the current command.
-
#to_a ⇒ Array<String>
Render this command to an array of strings, suitable for execution with
systemor other methods that take an ARGV array. -
#to_s ⇒ String
Render this command as a string, suitable for execution with
shorsystemor other methods that take a command string.
Constructor Details
#initialize(command, opts = {}) ⇒ CommandBuilder
Returns a new instance of CommandBuilder.
24 25 26 27 28 29 30 31 32 |
# File 'lib/appear/util/command_builder.rb', line 24 def initialize(command, opts = {}) @command = command @flags = opts.delete(:flags) || Hash.new { |h, k| h[k] = [] } @argv = opts.delete(:argv) || [] = { :single_dash_long_flags => false, :dashdash_after_flags => false, }.merge(opts) end |
Instance Method Details
#==(other) ⇒ Object
Override the == method
136 137 138 |
# File 'lib/appear/util/command_builder.rb', line 136 def ==(other) other.class == self.class && other.state == self.state end |
#args(*args) ⇒ self
Add arguments to this command. Arguments always come after flags, and may be separated from flags with – if you pass :dashdash_after_flags option in the constructor.
72 73 74 75 |
# File 'lib/appear/util/command_builder.rb', line 72 def args(*args) @argv.concat(args) self end |
#dup ⇒ CommandBuilder
Duplicate this Appear::Util::CommandBuilder instance.
128 129 130 131 132 133 |
# File 'lib/appear/util/command_builder.rb', line 128 def dup opts = .dup opts[:argv] = @argv.dup opts[:flags] = @flags.dup self.class.new(@command.dup, opts) end |
#flag(name, val) ⇒ self
Add a flag to this command
40 41 42 43 |
# File 'lib/appear/util/command_builder.rb', line 40 def flag(name, val) @flags[name] << val self end |
#flags(flag_map) ⇒ self
Add a bunch of flags at once, using a map of flag => argument.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/appear/util/command_builder.rb', line 53 def flags(flag_map) flag_map.each do |f, v| if v.is_a?(Array) v.each do |v_prime| flag(f, v_prime) end else flag(f, v) end end self end |
#subcommand(name, opts = {}) {|subc| ... } ⇒ self
Add a subcommand, with its own flags arguments, after the current command. This is useful for eg building calls to nested commands.
90 91 92 93 94 95 96 97 |
# File 'lib/appear/util/command_builder.rb', line 90 def subcommand(name, opts = {}) # use our options as the defaults # then use the given options as the overrides subc = CommandBuilder.new(name, .merge(opts)) yield(subc) args(*subc.to_a) self end |
#to_a ⇒ Array<String>
Render this command to an array of strings, suitable for execution with system or other methods that take an ARGV array.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/appear/util/command_builder.rb', line 103 def to_a res = [@command].flatten @flags.each do |name, params| flag = flag_name_to_arg(name) params.each do |param| res << flag res << param.to_s unless param == true end end res << '--' if [:dashdash_after_flags] res.concat(@argv) res.map { |v| v.to_s } end |
#to_s ⇒ String
Render this command as a string, suitable for execution with sh or system or other methods that take a command string.
121 122 123 |
# File 'lib/appear/util/command_builder.rb', line 121 def to_s to_a.shelljoin end |