Class: MGit::ARGV::OptList

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

Overview

参数对象列表

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ OptList

attr_reader :valid_opts



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

def initialize(opts)
  @opts = opts
end

Instance Attribute Details

#optsObject

Array<ARGV::Opt>

参数对象数组



9
10
11
# File 'lib/m-git/argv/opt_list.rb', line 9

def opts
  @opts
end

Instance Method Details

#did_register_opt?(key) ⇒ Boolean

判断参数是否注册过

Returns:

  • (Boolean)

    参数是否注册过



56
57
58
# File 'lib/m-git/argv/opt_list.rb', line 56

def did_register_opt?(key)
  !registered_opt(key).nil?
end

#did_set_opt?(key) ⇒ Boolean

判断参数是否设置过值

Returns:

  • (Boolean)

    参数是否设置过值



36
37
38
# File 'lib/m-git/argv/opt_list.rb', line 36

def did_set_opt?(key)
  !opt(key).nil?
end

#opt(key) ⇒ ARGV::Opt Also known as: opt_with

获取某个参数对象

Parameters:

  • key (String)

    参数名,如‘–key’

Returns:

  • (ARGV::Opt)

    参数对象,若参数未设置过则返回nil



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

def opt(key)
  valid_opts.find { |e| (e.key == key || e.short_key == key) }
end

#opts_ordered_by_priorityArray<ARGV::Opt>

将参数根据优先级排序(逆序)后返回

Returns:

  • (Array<ARGV::Opt>)

    包含参数对象的数组



64
65
66
67
# File 'lib/m-git/argv/opt_list.rb', line 64

def opts_ordered_by_priority
  # 按照优先级进行降序排序
  opts.sort_by { |e| e.priority }.reverse
end

#registered_opt(key) ⇒ ARGV::Opt

返回某个注册过的参数对象

Parameters:

  • key (String)

    参数名,如‘–key’

Returns:

  • (ARGV::Opt)

    参数对象,无论参数是否设置过,只要注册过就返回



48
49
50
# File 'lib/m-git/argv/opt_list.rb', line 48

def registered_opt(key)
  @opts.find { |e| (e.key == key || e.short_key == key) }
end

#valid_optsObject



17
18
19
# File 'lib/m-git/argv/opt_list.rb', line 17

def valid_opts
  @opts.select { |e| !e.empty? }
end