class Hub::Args

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.

Attributes

executable[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# 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

Public Instance Methods

add_exec_flags(flags) click to toggle source
# File lib/hub/args.rb, line 68
def add_exec_flags(flags)
  self.executable = Array(executable).concat(flags)
end
after(cmd_or_args = nil, args = nil, &block) click to toggle source

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
before(cmd_or_args = nil, args = nil, &block) click to toggle source

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
chained?() click to toggle source

Tells if there are multiple (chained) commands or not.

# File lib/hub/args.rb, line 32
def chained?
  @chain.size > 1
end
changed?() click to toggle source

Tests if arguments were modified since instantiation

# File lib/hub/args.rb, line 91
def changed?
  chained? or self != @original_args
end
commands() click to toggle source

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
flags() click to toggle source

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
has_flag?(*flags) click to toggle source
# File lib/hub/args.rb, line 95
def has_flag?(*flags)
  pattern = flags.flatten.map { |f| Regexp.escape(f) }.join('|')
  !grep(/^#{pattern}(?:=|$)/).empty?
end
noop!() click to toggle source

Mark that this command shouldn't really run.

# File lib/hub/args.rb, line 54
def noop!
  @noop = true
end
noop?() click to toggle source
# File lib/hub/args.rb, line 58
def noop?
  @noop
end
skip!() click to toggle source

Skip running this command.

# File lib/hub/args.rb, line 44
def skip!
  @skip = true
end
skip?() click to toggle source

Boolean indicating whether this command will run.

# File lib/hub/args.rb, line 49
def skip?
  @skip
end
to_exec(args = self) click to toggle source

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
words() click to toggle source

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

Private Instance Methods

normalize_callback(cmd_or_args, args, block) click to toggle source
# 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