Class: ActiveMailbox::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/active_mailbox/cli.rb

Overview

This class facilites ActiveMailbox’s command line functions

Class Method Summary collapse

Class Method Details

.parse(argv) ⇒ Object

Parse CLI arguments (ARGV)



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
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
# File 'lib/active_mailbox/cli.rb', line 30

def self.parse(argv)
  options = {}

  ::OptionParser.new do |opts|
    opts.banner = "Usage: active_mailbox [OPTION] MAILBOX\n"

    opts.separator ""
    opts.separator "Mailbox Options:"

    opts.on('--context', 'Look for [MAILBOX] in [CONTEXT]') do |context|
      options[:context] = conext
    end

    opts.on('--delete', 'Delete [MAILBOX] and all messages') do
      options[:command] = :delete!
    end

    opts.on('--sort', 'Sort messages in [MAILBOX] (recursive)') do
      options[:command] = :sort!
    end

    opts.separator ""
    opts.separator "Cleanup Options:"

    opts.on('--clean-ghosts', "Cleanup 'ghost' messages") do
      options[:command] = :clean_ghosts!
    end

    opts.on('--clean-stale', 'Cleanup messages older than 30 days') do
      options[:command] = :clean_stale!
    end

    opts.on('--clean-mp3s', 'Cleanup [MAILBOX]/.mp3 directory') do
      options[:command] = :clean_mp3s!
    end

    opts.on('--purge', 'Remove all messages, but leave [MAILBOX] folders intact') do
      options[:command] = :purge!
    end

    opts.separator ""
    opts.separator "Greeting Options:"

    opts.on('--delete-temp', 'Delete [MAILBOX]/temp.wav') do
      options[:command] = :delete_temp_greeting!
    end

    opts.on('--delete-unavail', 'Delete [MAILBOX]/unavail.wav') do
      options[:command] = :delete_unavail_greeting!
    end

    opts.on('--delete-busy', 'Delete [MAILBOX]/busy.wav') do
      options[:command] = :delete_busy_greeting!
    end

    opts.separator ""
    opts.separator "General Options:"

    opts.on('-h', '--help', 'Show this message') do
      options[:command] = :help
      puts opts
      exit
    end

    opts.on('-v', '--version', 'Show version') do
      options[:command] = :version
      puts ActiveMailbox::Version
      exit
    end

    begin
      argv = ['-h'] if argv.empty?
      opts.parse!(argv)
      options[:mailbox] = argv.shift
      options
    rescue ::OptionParser::ParseError => err
      STDERR.puts err.message, "\n", opts
      exit(-1)
    end
  end
end

.run!(argv) ⇒ Object

Invoke the CLI



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_mailbox/cli.rb', line 12

def self.run!(argv)
  options = ActiveMailbox::CLI.parse(argv)

  args = [options[:mailbox]]
  args.unshift(options[:context]) if options[:context]

  mailbox = ActiveMailbox::Mailbox.find(*args)

  if options[:arg]
    mailbox.send(options[:command], options[:arg])
  else
    mailbox.send(options[:command])
  end
end