Class: MGit::ARGV
- Inherits:
-
Object
- Object
- MGit::ARGV
- 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
-
#absolute_cmd ⇒ Object
readonly
完整指令,如:“mgit checkout -b branch_name”.
-
#cmd ⇒ Object
readonly
指令名,如:“mgit checkout -b branch_name”的“checkout”.
-
#opt_list ⇒ Object
readonly
所有已注册的参数列表.
-
#pure_opts ⇒ Object
readonly
所有参数,如:“mgit checkout -b branch_name”的“checkout -b branch_name”.
-
#raw_opts ⇒ Object
readonly
本次传入的mgit指令中自定义的部分,如:“mgit checkout -b branch_name –mrepo boxapp BBAAccount –command test”的“[[–mrepo boxapp BBAAccount],[–command test]]”.
-
#value ⇒ Object
readonly
更新指令值.
Instance Method Summary collapse
-
#enumerate_valid_opts ⇒ Object
遍历本次调用中传入过值(或有默认值)的选项,未传入值且无默认值则不遍历.
-
#git_opts(raw: true) ⇒ Type
获取原生git指令(非自定义的指令).
-
#info(key) ⇒ Object
获取某个option的描述信息.
-
#initialize(cmd, pure_opts, absolute_cmd, raw_opts) ⇒ ARGV
constructor
A new instance of ARGV.
-
#is_option?(opt_str) ⇒ Boolean
判断一个字符串是否是option(以‘–’或‘-’开头).
-
#opt(key) ⇒ Object
获取某个option.
- #register_opts(opts) ⇒ Object
-
#resolve! ⇒ Object
注册解析指令.
-
#show_detail ⇒ Object
输出本次指令的具体值信息,调试时使用.
-
#show_info ⇒ Object
输出参数说明信息.
-
#update_opt(key, value, priority: nil, info: nil) ⇒ Object
更新指令值.
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_cmd ⇒ Object (readonly)
完整指令,如:“mgit checkout -b branch_name”
19 20 21 |
# File 'lib/m-git/argv.rb', line 19 def absolute_cmd @absolute_cmd end |
#cmd ⇒ Object (readonly)
指令名,如:“mgit checkout -b branch_name”的“checkout”
13 14 15 |
# File 'lib/m-git/argv.rb', line 13 def cmd @cmd end |
#opt_list ⇒ Object (readonly)
所有已注册的参数列表
28 29 30 |
# File 'lib/m-git/argv.rb', line 28 def opt_list @opt_list end |
#pure_opts ⇒ Object (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_opts ⇒ Object (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 |
#value ⇒ Object (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_opts ⇒ Object
遍历本次调用中传入过值(或有默认值)的选项,未传入值且无默认值则不遍历
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指令(非自定义的指令)
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(以‘–’或‘-’开头)
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_detail ⇒ Object
输出本次指令的具体值信息,调试时使用
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_info ⇒ Object
输出参数说明信息
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.("[#{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 |