Class: Hammer::Option
- Inherits:
-
Object
- Object
- Hammer::Option
- Defined in:
- lib/hammer/option.rb
Overview
Single option/flag definition.
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.normalize_alias(value) ⇒ Object
‘:p` -> `-p`, `:dry_run` -> `–dry-run`.
Instance Method Summary collapse
-
#annotation ⇒ Object
“(default: dev)” or “(required)”.
- #boolean? ⇒ Boolean
- #cast(value) ⇒ Object
-
#initialize(name, **opts) ⇒ Option
constructor
A new instance of Option.
- #negation ⇒ Object
- #switch ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(name, **opts) ⇒ Option
Returns a new instance of Option.
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
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
4 5 6 |
# File 'lib/hammer/option.rb', line 4 def aliases @aliases end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
4 5 6 |
# File 'lib/hammer/option.rb', line 4 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
4 5 6 |
# File 'lib/hammer/option.rb', line 4 def desc @desc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/hammer/option.rb', line 4 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
4 5 6 |
# File 'lib/hammer/option.rb', line 4 def required @required end |
#type ⇒ Object (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
#annotation ⇒ Object
“(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
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 |
#negation ⇒ Object
47 48 49 |
# File 'lib/hammer/option.rb', line 47 def negation "--no-#{name.to_s.tr('_', '-')}" end |
#switch ⇒ Object
43 44 45 |
# File 'lib/hammer/option.rb', line 43 def switch "--#{name.to_s.tr('_', '-')}" end |
#usage ⇒ Object
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 |