Class: UltraCommandLine::Commands::OptionDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ultra_command_line/commands/option_definition.rb

Constant Summary collapse

DEFAULT_SUMMARY =
'Option summary not provided.'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ OptionDefinition

Returns a new instance of OptionDefinition.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ultra_command_line/commands/option_definition.rb', line 11

def initialize(name, type, options = {})
  @name = name
  @type = type
  @summary = options.fetch(:summary, DEFAULT_SUMMARY)
  @default = options.fetch(:default, nil)
  @description = options.fetch(:description, summary)
  @help = options.fetch(:help, description)
  @global = options.fetch(:global, false)
  @dependencies = options.fetch(:dependencies, [])
  self.dependencies = dependencies.is_a?(Array) ? dependencies : [dependencies]
  @incompatibilities = options.fetch(:incompatibilities, [])
  self.incompatibilities = incompatibilities.is_a?(Array) ? incompatibilities : [incompatibilities]
  short_aliases = options.fetch(:short_aliases, [])
  long_aliases = options.fetch(:long_aliases, [])
  self.short_aliases = (short_aliases.is_a?(Array) ? short_aliases : [short_aliases]).map &:to_sym
  self.long_aliases = (long_aliases.is_a?(Array) ? long_aliases : [long_aliases]).map &:to_sym
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



9
10
11
# File 'lib/ultra_command_line/commands/option_definition.rb', line 9

def default
  @default
end

#dependenciesObject

Returns the value of attribute dependencies.



9
10
11
# File 'lib/ultra_command_line/commands/option_definition.rb', line 9

def dependencies
  @dependencies
end

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/ultra_command_line/commands/option_definition.rb', line 9

def description
  @description
end

#helpObject

Returns the value of attribute help.



9
10
11
# File 'lib/ultra_command_line/commands/option_definition.rb', line 9

def help
  @help
end

#incompatibilitiesObject

Returns the value of attribute incompatibilities.



9
10
11
# File 'lib/ultra_command_line/commands/option_definition.rb', line 9

def incompatibilities
  @incompatibilities
end

#long_aliasesObject

Returns the value of attribute long_aliases.



8
9
10
# File 'lib/ultra_command_line/commands/option_definition.rb', line 8

def long_aliases
  @long_aliases
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ultra_command_line/commands/option_definition.rb', line 7

def name
  @name
end

#short_aliasesObject

Returns the value of attribute short_aliases.



8
9
10
# File 'lib/ultra_command_line/commands/option_definition.rb', line 8

def short_aliases
  @short_aliases
end

#summaryObject

Returns the value of attribute summary.



9
10
11
# File 'lib/ultra_command_line/commands/option_definition.rb', line 9

def summary
  @summary
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/ultra_command_line/commands/option_definition.rb', line 7

def type
  @type
end

Instance Method Details

#global?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ultra_command_line/commands/option_definition.rb', line 44

def global?
  @global
end

#help_line(slop_options = UltraCommandLine.new_slop_options) ⇒ Object



61
62
63
# File 'lib/ultra_command_line/commands/option_definition.rb', line 61

def help_line(slop_options = UltraCommandLine.new_slop_options)
  to_slop_options(slop_options).to_s.gsub /\n/, ''
end

#to_slop_options(slop_options = UltraCommandLine.new_slop_options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ultra_command_line/commands/option_definition.rb', line 48

def to_slop_options(slop_options = UltraCommandLine.new_slop_options)
  one_letter_options = short_aliases.map.each { |option| "-#{option}" }
  word_options = long_aliases.map.each { |option| "--#{option}" }
  # The main option as last entry before description
  option_def = word_options.concat(one_letter_options) << "--#{name}" << summary
  option_def.concat [{ default: default }] unless default.nil?
  slop_options.send type, *option_def
  slop_options.banner = nil
  slop_options
rescue NoMethodError => e
  raise UltraCommandLine::Error, "Invalid option type '#{e.name}' for option '#{name}' !"
end