Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Validate CLI options.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinko committed Nov 8, 2010
1 parent 848d950 commit 401f27b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
35 changes: 28 additions & 7 deletions lib/relish/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ module Command
class Base
extend Dsl

option :organization
option :project
option :api_token, :default => lambda { get_and_store_api_token }
option :host, :default => lambda { Relish.default_host }, :display => false

attr_writer :args
attr_reader :cli_options

def initialize(args = [])
@args = clean_args(args)
@param = get_param
@cli_options = Hash[*@args]

validate_cli_options
end

def url
Expand All @@ -26,12 +33,7 @@ def get_param
end

private

option :organization
option :project
option :api_token, :default => lambda { get_and_store_api_token }
option :host, :default => lambda { Relish.default_host }


def get_and_store_api_token
api_token = get_api_token
global_options_file.store('api_token' => api_token)
Expand All @@ -48,11 +50,30 @@ def get_api_token
def resource(options = {})
RestClient::Resource.new(url, options)
end

def clean_args(args)
args.inject([]) {|cleaned, arg| cleaned << arg.sub('--', '') }
end

def valid_option_names
self.class.option_names
end

def option_names_to_display
self.class.option_names_to_display
end

def validate_cli_options
@cli_options.keys.each do |option|
unless valid_option_names.include?(option.to_s)
puts "#{option} is not a valid option.\n" +
"Valid options are: #{option_names_to_display.sort.join(', ')}"

exit 1
end
end
end

def global_options_file
@global_options ||= OptionsFile.new(Relish.global_options_file)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/relish/commands/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def show
end

def add
File.open(Relish.local_options_file, 'a') do |f|
f.write(YAML::dump(Hash[*@args]))
end
OptionsFile.new(Relish.local_options_file).store(@cli_options)
end

end
Expand Down
23 changes: 18 additions & 5 deletions lib/relish/commands/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ module Command
module Dsl

def option(name, options = {})
default_proc = options[:default] || lambda {}
name = name.to_s
default_proc = options[:default] || Proc.new {}

define_method(name) do
cli_options[name.to_s] ||
local_options_file[name.to_s] ||
global_options_file[name.to_s] ||
instance_exec(&default_proc)
cli_options[name] ||
local_options_file[name] ||
global_options_file[name] ||
instance_exec(&default_proc)
end

option_names << name
option_names_to_display << name unless options[:display] == false
end

def option_names
@@option_names ||= []
end

def option_names_to_display
@@option_names_to_display ||= []
end

end
Expand Down

0 comments on commit 401f27b

Please sign in to comment.