diff options
| author | Markus Reiter | 2017-05-21 00:15:56 +0200 |
|---|---|---|
| committer | Markus Reiter | 2017-05-22 02:51:17 +0200 |
| commit | df1864ee435c1c48f87344800d2e7cf055a12f93 (patch) | |
| tree | 338018ac56614b5d2a8fe313fb8e1c0a36346032 /Library/Homebrew/cask/lib/hbc/cli/options.rb | |
| parent | debe4540e4ff76be80467a2bf32bfd32dbfc72eb (diff) | |
| download | brew-df1864ee435c1c48f87344800d2e7cf055a12f93.tar.bz2 | |
Add `CLI::Options` DSL.
Diffstat (limited to 'Library/Homebrew/cask/lib/hbc/cli/options.rb')
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/cli/options.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/cli/options.rb b/Library/Homebrew/cask/lib/hbc/cli/options.rb new file mode 100644 index 000000000..84126caa5 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/cli/options.rb @@ -0,0 +1,62 @@ +module Hbc + class CLI + module Options + def self.included(klass) + klass.extend(ClassMethods) + end + + module ClassMethods + def options + @options ||= {} + return @options unless superclass.respond_to?(:options) + superclass.options.merge(@options) + end + + def option(name, method, default_value = nil) + @options ||= {} + @options[name] = method + + return if method.respond_to?(:call) + + define_method(:"#{method}=") do |value| + instance_variable_set(:"@#{method}", value) + end + + if [true, false].include?(default_value) + define_method(:"#{method}?") do + instance_variable_get(:"@#{method}") == true + end + else + define_method(:"#{method}") do + instance_variable_get(:"@#{method}") + end + end + end + end + + def process_arguments(*arguments) + parser = OptionParser.new do |opts| + next if self.class.options.nil? + + self.class.options.each do |option_name, option_method| + option_type = case option_name.split(/(\ |\=)/).last + when "PATH" + Pathname + when /\w+(,\w+)+/ + Array + end + + opts.on(option_name, *option_type) do |value| + if option_method.respond_to?(:call) + option_method.call(value) + else + send(:"#{option_method}=", value) + end + end + end + end + parser.parse(*arguments) + end + end + end +end |
