Class: Hub::Args
- Inherits:
-
Array
- Object
- Array
- Hub::Args
- 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
-
#executable ⇒ Object
Returns the value of attribute executable.
Instance Method Summary collapse
- #add_exec_flags(flags) ⇒ Object
-
#after(cmd_or_args = nil, args = nil, &block) ⇒ Object
Adds an ‘after` callback.
-
#before(cmd_or_args = nil, args = nil, &block) ⇒ Object
Adds a ‘before` callback.
-
#chained? ⇒ Boolean
Tells if there are multiple (chained) commands or not.
-
#changed? ⇒ Boolean
Tests if arguments were modified since instantiation.
-
#commands ⇒ Object
Returns an array of all commands.
-
#flags ⇒ Object
All the flags (as opposed to words) contained in this argument list.
- #has_flag?(*flags) ⇒ Boolean
-
#initialize(*args) ⇒ Args
constructor
A new instance of Args.
-
#noop! ⇒ Object
Mark that this command shouldn’t really run.
- #noop? ⇒ Boolean
-
#skip! ⇒ Object
Skip running this command.
-
#skip? ⇒ Boolean
Boolean indicating whether this command will run.
-
#to_exec(args = self) ⇒ Object
Array of ‘executable` followed by all args suitable as arguments for `exec` or `system` calls.
-
#words ⇒ Object
All the words (as opposed to flags) contained in this argument list.
Constructor Details
#initialize(*args) ⇒ Args
Returns a new instance of Args.
11 12 13 14 15 16 17 |
# 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 |
Instance Attribute Details
#executable ⇒ Object
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
68 69 70 |
# 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) ⇒ Object
Adds an ‘after` callback. A callback can be a command or a proc.
21 22 23 |
# 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) ⇒ Object
Adds a ‘before` callback. A callback can be a command or a proc.
27 28 29 |
# 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? ⇒ Boolean
Tells if there are multiple (chained) commands or not.
32 33 34 |
# File 'lib/hub/args.rb', line 32 def chained? @chain.size > 1 end |
#changed? ⇒ Boolean
Tests if arguments were modified since instantiation
91 92 93 |
# File 'lib/hub/args.rb', line 91 def changed? chained? or self != @original_args end |
#commands ⇒ Object
Returns an array of all commands.
37 38 39 40 41 |
# File 'lib/hub/args.rb', line 37 def commands chain = @chain.dup chain[chain.index(nil)] = self.to_exec chain end |
#flags ⇒ Object
All the flags (as opposed to words) contained in this argument list.
args = Args.new([ ‘remote’, ‘add’, ‘-f’, ‘tekkub’ ]) args.flags == [ ‘-f’ ]
86 87 88 |
# File 'lib/hub/args.rb', line 86 def flags self - words end |
#has_flag?(*flags) ⇒ Boolean
95 96 97 98 |
# 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! ⇒ Object
Mark that this command shouldn’t really run.
54 55 56 |
# File 'lib/hub/args.rb', line 54 def noop! @noop = true end |
#noop? ⇒ Boolean
58 59 60 |
# File 'lib/hub/args.rb', line 58 def noop? @noop end |
#skip! ⇒ Object
Skip running this command.
44 45 46 |
# File 'lib/hub/args.rb', line 44 def skip! @skip = true end |
#skip? ⇒ Boolean
Boolean indicating whether this command will run.
49 50 51 |
# File 'lib/hub/args.rb', line 49 def skip? @skip end |
#to_exec(args = self) ⇒ Object
Array of ‘executable` followed by all args suitable as arguments for `exec` or `system` calls.
64 65 66 |
# File 'lib/hub/args.rb', line 64 def to_exec(args = self) Array(executable) + args end |
#words ⇒ Object
All the words (as opposed to flags) contained in this argument list.
args = Args.new([ ‘remote’, ‘add’, ‘-f’, ‘tekkub’ ]) args.words == [ ‘remote’, ‘add’, ‘tekkub’ ]
77 78 79 |
# File 'lib/hub/args.rb', line 77 def words reject { |arg| arg.index('-') == 0 } end |