Class: Coaster::CmdOptions
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#remain_args ⇒ Object
readonly
Returns the value of attribute remain_args.
-
#str ⇒ Object
readonly
Returns the value of attribute str.
-
#sub_cmd ⇒ Object
readonly
Returns the value of attribute sub_cmd.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(cmd, *args, **options) ⇒ CmdOptions
constructor
A new instance of CmdOptions.
- #merge(*args, **options) ⇒ Object
- #parser ⇒ Object
- #parser_proc(*args) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(cmd, *args, **options) ⇒ CmdOptions
Returns a new instance of CmdOptions.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/coaster/cmd_options.rb', line 74 def initialize(cmd, *args, **) = args. = .merge() @cmd = cmd @cmd, @sub_cmd = @cmd if @cmd.is_a?(Array) remain_ix = args.index{ |k| k == '--'} if remain_ix @remain_args = args[remain_ix+1..-1] @args = args[0...remain_ix] else @args = args end @args.delete_if do |arg| if arg.is_a?(self.class) = arg.to_h.merge() true else false end end ['--'] ||= [] ['--'] += @remain_args if @remain_args @options = @args << self.class.().strip @str = @args.join(' ') end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
72 73 74 |
# File 'lib/coaster/cmd_options.rb', line 72 def args @args end |
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
72 73 74 |
# File 'lib/coaster/cmd_options.rb', line 72 def cmd @cmd end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
72 73 74 |
# File 'lib/coaster/cmd_options.rb', line 72 def @options end |
#remain_args ⇒ Object (readonly)
Returns the value of attribute remain_args.
72 73 74 |
# File 'lib/coaster/cmd_options.rb', line 72 def remain_args @remain_args end |
#str ⇒ Object (readonly)
Returns the value of attribute str.
72 73 74 |
# File 'lib/coaster/cmd_options.rb', line 72 def str @str end |
#sub_cmd ⇒ Object (readonly)
Returns the value of attribute sub_cmd.
72 73 74 |
# File 'lib/coaster/cmd_options.rb', line 72 def sub_cmd @sub_cmd end |
Class Method Details
.option_v_to_s(option_v) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/coaster/cmd_options.rb', line 14 def option_v_to_s(option_v) case option_v when Hash then option_v.map{|vk,vv| Set[vk, vv]}.to_set when Array then option_v.map{|v| option_v_to_s(v)}.join(',') when Set then option_v.map{|v| option_v_to_s(v)}.join('=') else option_v = (option_v || '').to_s option_v = option_v.gsub(/"/, '\"') option_v = "\"#{option_v}\"" if option_v.include?(' ') option_v end end |
.options_h_to_s(options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/coaster/cmd_options.rb', line 27 def () opts = [] # multiple options can be passed by set .map do |k, v| if v.is_a?(Set) v.each {|set_v| opts << [k, set_v]} else opts << [k, v] end end targets = '' parsed = opts.map do |k, v| if k.start_with?(/--\w/) v = option_v_to_s(v) if v.is_a?(Set) v.map {|vv| "#{k}=#{option_v_to_s(vv)}" }.join(' ') else "#{k}#{v.length > 0 ? "=#{v}" : ''}" # ex, --config-env=<name>=<envvar> end elsif k.start_with?(/-\w/) v = option_v_to_s(v) if v.is_a?(Set) v.map {|vv| "#{k} #{option_v_to_s(vv)}"}.join(' ') else "#{k} #{v}" # ex, -c <name>=<value> end elsif k == '--' if v.present? v = Array.wrap(v) v = v.map{|e| option_v_to_s(e)}.join(' ') targets = "-- #{v}" # ex, -- <args> '' end else raise "Unknown option: #{k}" end end parsed << targets parsed.join(' ') end |
Instance Method Details
#merge(*args, **options) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/coaster/cmd_options.rb', line 119 def merge(*args, **) if args.first.is_a?(CmdOptions) other = args.shift else other = self.class.new([@cmd, @sub_cmd], *args, **) end self.class.new(to_h.merge(other.to_h)) end |
#parser ⇒ Object
105 106 107 108 |
# File 'lib/coaster/cmd_options.rb', line 105 def parser parser_proc = parser_proc(@cmd, @sub_cmd) instance_exec(&parser_proc) end |
#parser_proc(*args) ⇒ Object
101 102 103 |
# File 'lib/coaster/cmd_options.rb', line 101 def parser_proc(*args) raise 'Not implemented' end |
#to_h ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/coaster/cmd_options.rb', line 110 def to_h return @hash if defined?(@hash) @hash = {} remain_args = parser.parse!(@str.split(' ')) @hash['--'] = remain_args if remain_args.any? @hash end |
#to_s ⇒ Object
128 129 130 131 |
# File 'lib/coaster/cmd_options.rb', line 128 def to_s return self.class.(@hash) if defined?(@hash) @str end |