The Args class exists to make it more convenient to work with command line arguments intended for git from within the Hub codebase.
The ARGV array is converted into an Args instance by the Hub instance when instantiated.
# File lib/hub/args.rb, line 11 def initialize(*args) super @executable = ENV["GIT"] || "git" @skip = @noop = false @original_args = args.first @chain = [nil] end
# File lib/hub/args.rb, line 68 def add_exec_flags(flags) self.executable = Array(executable).concat(flags) end
Adds an `after` callback. A callback can be a command or a proc.
# File lib/hub/args.rb, line 21 def after(cmd_or_args = nil, args = nil, &block) @chain.insert(-1, normalize_callback(cmd_or_args, args, block)) end
Adds a `before` callback. A callback can be a command or a proc.
# File lib/hub/args.rb, line 27 def before(cmd_or_args = nil, args = nil, &block) @chain.insert(@chain.index(nil), normalize_callback(cmd_or_args, args, block)) end
Tells if there are multiple (chained) commands or not.
# File lib/hub/args.rb, line 32 def chained? @chain.size > 1 end
Tests if arguments were modified since instantiation
# File lib/hub/args.rb, line 91 def changed? chained? or self != @original_args end
Returns an array of all commands.
# File lib/hub/args.rb, line 37 def commands chain = @chain.dup chain[chain.index(nil)] = self.to_exec chain end
All the flags (as opposed to words) contained in this argument list.
args = ::new([ 'remote', 'add', '-f', 'tekkub' ]) args.flags == [ '-f' ]
# File lib/hub/args.rb, line 86 def flags self - words end
# File lib/hub/args.rb, line 95 def has_flag?(*flags) pattern = flags.flatten.map { |f| Regexp.escape(f) }.join('|') !grep(/^#{pattern}(?:=|$)/).empty? end
Mark that this command shouldn't really run.
# File lib/hub/args.rb, line 54 def noop! @noop = true end
# File lib/hub/args.rb, line 58 def noop? @noop end
Skip running this command.
# File lib/hub/args.rb, line 44 def skip! @skip = true end
Boolean indicating whether this command will run.
# File lib/hub/args.rb, line 49 def skip? @skip end
Array of `executable` followed by all args suitable as arguments for `exec` or `system` calls.
# File lib/hub/args.rb, line 64 def to_exec(args = self) Array(executable) + args end
All the words (as opposed to flags) contained in this argument list.
args = ::new([ 'remote', 'add', '-f', 'tekkub' ]) args.words == [ 'remote', 'add', 'tekkub' ]
# File lib/hub/args.rb, line 77 def words reject { |arg| arg.index('-') == 0 } end
# File lib/hub/args.rb, line 102 def normalize_callback(cmd_or_args, args, block) if block block elsif args [cmd_or_args].concat args elsif Array === cmd_or_args self.to_exec cmd_or_args elsif cmd_or_args cmd_or_args else raise ArgumentError, "command or block required" end end