Class: GmailCli::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/gmail_cli/shell.rb

Overview

class that groks the command line options and invokes the required task

Constant Summary collapse

OPTIONS =

defines the valid command line options

%w(help verbose client_id:s client_secret:s)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, args) ⇒ Shell

initializes the shell with command line argments:

options is expected to be the hash structure as provided by GetOptions.new(..)

args is the remaining command line arguments



16
17
18
19
20
# File 'lib/gmail_cli/shell.rb', line 16

def initialize(options,args)
  @options = (options||{}).each{|k,v| {k => v} }
  @args = args
  GmailCli::Logger.set_log_mode(options[:verbose])
end

Instance Attribute Details

#argsObject (readonly)

holds the remaining command line arguments



8
9
10
# File 'lib/gmail_cli/shell.rb', line 8

def args
  @args
end

#optionsObject (readonly)

holds the parsed options



5
6
7
# File 'lib/gmail_cli/shell.rb', line 5

def options
  @options
end

Class Method Details

.usageObject

prints usage/help information



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gmail_cli/shell.rb', line 38

def usage
  $stderr.puts "\nGmailCli v\#{GmailCli::VERSION}\n===================================\n\nUsage:\n  gmail_cli [options] [commands]\n\nOptions:\n  -h  | --help           : shows command help\n  -v  | --verbose        : run with verbose\n\n  --client_id \"xxxx\"     : OAuth2 client_id\n  --client_secret \"yyy\"  : OAuth2 client_secret\n\nCommands:\n  authorize              : perform Google OAuth2 client authorization\n\n\n"
end

Instance Method Details

#authorizeObject



67
68
69
# File 'lib/gmail_cli/shell.rb', line 67

def authorize
  GmailCli::Oauth2Helper.authorize!(options)
end

#runObject

Command: execute the task according to the options provided on initialisation



23
24
25
26
27
28
29
30
# File 'lib/gmail_cli/shell.rb', line 23

def run
  case
  when args.first =~ /authorize/i
    authorize
  else
    usage
  end
end

#usageObject

prints usage/help information



63
64
65
# File 'lib/gmail_cli/shell.rb', line 63

def usage
  self.class.usage
end