Class: MGit::ARGV

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

Overview

参数处理类

Defined Under Namespace

Modules: Parser Classes: Opt, OptList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, pure_opts, absolute_cmd, raw_opts) ⇒ ARGV

Returns a new instance of ARGV.



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

def initialize(cmd, pure_opts, absolute_cmd, raw_opts)
  @cmd = cmd
  @pure_opts = pure_opts
  @absolute_cmd = absolute_cmd
  @raw_opts = raw_opts
  @git_opts = []
end

Instance Attribute Details

#absolute_cmdObject (readonly)

完整指令,如:“mgit checkout -b branch_name”



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

def absolute_cmd
  @absolute_cmd
end

#cmdObject (readonly)

指令名,如:“mgit checkout -b branch_name”的“checkout”



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

def cmd
  @cmd
end

#opt_listObject (readonly)

所有已注册的参数列表



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

def opt_list
  @opt_list
end

#pure_optsObject (readonly)

所有参数,如:“mgit checkout -b branch_name”的“checkout -b branch_name”



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

def pure_opts
  @pure_opts
end

#raw_optsObject (readonly)

本次传入的mgit指令中自定义的部分,如:“mgit checkout -b branch_name –mrepo boxapp BBAAccount –command test”的“[[–mrepo boxapp BBAAccount],[–command test]]”



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

def raw_opts
  @raw_opts
end

#valueObject (readonly)

更新指令值



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/m-git/argv.rb', line 59

def update_opt(key, value, priority:nil, info:nil)
  return unless @opt_list.did_register_opt?(key)
  opt = @opt_list.registered_opt(key)
  case opt.value_type
  when Array
    opt.value = Array(value)
  when String
    opt.value = value.is_a?(Array) ? value.first.to_s : value.to_s
  else # boolean
    opt.value = value
  end

  opt.priority = priority if !priority.nil?
  opt.info = info if info.is_a?(String)
end

Instance Method Details

#enumerate_valid_optsObject

遍历本次调用中传入过值(或有默认值)的选项,未传入值且无默认值则不遍历



102
103
104
105
106
107
# File 'lib/m-git/argv.rb', line 102

def enumerate_valid_opts
  @opt_list.opts_ordered_by_priority.each { |opt|
    next unless @opt_list.did_set_opt?(opt.key)
    yield(opt) if block_given?
  }
end

#git_opts(raw: true) ⇒ Type

获取原生git指令(非自定义的指令)

Parameters:

  • raw (Boolean) (defaults to: true)

    default: true,true:用空格拼接成一个字符串返回。false:直接返回数组,如‘-k k1 k2’ -> 【‘-k’,‘k1’,‘k2’】

Returns:

  • (Type)

    description_of_returned_object



92
93
94
95
96
97
98
99
# File 'lib/m-git/argv.rb', line 92

def git_opts(raw: true)
  return @git_opts unless raw
  opts = []
  @git_opts.each { |e_arr|
    opts += e_arr
  }
  opts.join(' ')
end

#info(key) ⇒ Object

获取某个option的描述信息



81
82
83
84
# File 'lib/m-git/argv.rb', line 81

def info(key)
  return '' unless @opt_list.did_register_opt?(key)
  @opt_list.registered_opt(key)&.info
end

#is_option?(opt_str) ⇒ Boolean

判断一个字符串是否是option(以‘–’或‘-’开头)

Returns:

  • (Boolean)


110
111
112
# File 'lib/m-git/argv.rb', line 110

def is_option?(opt_str)
  (opt_str =~ /-/) == 0 || (opt_str =~ /--/) == 0
end

#opt(key) ⇒ Object

获取某个option



76
77
78
# File 'lib/m-git/argv.rb', line 76

def opt(key)
  @opt_list.opt(key)
end

#register_opts(opts) ⇒ Object



38
39
40
41
# File 'lib/m-git/argv.rb', line 38

def register_opts(opts)
  return if opts.nil?
  @opt_list = OptList.new(opts)
end

#resolve!Object

注册解析指令



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/m-git/argv.rb', line 44

def resolve!
  @raw_opts.each { |raw_opt|
    next if @opt_list.did_register_opt?(raw_opt.first)
    @git_opts.push(raw_opt)
  }

  @raw_opts -= @git_opts

  __resolve_git_opts
  __resolve_raw_opts
end

#show_detailObject

输出本次指令的具体值信息,调试时使用



115
116
117
118
119
120
121
122
123
# File 'lib/m-git/argv.rb', line 115

def show_detail
  @opt_list.opts.each { |opt|
    puts '======='
    puts "key:#{opt.key}"
    puts "value:#{opt.value}"
    puts "info:#{opt.info}"
    puts "\n"
  }
end

#show_infoObject

输出参数说明信息



126
127
128
129
130
131
132
133
# File 'lib/m-git/argv.rb', line 126

def show_info
  @opt_list.opts.each { |opt|
    short_key = "#{opt.short_key}, " if !opt.short_key.nil?
    puts "\n"
    puts Output.blue_message("[#{short_key}#{opt.key}]")
    puts "#{opt.info}"
  }
end

#update_opt(key, value, priority: nil, info: nil) ⇒ Object

更新指令值



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/m-git/argv.rb', line 59

def update_opt(key, value, priority:nil, info:nil)
  return unless @opt_list.did_register_opt?(key)
  opt = @opt_list.registered_opt(key)
  case opt.value_type
  when Array
    opt.value = Array(value)
  when String
    opt.value = value.is_a?(Array) ? value.first.to_s : value.to_s
  else # boolean
    opt.value = value
  end

  opt.priority = priority if !priority.nil?
  opt.info = info if info.is_a?(String)
end