Class: CommandLineEmail::CleOptparse

Inherits:
Object
  • Object
show all
Defined in:
lib/command_line_email/cle_optparse.rb

Class Method Summary collapse

Class Method Details

.parse(args, user_config) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
# File 'lib/command_line_email/cle_optparse.rb', line 5

def self.parse(args, user_config)

  mail_attrs = {:to => []}

  option_parser = OptionParser.new do |opts|

    opts.on("-t TO","--to TO") do |to|
      mail_attrs[:to] = to_string_to_mailing_list(user_config, to, mail_attrs[:to])
    end

    opts.on("-f FROM","--from FROM") do |from|
      mail_attrs[:from] = from
    end

    opts.on("-b BODY","--body BODY") do |body|
      mail_attrs[:body] ||= ""
      mail_attrs[:body] << body + "\n"
    end

    opts.on("-s SUBJECT","--subject SUBJECT") do |subject|
      mail_attrs[:subject] = subject
    end

    opts.on("-c CC","--cc CC") do |cc|
      if user_config.mailing_lists.has_key? cc.to_sym
        mail_attrs[:cc] = user_config.mailing_lists[cc.to_sym]
      else
        mail_attrs[:cc] = cc
      end
    end

    opts.on("-F FILE","--file FILE") do |file|
      mail_attrs[:files] ||= []
      mail_attrs[:files] << file
    end

    opts.on("-d DIRECTORY","--directory DIRECTORY") do |directory|
      mail_attrs[:directory] = directory
    end

  end

  option_parser.parse!(args)

  mail_attrs

end

.to_string_to_mailing_list(user_config, to, list = []) ⇒ Object

parse()



53
54
55
56
57
58
59
60
61
# File 'lib/command_line_email/cle_optparse.rb', line 53

def self.to_string_to_mailing_list(user_config, to, list=[])
  # takes optional list in case you want to add to an existing list
  if user_config.mailing_lists && user_config.mailing_lists.has_key?(to.to_sym)
    Array(user_config.mailing_lists[to.to_sym]).each { |address| list << address }
  else
    list << to
  end
  list
end