Module: Capistrano::CLI::Options

Included in:
Capistrano::CLI
Defined in:
lib/capistrano/cli/options.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

The hash of (parsed) command-line options



21
22
23
# File 'lib/capistrano/cli/options.rb', line 21

def options
  @options
end

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/capistrano/cli/options.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#coerce_variable(value) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/capistrano/cli/options.rb', line 210

def coerce_variable(value)
  case value
  when /^"(.*)"$/ then $1
  when /^'(.*)'$/ then $1
  when /^\d+$/ then value.to_i
  when /^\d+\.\d*$/ then value.to_f
  when "true" then true
  when "false" then false
  when "nil" then nil
  else value
  end
end

#coerce_variable_types!Object



202
203
204
205
206
207
208
# File 'lib/capistrano/cli/options.rb', line 202

def coerce_variable_types!
  [:pre_vars, :vars].each do |collection|
    options[collection].keys.each do |key|
      options[collection][key] = coerce_variable(options[collection][key])
    end
  end
end

#default_dotfileObject

:nodoc:



186
187
188
# File 'lib/capistrano/cli/options.rb', line 186

def default_dotfile #:nodoc:
  File.join(home_directory, ".caprc")
end

#default_sysconfObject

:nodoc:



182
183
184
# File 'lib/capistrano/cli/options.rb', line 182

def default_sysconf #:nodoc:
  File.join(sysconf_directory, "capistrano.conf")
end

#extract_environment_variables!Object

Extracts name=value pairs from the remaining command-line arguments and assigns them as environment variables.



155
156
157
158
159
160
# File 'lib/capistrano/cli/options.rb', line 155

def extract_environment_variables! #:nodoc:
  args.delete_if do |arg|
    next unless arg.match(/^(\w+)=(.*)$/)
    ENV[$1] = $2
  end
end

#home_directoryObject

:nodoc:



196
197
198
199
200
# File 'lib/capistrano/cli/options.rb', line 196

def home_directory #:nodoc:
  ENV["HOME"] ||
    (ENV["HOMEPATH"] && "#{ENV["HOMEDRIVE"]}#{ENV["HOMEPATH"]}") ||
    "/"
end

#look_for_default_recipe_file!Object

Looks for a default recipe file in the current directory.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/capistrano/cli/options.rb', line 163

def look_for_default_recipe_file! #:nodoc:
  current = Dir.pwd

  loop do
    %w(Capfile capfile).each do |file|
      if File.file?(file)
        options[:recipes] << file
        return
      end
    end

    pwd = Dir.pwd
    Dir.chdir("..")
    break if pwd == Dir.pwd # if changing the directory made no difference, then we're at the top
  end

  Dir.chdir(current)
end

#option_parserObject

Return an OptionParser instance that defines the acceptable command line switches for Capistrano, and what their corresponding behaviors are.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/capistrano/cli/options.rb', line 26

def option_parser #:nodoc:
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options] action ..."

    opts.on("-d", "--debug",
      "Prompts before each remote command execution."
    ) { |value| options[:debug] = true }

    opts.on("-e", "--explain TASK",
      "Displays help (if available) for the task."
    ) { |value| options[:explain] = value }

    opts.on("-F", "--default-config",
      "Always use default config, even with -f."
    ) { options[:default_config] = true }

    opts.on("-f", "--file FILE",
      "A recipe file to load. May be given more than once."
    ) { |value| options[:recipes] << value }

    opts.on("-H", "--long-help", "Explain these options and environment variables.") do
      long_help
      exit
    end

    opts.on("-h", "--help", "Display this help message.") do
      puts opts
      exit
    end

    opts.on("-n", "--dry-run",
      "Prints out commands without running them."
    ) { |value| options[:dry_run] = true }

    opts.on("-p", "--password",
      "Immediately prompt for the password."
    ) { options[:password] = nil }

    opts.on("-q", "--quiet",
      "Make the output as quiet as possible."
    ) { options[:verbose] = 0 }

    opts.on("-S", "--set-before NAME=VALUE",
      "Set a variable before the recipes are loaded."
    ) do |pair|
      name, value = pair.split(/=/, 2)
      options[:pre_vars][name.to_sym] = value
    end

    opts.on("-s", "--set NAME=VALUE",
      "Set a variable after the recipes are loaded."
    ) do |pair|
      name, value = pair.split(/=/, 2)
      options[:vars][name.to_sym] = value
    end

    opts.on("-T", "--tasks [PATTERN]",
      "List all tasks (matching optional PATTERN) in the loaded recipe files."
    ) do |value|
      options[:tasks] = if value
        value
      else
        true
      end
      options[:verbose] ||= 0
    end

    opts.on("-t", "--tool",
      "Abbreviates the output of -T for tool integration."
    ) { options[:tool] = true }

    opts.on("-V", "--version",
      "Display the Capistrano version, and exit."
    ) do
      require 'capistrano/version'
      puts "Capistrano v#{Capistrano::Version::STRING}"
      exit
    end

    opts.on("-v", "--verbose",
      "Be more verbose. May be given more than once."
    ) do
      options[:verbose] ||= 0
      options[:verbose] += 1
    end

    opts.on("-X", "--skip-system-config",
      "Don't load the system config file (capistrano.conf)"
    ) { options.delete(:sysconf) }

    opts.on("-x", "--skip-user-config",
      "Don't load the user config file (.caprc)"
    ) { options.delete(:dotfile) }
  end
end

#parse_options!Object

If the arguments to the command are empty, this will print the allowed options and exit. Otherwise, it will parse the command line and set up any default options.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/capistrano/cli/options.rb', line 125

def parse_options! #:nodoc:
  @options = { :recipes => [], :actions => [],
    :vars => {}, :pre_vars => {},
    :sysconf => default_sysconf, :dotfile => default_dotfile }

  if args.empty?
    warn "Please specify at least one action to execute."
    warn option_parser
    exit
  end

  option_parser.parse!(args)

  coerce_variable_types!

  # if no verbosity has been specified, be verbose
  options[:verbose] = 3 if !options.has_key?(:verbose)

  look_for_default_recipe_file! if options[:default_config] || options[:recipes].empty?
  extract_environment_variables!

  options[:actions].concat(args)

  password = options.has_key?(:password)
  options[:password] = Proc.new { self.class.password_prompt }
  options[:password] = options[:password].call if password
end

#sysconf_directoryObject

:nodoc:



190
191
192
193
194
# File 'lib/capistrano/cli/options.rb', line 190

def sysconf_directory #:nodoc:
  # TODO if anyone cares, feel free to submit a patch that uses a more
  # appropriate location for this file in Windows.
  ENV["SystemRoot"] || '/etc'
end