Class: MGit::ARGV::Opt

Inherits:
Object
  • Object
show all
Defined in:
lib/m-git/argv/opt.rb

Overview

单个选项, 如‘–k1=v1 –k2 v2 v3 –flag’ ‘–k1=v1’ key: ‘–k1’, value: ‘v1’ ‘–k2 v2 v3’ key: ‘–k2’, value: [‘v2’, ‘v3’] ‘–flag’ key: ‘–flag’, value: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, default: nil, short_key: nil, priority: -1,, info: nil, type: :array) ⇒ Opt

Returns a new instance of Opt.



21
22
23
24
25
# File 'lib/m-git/argv/opt.rb', line 21

def initialize(key, default:nil, short_key:nil, priority:-1, info:nil, type: :array)
  raise("初始化Opt选项必须有key") if key.nil?
  @key, @value, @short_key, @priority, @info = key, default, short_key, priority, info
  @value_type = type
end

Instance Attribute Details

#infoObject

参数说明



15
16
17
# File 'lib/m-git/argv/opt.rb', line 15

def info
  @info
end

#keyObject (readonly)

Returns the value of attribute key.



11
12
13
# File 'lib/m-git/argv/opt.rb', line 11

def key
  @key
end

#priorityObject

参数解析优先级



14
15
16
# File 'lib/m-git/argv/opt.rb', line 14

def priority
  @priority
end

#short_keyObject

短参数格式,如’–command’对应的‘-c’



13
14
15
# File 'lib/m-git/argv/opt.rb', line 13

def short_key
  @short_key
end

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/m-git/argv/opt.rb', line 12

def value
  @value
end

#value_typeObject

:array :string :boolean



19
20
21
# File 'lib/m-git/argv/opt.rb', line 19

def value_type
  @value_type
end

#value值类型Object

:array :string :boolean



19
# File 'lib/m-git/argv/opt.rb', line 19

attr_accessor :value_type

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/m-git/argv/opt.rb', line 27

def empty?
  value.nil? || value == '' || value == [] || value == false
end

#validate?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/m-git/argv/opt.rb', line 31

def validate?
  return false if empty?
  value.is_a?(value_type)
end