class DotOpts::Command

Command class encapsulate a configuration for a given command and a given profile.

Attributes

name[R]

Command name. [String]

Public Class Methods

new(name, settings={}) click to toggle source

Initialize new instance of Command.

@param [String] name

The name of the command. Can include subcommand, e.g. `yard doc`.

@option settings [String,nil] :profile

The profile for which this command configuation would be applicable.

@return [void]

# File lib/dotopts/command.rb, line 29
def initialize(name, settings={})
  @name = name

  self.profile = settings[:profile]

  @arguments   = []
  @environment = {}
end

Public Instance Methods

<<(entry) click to toggle source

Add argument or environment entries to command.

TODO: Is there too much “parsing” going on here?

Should some of this be in Parser instead?
# 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() click to toggle source

Arguments.

@return [Array]

# File lib/dotopts/command.rb, line 68
def arguments
  @arguments
end
command?() click to toggle source

Does the command’s name match the current command?

@return [Boolean]

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

Is the command applicable to the current command line?

@return [Boolean]

# File lib/dotopts/command.rb, line 96
def current?
  command? && profile?
end
environment() click to toggle source

Environment settings.

@return [Hash]

# File lib/dotopts/command.rb, line 61
def environment
  @environment
end
profile() click to toggle source

Profile designation.

@return [String,nil]

# File lib/dotopts/command.rb, line 44
def profile
  @profile
end
profile=(profile) click to toggle source

Set profile designation.

@param [String,nil]

The profile designation.

@return [String,nil]

# File lib/dotopts/command.rb, line 54
def profile=(profile)
  @profile = profile ? profile.to_str : nil  #? shellwords(profile).first : nil
end
profile?() click to toggle source

Does the command’s profile match the current environment?

@return [Boolean]

# 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

Private Instance Methods

shellwords(value) click to toggle source

Split a string up into shellwords.

@return [Array]

# File lib/dotopts/command.rb, line 136
def shellwords(value)
  Shellwords.shellwords(value)
end
subenv(value) click to toggle source

Substitute environment variables.

@return [String]

# File lib/dotopts/command.rb, line 143
def subenv(value)
  value.gsub(/\$(\w+)/){ |m| ENV[$1] }
end