Class: Exec::CommandOption
- Inherits:
-
Object
- Object
- Exec::CommandOption
- Defined in:
- lib/exec/command_option.rb
Overview
This class is an helper to the ParserOption class which manages ans parse options given to a linux command.
Direct Known Subclasses
Exec::ClusterAllocate::CustomCommandOption, ExecutableCommand::CustomCommandOption, NodeInstall::CustomCommandOption, ServiceFollowRequest::CustomCommandOption, ServiceLs::CustomCommandOption
Defined Under Namespace
Classes: Option
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Get the values.
Instance Method Summary collapse
-
#add_option(short_name, long_name, description, mandatory, require_value = false, check_format = nil) ⇒ Object
Declare new option.
-
#check_mandatory ⇒ Object
protected
Default behaviour for commandeOption: raise an exception if a mandatory is missing.
-
#get_banner ⇒ Object
Generate a basic usage.
-
#get_help ⇒ Object
The option parser help.
-
#get_sorted_options ⇒ Array<Option>
A sorted array where mandatory are in first place.
-
#has_argument(arg_short_name, arg_long_name) ⇒ Object
Indicates if the command has arguments.
-
#initialize(command_name, argv) ⇒ CommandOption
constructor
Default constructor.
-
#initialize_option_values ⇒ Hash, OptionParser
private
Initialize the return hash.
-
#verify ⇒ Hash
This method verifies if given arguments are valid.
Constructor Details
#initialize(command_name, argv) ⇒ CommandOption
Default constructor
32 33 34 35 36 |
# File 'lib/exec/command_option.rb', line 32 def initialize(command_name, argv) @options = {} @argv = argv @command_name = command_name end |
Instance Attribute Details
#values ⇒ Object (readonly)
Get the values
17 18 19 |
# File 'lib/exec/command_option.rb', line 17 def values @values end |
Instance Method Details
#add_option(short_name, long_name, description, mandatory, require_value = false, check_format = nil) ⇒ Object
Declare new option
47 48 49 50 |
# File 'lib/exec/command_option.rb', line 47 def add_option(short_name, long_name, description, mandatory, require_value = false, check_format = nil) raise NameError.new("Duplicate option name") if @options.include?(long_name) @options.store(long_name, Option.new(short_name, long_name, description, mandatory, require_value, check_format)) end |
#check_mandatory ⇒ Object (protected)
You should override this method to implement a custom behaviour.
Default behaviour for commandeOption: raise an exception if a mandatory is missing.
130 131 132 133 134 135 136 137 |
# File 'lib/exec/command_option.rb', line 130 def check_mandatory @options.each_value do |option| if option.mandatory raise Common::MissingParameter.new("-#{option.short_name}", "Mandatory option.") if @values[option.long_name].nil? end end return true end |
#get_banner ⇒ Object
Should be overrided in inherited classes to generate a consistent banner.
Generate a basic usage. A banner generaly looks like : Usage: command_name -arg1 VALUE_1 [-optional1 OPT_VAL_1]
66 67 68 69 70 71 72 |
# File 'lib/exec/command_option.rb', line 66 def usage = "Usage: #@command_name " ().each do |option| usage += option.to_usage_s + " " end return usage end |
#get_help ⇒ Object
Returns The option parser help.
55 56 57 |
# File 'lib/exec/command_option.rb', line 55 def get_help return @opt_parser.help end |
#get_sorted_options ⇒ Array<Option>
Returns a sorted array where mandatory are in first place.
76 77 78 |
# File 'lib/exec/command_option.rb', line 76 def return @options.values.sort { |x, y| y.mandatory.to_s <=> x.mandatory.to_s } end |
#has_argument(arg_short_name, arg_long_name) ⇒ Object
Indicates if the command has arguments.
144 145 146 |
# File 'lib/exec/command_option.rb', line 144 def has_argument(arg_short_name, arg_long_name) return @argv.include?('-' + arg_short_name) || @argv.include?('--' + arg_long_name) end |
#initialize_option_values ⇒ Hash, OptionParser (private)
Initialize the return hash
152 153 154 155 156 157 158 159 |
# File 'lib/exec/command_option.rb', line 152 def initialize_option_values @values = {} # set to false all boolean options which are not already set @options.each_value do |option| @values.store(option.long_name, false) if !option.require_value end end |
#verify ⇒ Hash
This method verifies if given arguments are valid.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/exec/command_option.rb', line 84 def verify() initialize_option_values() @opt_parser = OptionParser.new do |opt| opt. = () opt.separator "Options" @options.each_value do |option| # adding the option value if option.require_value opt.on("-#{option.short_name}", "--#{option.long_name} #{option.value_name}", option.description) do |value| @values[option.long_name] = value end else opt.on("-#{option.short_name}", "--#{option.long_name}", option.description) do @values[option.long_name] = true end end end end begin @opt_parser.parse(@argv) rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e raise Common::InvalidParameter.new(e., @opt_parser.to_s) rescue OptionParser::ParseError => e raise Common::ParameterError.new(e., @opt_parser.to_s) end @options.each_value do |option| if @values.include?(option.long_name) && option.require_value && !option.check_format.nil? valid, msg = option.valid_value?(@values[option.long_name]) raise Common::InvalidParameter.new("-#{option.short_name}", "Bad Format: #{msg}") unless valid #end end end check_mandatory() return @values end |