Class: DotOpts::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/dotopts/command.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, settings = {}) ⇒ void

Initialize new instance of Command.

Parameters:

  • name (String)

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

  • settings (Hash) (defaults to: {})

    a customizable set of options

Options Hash (settings):

  • :profile (String, nil)

    The profile for which this command configuation would be applicable.



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

#nameObject (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

#argumentsArray

Arguments.

Returns:

  • (Array)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


96
97
98
# File 'lib/dotopts/command.rb', line 96

def current?
  command? && profile?
end

#environmentHash

Environment settings.

Returns:

  • (Hash)


61
62
63
# File 'lib/dotopts/command.rb', line 61

def environment
  @environment
end

#profileString?

Profile designation.

Returns:

  • (String, nil)


44
45
46
# File 'lib/dotopts/command.rb', line 44

def profile
  @profile
end

#profile=(profile) ⇒ String?

Set profile designation.

Parameters:

  • The (String, nil)

    profile designation.

Returns:

  • (String, nil)


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?

Returns:

  • (Boolean)


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