lib/cogy/command.rb in cogy-0.1.0 vs lib/cogy/command.rb in cogy-0.1.1
- old
+ new
@@ -5,19 +5,19 @@
# the result (ie. handler).
class Command
# The name of the command. Also used in {Cogy.bundle_config}.
#
# @return [String]
- attr :name
+ attr_reader :name
# The code that will run when the command is invoked
#
# @return [Proc]
- attr :handler
+ attr_reader :handler
# Attributes related to the bundle config in Cog
- attr :args, :opts, :desc, :long_desc, :examples, :rules
+ attr_reader :args, :opts, :desc, :long_desc, :examples, :rules
# See {Cogy.on}
def initialize(name, handler, args: [], opts: {}, desc:, long_desc: nil, examples: nil, rules: nil)
@name = name.to_s
@handler = handler
@@ -25,10 +25,12 @@
@opts = opts.with_indifferent_access
@desc = desc
@long_desc = long_desc
@examples = examples
@rules = rules || ["allow"]
+
+ validate_opts
end
# Registers a command.
#
# @raise [StandardError] if a command with the same name is already
@@ -53,8 +55,21 @@
# for displaying in a bundle config
def formatted_opts
# Convert to Hash in order to get rid of HashWithIndifferentAccess,
# otherwise the resulting YAML will contain garbage.
opts.to_hash
+ end
+
+ private
+
+ def validate_opts
+ opts.each do |k, v|
+ missing = [:type, :required] - v.keys.map(&:to_sym)
+
+ if !missing.empty?
+ raise ArgumentError,
+ "`#{name}`: Parameters #{missing} for `#{k}` option are missing"
+ end
+ end
end
end
end