Class: Bitsa::ArgsProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/bitsa/args_processor.rb

Overview

Arguments passed on the command line. Trollop trollop.rubyforge.org is used to handle the parsing.

Constant Summary collapse

SUB_COMMANDS =

Valid commands.

%w(update reload search)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cmdObject (readonly)

The command to execute



36
37
38
# File 'lib/bitsa/args_processor.rb', line 36

def cmd
  @cmd
end

#global_optsObject (readonly)

Global options passed on the command line.



33
34
35
# File 'lib/bitsa/args_processor.rb', line 33

def global_opts
  @global_opts
end

#search_dataObject (readonly)

Data to search cached contacts for.



39
40
41
# File 'lib/bitsa/args_processor.rb', line 39

def search_data
  @search_data
end

Instance Method Details

#parse(args) ⇒ Object

Parse arguments and setup attributes. If invalid data is passed a Trollop exception is thrown and the program terminated.

It also handles showing the Help and Version information.



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
# File 'lib/bitsa/args_processor.rb', line 45

def parse(args)
  @global_opts = Trollop::options(args) do
    version "bitsa v#{Bitsa::VERSION}"
    banner <<EOS
Usage: bitsa [global-options] [subcommand] [command-opts]

Global options are:
EOS
    opt :config_file, "Configuration file", :default => "~/.bitsa_config.yml"
    opt :login, "Login", :type => String
    opt :password, "Password", :type => String

    stop_on SUB_COMMANDS

    banner <<EOS

bitsa subcommands
   update: get the latest changes from Gmail
   reload: Clear all cached addresses and reload from Gmail
   search: Search for the passed string

Information about this program
EOS
  end

  @cmd = args.shift || ''
  @search_data = ''

  if cmd == "search"
    @search_data << args.shift unless args.empty?
  elsif !["search", "update", "reload"].include?(cmd)
    Trollop::die "unknown subcommand '#{cmd}'"
  end
end