diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-17 22:25:06 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-17 22:25:06 -0700 |
| commit | 7e9b45d604148d48b90e84bb091d751f3d393874 (patch) | |
| tree | 72b3a0ad1359136ab60a3837c145878e22468681 /lib/puppet | |
| parent | eeb82f8b2d5c5a3e98d54d7a23ad3e5dbcbae39d (diff) | |
| parent | 9d2ec219bbd77bfca48a72b52fe5d0d3fcc0dcf7 (diff) | |
| download | puppet-7e9b45d604148d48b90e84bb091d751f3d393874.tar.gz puppet-7e9b45d604148d48b90e84bb091d751f3d393874.tar.xz puppet-7e9b45d604148d48b90e84bb091d751f3d393874.zip | |
Merge branch 'bug/2.7.x/7013-interfaces-should-be-able-to-mark-options-as-required' into 2.7.x
Diffstat (limited to 'lib/puppet')
| -rw-r--r-- | lib/puppet/interface/action.rb | 16 | ||||
| -rw-r--r-- | lib/puppet/interface/option.rb | 6 | ||||
| -rw-r--r-- | lib/puppet/interface/option_builder.rb | 6 |
3 files changed, 22 insertions, 6 deletions
diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb index b94298963..412e39449 100644 --- a/lib/puppet/interface/action.rb +++ b/lib/puppet/interface/action.rb @@ -83,10 +83,10 @@ class Puppet::Interface::Action # idea how motivated we were to make this cleaner. Sorry. --daniel 2011-03-31 internal_name = "#{@name} implementation, required on Ruby 1.8".to_sym - file = __FILE__ + "+eval" - line = __LINE__ + 1 + file = __FILE__ + "+eval" + line = __LINE__ + 1 wrapper = <<WRAPPER -def #{@name}(*args, &block) +def #{@name}(*args) if args.last.is_a? Hash then options = args.last else @@ -94,6 +94,7 @@ def #{@name}(*args, &block) end action = get_action(#{name.inspect}) + action.validate_args(args) __invoke_decorations(:before, action, args, options) rval = self.__send__(#{internal_name.inspect}, *args) __invoke_decorations(:after, action, args, options) @@ -142,6 +143,15 @@ WRAPPER option end + def validate_args(args) + required = options.map do |name| + get_option(name) + end.select(&:required?).collect(&:name) - args.last.keys + + return if required.empty? + raise ArgumentError, "missing required options (#{required.join(', ')})" + end + ######################################################################## # Support code for action decoration; see puppet/interface.rb for the gory # details of why this is hidden away behind private. --daniel 2011-04-15 diff --git a/lib/puppet/interface/option.rb b/lib/puppet/interface/option.rb index 2abcd4033..c04c2bf67 100644 --- a/lib/puppet/interface/option.rb +++ b/lib/puppet/interface/option.rb @@ -74,10 +74,12 @@ class Puppet::Interface::Option def optional_argument? !!@optional_argument end - + def required? + !!@required + end attr_reader :parent, :name, :aliases, :optparse - attr_accessor :desc + attr_accessor :required, :desc attr_accessor :before_action def before_action=(proc) diff --git a/lib/puppet/interface/option_builder.rb b/lib/puppet/interface/option_builder.rb index d4e59a4df..7c2ab89de 100644 --- a/lib/puppet/interface/option_builder.rb +++ b/lib/puppet/interface/option_builder.rb @@ -11,7 +11,7 @@ class Puppet::Interface::OptionBuilder def initialize(face, *declaration, &block) @face = face @option = Puppet::Interface::Option.new(face, *declaration) - block and instance_eval(&block) + instance_eval(&block) if block_given? @option end @@ -47,4 +47,8 @@ class Puppet::Interface::OptionBuilder end @option.after_action = block end + + def required(value = true) + @option.required = value + end end |
