Class: Hub::Args

Inherits:
Array
  • Object
show all
Defined in:
lib/hub/args.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Args

Returns a new instance of Args.



11
12
13
14
15
16
17
18
# File 'lib/hub/args.rb', line 11

def initialize(*args)
  super
  @executable = ENV["GIT"] || "git"
  @after = nil
  @skip = @noop = false
  @original_args = args.first
  @chain = [nil]
end

Instance Attribute Details

#executableObject

Returns the value of attribute executable.



9
10
11
# File 'lib/hub/args.rb', line 9

def executable
  @executable
end

Instance Method Details

#add_exec_flags(flags) ⇒ Object



69
70
71
# File 'lib/hub/args.rb', line 69

def add_exec_flags(flags)
  self.executable = Array(executable).concat(flags)
end

#after(cmd_or_args = nil, args = nil, &block) ⇒ Object

Adds an ‘after` callback. A callback can be a command or a proc.



22
23
24
# File 'lib/hub/args.rb', line 22

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) ⇒ Object

Adds a ‘before` callback. A callback can be a command or a proc.



28
29
30
# File 'lib/hub/args.rb', line 28

def before(cmd_or_args = nil, args = nil, &block)
  @chain.insert(@chain.index(nil), normalize_callback(cmd_or_args, args, block))
end

#chained?Boolean

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/hub/args.rb', line 33

def chained?
  @chain.size > 1
end

#changed?Boolean

Tests if arguments were modified since instantiation

Returns:

  • (Boolean)


92
93
94
# File 'lib/hub/args.rb', line 92

def changed?
  chained? or self != @original_args
end

#commandsObject

Returns an array of all commands.



38
39
40
41
42
# File 'lib/hub/args.rb', line 38

def commands
  chain = @chain.dup
  chain[chain.index(nil)] = self.to_exec
  chain
end

#flagsObject

All the flags (as opposed to words) contained in this argument list.

args = Args.new([ ‘remote’, ‘add’, ‘-f’, ‘tekkub’ ]) args.flags == [ ‘-f’ ]



87
88
89
# File 'lib/hub/args.rb', line 87

def flags
  self - words
end

#has_flag?(*flags) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/hub/args.rb', line 96

def has_flag?(*flags)
  pattern = flags.flatten.map { |f| Regexp.escape(f) }.join('|')
  !grep(/^#{pattern}(?:=|$)/).empty?
end

#noop!Object

Mark that this command shouldn’t really run.



55
56
57
# File 'lib/hub/args.rb', line 55

def noop!
  @noop = true
end

#noop?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/hub/args.rb', line 59

def noop?
  @noop
end

#skip!Object

Skip running this command.



45
46
47
# File 'lib/hub/args.rb', line 45

def skip!
  @skip = true
end

#skip?Boolean

Boolean indicating whether this command will run.

Returns:

  • (Boolean)


50
51
52
# File 'lib/hub/args.rb', line 50

def skip?
  @skip
end

#to_exec(args = self) ⇒ Object

Array of ‘executable` followed by all args suitable as arguments for `exec` or `system` calls.



65
66
67
# File 'lib/hub/args.rb', line 65

def to_exec(args = self)
  Array(executable) + args
end

#wordsObject

All the words (as opposed to flags) contained in this argument list.

args = Args.new([ ‘remote’, ‘add’, ‘-f’, ‘tekkub’ ]) args.words == [ ‘remote’, ‘add’, ‘tekkub’ ]



78
79
80
# File 'lib/hub/args.rb', line 78

def words
  reject { |arg| arg.index('-') == 0 }
end