Module: Thor::Base

Defined in:
lib/cnvrg/cli.rb

Instance Method Summary collapse

Instance Method Details

#initialize(args = [], local_options = {}, config = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cnvrg/cli.rb', line 53

def initialize(args = [], local_options = {}, config = {})
  parse_options = Thor.class_options

  # The start method splits inbound arguments at the first argument
  # that looks like an option (starts with - or --). It then calls
  # new, passing in the two halves of the arguments Array as the
  # first two parameters.

  command_options = config.delete(:command_options) # hook for start
  parse_options = parse_options.merge(command_options) if command_options
  if local_options.is_a?(Array)
    array_options = local_options
    hash_options = {}
  else
    # Handle the case where the class was explicitly instantiated
    # with pre-parsed options.
    array_options = []
    hash_options = local_options

  end


  # Let Thor::Options parse the options first, so it can remove
  # declared options from the array. This will leave us with
  # a list of arguments that weren't declared.
  stop_on_unknown = Thor.stop_on_unknown_option? config[:current_command]
  opts = Thor::Options.new(parse_options, hash_options, stop_on_unknown)
  real_options = []
  real_args = [].replace(array_options)
  if local_options.is_a? (Array) and !local_options.empty? and args.empty?
    array_options.each_with_index do |p, i|
      opt = p
      if p.include? "="
        opt = p.split("=")[0]
      end
      option = is_option(parse_options.values, opt)
      if !option
        break
      else
        real_options << p
        real_args.delete(p)
        if !p.include? "=" and option.type != :boolean
          if i + 1 < array_options.size
            real_options << array_options[i + 1]
            real_args.delete(array_options[i + 1])
          end
        end

      end

    end

    args = real_args
  else
    if !args.empty? and local_options.is_a? Array and !local_options.empty?
      args = args + local_options
    else
      args = args.flatten()
    end

  end


  self.options = opts.parse(real_options)
  self.options = config[:class_options].merge(options) if config[:class_options]

  # If unknown options are disallowed, make sure that none of the
  # remaining arguments looks like an option.
  opts.check_unknown! if Thor.check_unknown_options?(config)

  # Add the remaining arguments from the options parser to the
  # arguments passed in to initialize. Then remove any positional
  # arguments declared using #argument (this is primarily used
  # by Thor::Group). Tis will leave us with the remaining
  # positional arguments.
  to_parse = args
  to_parse += opts.remaining unless self.class.strict_args_position?(config)
  thor_args = Thor::Arguments.new(self.class.arguments)
  thor_args.parse(to_parse).each {|k, v| __send__("#{k}=", v)}
  @args = thor_args.remaining
end

#is_option(options, p) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cnvrg/cli.rb', line 135

def is_option (options, p)
  options.each do |o|
    if !o.aliases.nil?
      if (o.aliases.is_a? Array and o.aliases.include? p) or (o.aliases.is_a? Array and o.aliases.size == 1 and o.aliases[0].split(",").include? p) or o.switch_name.eql? p
        return o
      end
    end

  end
  return false
end