Class: Exec::CommandOption

Inherits:
Object
  • Object
show all
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.

Author:

  • tmarmin

Defined Under Namespace

Classes: Option

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name, argv) ⇒ CommandOption

Default constructor

Author:

  • tmarmin



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

#valuesObject (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

Parameters:

  • short_name (String)

    Short name (String.length=1) of the option

  • long_name (String)

    The long name of the option

  • description (String)

    Description of the option

  • mandatory (Boolean)

    If true verify method will raise an error if the option is not

  • require_value (Boolean) (defaults to: false)

    If true, a value is needed for this option.

  • check_format (Method) (defaults to: nil)

    A method which take only one string in parameter and return a boolean (it will be used to validate the option) in argv.

Raises:

  • (NameError)


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_mandatoryObject (protected)

Note:

You should override this method to implement a custom behaviour.

Default behaviour for commandeOption: raise an exception if a mandatory is missing.

Raises:



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_bannerObject

Note:

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]

Returns:

  • The help banner

Author:

  • tmarmin



66
67
68
69
70
71
72
# File 'lib/exec/command_option.rb', line 66

def get_banner
  usage = "Usage: #@command_name "
  get_sorted_options().each do |option|
    usage += option.to_usage_s + " "
  end
  return usage
end

#get_helpObject

Returns The option parser help.

Returns:

  • The option parser help.

Author:

  • tmarmin



55
56
57
# File 'lib/exec/command_option.rb', line 55

def get_help
  return @opt_parser.help
end

#get_sorted_optionsArray<Option>

Returns a sorted array where mandatory are in first place.

Returns:

  • (Array<Option>)

    a sorted array where mandatory are in first place.



76
77
78
# File 'lib/exec/command_option.rb', line 76

def get_sorted_options
  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.

Parameters:

  • arg_short_name

    The short named arguments.

  • arg_long_name

    The long named arguments.

Returns:

  • True if the command has arguments, false otherwise.



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_valuesHash, OptionParser (private)

Initialize the return hash

Returns:

  • (Hash, OptionParser)

    The values list and the OptionParser object

Author:

  • tmarmin



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

#verifyHash

This method verifies if given arguments are valid.

Returns:

  • (Hash)

    The arguments passed

Raises:



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.banner = get_banner()
    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.message, @opt_parser.to_s)
  rescue OptionParser::ParseError => e
    raise Common::ParameterError.new(e.message, @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