Class: Hammer::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer/option.rb

Overview

Single option/flag definition.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **opts) ⇒ Option

Returns a new instance of Option.

Raises:

  • (Hammer::Error)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hammer/option.rb', line 9

def initialize(name, **opts)
  bad = opts.keys - ALLOWED_KEYS
  unless bad.empty?
    raise Hammer::Error,
          "unknown opt parameter(s) for :#{name}: #{bad.join(', ')}. " \
          "allowed: #{ALLOWED_KEYS.join(', ')}"
  end
  @name     = name.to_sym
  @type     = opts[:type] || :string
  @default  = opts[:default]
  @desc     = opts[:desc]
  @aliases  = Array(opts[:alias]).map { |a| Option.normalize_alias(a) }
  @required = opts[:req] ? true : false

  # Reserve -h / --help so every command supports them uniformly.
  if RESERVED_FLAGS.include?(switch)
    raise Hammer::Error, "opt :#{name} produces reserved flag #{switch.inspect} (used for help)"
  end
  clash = @aliases.find { |a| RESERVED_FLAGS.include?(a) }
  raise Hammer::Error, "alias #{clash.inspect} is reserved for help" if clash
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



4
5
6
# File 'lib/hammer/option.rb', line 4

def aliases
  @aliases
end

#defaultObject (readonly)

Returns the value of attribute default.



4
5
6
# File 'lib/hammer/option.rb', line 4

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



4
5
6
# File 'lib/hammer/option.rb', line 4

def desc
  @desc
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/hammer/option.rb', line 4

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



4
5
6
# File 'lib/hammer/option.rb', line 4

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/hammer/option.rb', line 4

def type
  @type
end

Class Method Details

.normalize_alias(value) ⇒ Object

‘:p` -> `-p`, `:dry_run` -> `–dry-run`. Strings starting with `-` pass through untouched.



33
34
35
36
37
# File 'lib/hammer/option.rb', line 33

def self.normalize_alias(value)
  s = value.to_s
  return s if s.start_with?('-')
  s.length == 1 ? "-#{s}" : "--#{s.tr('_', '-')}"
end

Instance Method Details

#annotationObject

“(default: dev)” or “(required)”



71
72
73
74
75
# File 'lib/hammer/option.rb', line 71

def annotation
  return '(required)' if required
  return '' if default.nil?
  "(default: #{default.inspect})"
end

#boolean?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/hammer/option.rb', line 39

def boolean?
  type == :boolean
end

#cast(value) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/hammer/option.rb', line 51

def cast(value)
  case type
  when :boolean then !!value && value != 'false' && value != '0'
  when :integer then Integer(value)
  when :float   then Float(value)
  when :array   then value.is_a?(Array) ? value : value.to_s.split(',')
  else value.to_s
  end
end

#negationObject



47
48
49
# File 'lib/hammer/option.rb', line 47

def negation
  "--no-#{name.to_s.tr('_', '-')}"
end

#switchObject



43
44
45
# File 'lib/hammer/option.rb', line 43

def switch
  "--#{name.to_s.tr('_', '-')}"
end

#usageObject



61
62
63
64
65
66
67
68
# File 'lib/hammer/option.rb', line 61

def usage
  flag = switch
  flag += " #{name.to_s.upcase}" unless boolean?
  line = aliases.empty? ? flag : "#{aliases.join(', ')}, #{flag}"
  suffix = annotation
  line = "#{line.ljust(28)} #{desc}" if desc
  suffix.empty? ? line : "#{line} #{suffix}"
end