Class: DotOpts::Command
- Inherits:
-
Object
- Object
- DotOpts::Command
- Defined in:
- lib/dotopts/command.rb
Overview
Command class encapsulate a configuration for a given command and a given profile.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Command name.
Instance Method Summary collapse
-
#<<(entry) ⇒ Object
Add argument or environment entries to command.
-
#arguments ⇒ Array
Arguments.
-
#command? ⇒ Boolean
Does the command’s name match the current command?.
-
#current? ⇒ Boolean
Is the command applicable to the current command line?.
-
#environment ⇒ Hash
Environment settings.
-
#initialize(name, settings = {}) ⇒ void
constructor
Initialize new instance of Command.
-
#profile ⇒ String?
Profile designation.
-
#profile=(profile) ⇒ String?
Set profile designation.
-
#profile? ⇒ Boolean
Does the command’s profile match the current environment?.
Constructor Details
#initialize(name, settings = {}) ⇒ void
Initialize new instance of Command.
29 30 31 32 33 34 35 36 |
# File 'lib/dotopts/command.rb', line 29 def initialize(name, settings={}) @name = name self.profile = settings[:profile] @arguments = [] @environment = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
Command name. [String]
39 40 41 |
# File 'lib/dotopts/command.rb', line 39 def name @name end |
Instance Method Details
#<<(entry) ⇒ Object
Add argument or environment entries to command.
TODO: Is there too much “parsing” going on here?
Should some of this be in Parser instead?
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dotopts/command.rb', line 76 def <<(entry) entry = entry.strip if entry.start_with?('$ ') # environment entry.sub!(/\$\s+/, '') shellwords(entry).each do |s| name, value = s.split('=') @environment[name] = subenv(value) unless name.empty? end else # argument shellwords(entry).each do |s| @arguments << subenv(s) unless s.empty? end end end |
#arguments ⇒ Array
Arguments.
68 69 70 |
# File 'lib/dotopts/command.rb', line 68 def arguments @arguments end |
#command? ⇒ Boolean
Does the command’s name match the current command?
103 104 105 106 107 |
# File 'lib/dotopts/command.rb', line 103 def command? this = @name.split(' ') real = [DotOpts.command, *ARGV][0,this.size] this == real && ARGV.size < this.size # no other arguments end |
#current? ⇒ Boolean
Is the command applicable to the current command line?
96 97 98 |
# File 'lib/dotopts/command.rb', line 96 def current? command? && profile? end |
#environment ⇒ Hash
Environment settings.
61 62 63 |
# File 'lib/dotopts/command.rb', line 61 def environment @environment end |
#profile ⇒ String?
Profile designation.
44 45 46 |
# File 'lib/dotopts/command.rb', line 44 def profile @profile end |
#profile=(profile) ⇒ String?
Set profile designation.
54 55 56 |
# File 'lib/dotopts/command.rb', line 54 def profile=(profile) @profile = profile ? profile.to_str : nil #? shellwords(profile).first : nil end |
#profile? ⇒ Boolean
Does the command’s profile match the current environment?
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dotopts/command.rb', line 112 def profile? shellwords(profile || "").all? do |sw| case sw when /^\~/ true if Regexp.new(sw.sub('~','')) === (ENV['profile'] || ENV['p']).to_s when /=~/ name, value = sw.split('=~') #name = 'profile' if name.empty? true if Regexp.new(value) === ENV[name] when /=/ name, value = sw.split('=') #name = 'profile' if name.empty? true if subenv(value) == ENV[name] else true if sw.to_s == (ENV['profile'] || ENV['p']).to_s end end end |