class MGit::ARGV
参数处理类
Attributes
absolute_cmd[R]
完整指令,如:“mgit checkout -b branch_name”
cmd[R]
指令名,如:“mgit checkout -b branch_name”的“checkout”
opt_list[R]
所有已注册的参数列表
pure_opts[R]
所有参数,如:“mgit checkout -b branch_name”的“checkout -b branch_name”
raw_opts[R]
本次传入的mgit指令中自定义的部分,如:“mgit checkout -b branch_name –mrepo boxapp BBAAccount –command test”的“[[–mrepo boxapp BBAAccount],[–command test]]”
Public Class Methods
new(cmd, pure_opts, absolute_cmd, raw_opts)
click to toggle source
# 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
Public Instance Methods
enumerate_valid_opts() { |opt| ... }
click to toggle source
遍历本次调用中传入过值(或有默认值)的选项,未传入值且无默认值则不遍历
# 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)
click to toggle source
获取原生git指令(非自定义的指令)
@param raw [Boolean] default: true,true:用空格拼接成一个字符串返回。false:直接返回数组,如‘-k k1 k2’ -> 【'-k','k1','k2'】
@return [Type] description_of_returned_object
# 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)
click to toggle source
获取某个option的描述信息
# 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)
click to toggle source
判断一个字符串是否是option(以'–'或'-'开头)
# File lib/m-git/argv.rb, line 110 def is_option?(opt_str) (opt_str =~ /-/) == 0 || (opt_str =~ /--/) == 0 end
opt(key)
click to toggle source
获取某个option
# File lib/m-git/argv.rb, line 76 def opt(key) @opt_list.opt(key) end
register_opts(opts)
click to toggle source
# File lib/m-git/argv.rb, line 38 def register_opts(opts) return if opts.nil? @opt_list = OptList.new(opts) end
resolve!()
click to toggle source
注册解析指令
# 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_detail()
click to toggle source
输出本次指令的具体值信息,调试时使用
# 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_info()
click to toggle source
输出参数说明信息
# 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)
click to toggle source
更新指令值 @!attribute [Array / String / true / false] value
# 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
Private Instance Methods
__resolve_git_opts()
click to toggle source
# File lib/m-git/argv.rb, line 137 def __resolve_git_opts # 统一将值用双引号括起来,避免空格和特殊字符等引起错误 @git_opts.each { |e_arr| next unless is_option?(e_arr.first) e_arr.map!.with_index { |e, i| # 如果是 -- / - 开始的option,则增加"" 避免特殊字符的错误,eg: --yoo=ajsdaf => --yoo="ajsdaf" # 如果是 repo1 repo2 这样的option,则不作处理,eg: yoo ajsdaf => yoo ajsdaf e = "\"#{e}\"" if !is_option?(e) && i > 0 e } } end
__resolve_raw_opts()
click to toggle source
# File lib/m-git/argv.rb, line 150 def __resolve_raw_opts @raw_opts.each { |raw_opt| key = raw_opt.first opt = @opt_list.registered_opt(key) case opt.value_type when :boolean raise "参数#{key}格式错误,禁止传入参数值!(用法:#{key})" if raw_opt.count != 1 opt.value = true when :string raise "参数#{key}格式错误,只能传入一个参数值!(用法:#{key} xxx)" if raw_opt.count != 2 opt.value = raw_opt.last.to_s when :array raise "参数#{key}格式错误,至少传入一个参数值!(用法:#{key} xxx xxx ...)" if raw_opt.count < 2 opt.value = raw_opt[1..-1] end } end