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

#default_dotfileObject

:nodoc:



169
170
171
# File 'lib/capistrano/cli/options.rb', line 169

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

#default_sysconfObject

:nodoc:



165
166
167
# File 'lib/capistrano/cli/options.rb', line 165

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.



138
139
140
141
142
143
# File 'lib/capistrano/cli/options.rb', line 138

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

#home_directoryObject

:nodoc:



179
180
181
182
183
# File 'lib/capistrano/cli/options.rb', line 179

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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/capistrano/cli/options.rb', line 146

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
# 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.") do
      long_help
      exit
    end

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

    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",
      "List all tasks in the loaded recipe files."
    ) do 
      options[:tasks] = true
      options[:verbose] ||= 0
    end

    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."
    ) { options[:verbose] ||= 0; options[:verbose] += 1 }

    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.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/capistrano/cli/options.rb', line 110

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)

  # 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:



173
174
175
176
177
# File 'lib/capistrano/cli/options.rb', line 173

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