Class: Coaster::CmdOptions

Inherits:
Object show all
Defined in:
lib/coaster/cmd_options.rb

Direct Known Subclasses

Git::Options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, **options)
  arg_options = args.extract_options!
  options = options.merge(arg_options)
  @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)
      options = arg.to_h.merge(options)
      true
    else
      false
    end
  end
  options['--'] ||= []
  options['--'] += @remain_args if @remain_args
  @options = options
  @args << self.class.options_to_s(options).strip
  @str = @args.join(' ')
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



72
73
74
# File 'lib/coaster/cmd_options.rb', line 72

def args
  @args
end

#cmdObject (readonly)

Returns the value of attribute cmd.



72
73
74
# File 'lib/coaster/cmd_options.rb', line 72

def cmd
  @cmd
end

#optionsObject (readonly)

Returns the value of attribute options.



72
73
74
# File 'lib/coaster/cmd_options.rb', line 72

def options
  @options
end

#remain_argsObject (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

#strObject (readonly)

Returns the value of attribute str.



72
73
74
# File 'lib/coaster/cmd_options.rb', line 72

def str
  @str
end

#sub_cmdObject (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 options_h_to_s(options)
  opts = []

  # multiple options can be passed by set
  options.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

.options_to_s(options) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/coaster/cmd_options.rb', line 6

def options_to_s(options)
  case options
  when Hash then options_h_to_s(options)
  when Array, Set then options.map{|o| options_to_s(o)}.join(' ')
  else options
  end
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, **options)
  if args.first.is_a?(CmdOptions)
    other = args.shift
  else
    other = self.class.new([@cmd, @sub_cmd], *args, **options)
  end
  self.class.new(to_h.merge(other.to_h))
end

#parserObject



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_hObject



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_sObject



128
129
130
131
# File 'lib/coaster/cmd_options.rb', line 128

def to_s
  return self.class.options_h_to_s(@hash) if defined?(@hash)
  @str
end