Class: Twat::ArgParse

Inherits:
Object
  • Object
show all
Defined in:
lib/twat/argparse.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



75
76
77
# File 'lib/twat/argparse.rb', line 75

def [](key)
  options[key]
end

#getoptsObject



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/twat/argparse.rb', line 15

def getopts
  options = Hash.new
  options[:action] = :tweet
  options[:count] = 1
  @optparser = OptionParser.new do |opts|
    opts.banner = "Usage: twat <tweet>"

    opts.on('-n', '--account ACCOUNT', 'Use ACCOUNT (or default)') do |acct| #{{{ --account ACCOUNT
      options[:account] = acct.to_sym
    end # }}}
    opts.on('-a', '--add ACCOUNT', 'Configure and authorise ACCOUNT') do |acct| #{{{ --add ACCOUNT
      options[:account] = acct.to_sym
      options[:action] = :add
    end #}}}
    opts.on('-d', '--delete ACCOUNT', 'Delete ACCOUNT') do |acct| #{{{ --delete ACCOUNT
      options[:account] = acct.to_sym
      options[:action] = :delete
    end #}}}
    opts.on('-h', '--help', 'Display this screen') do #{{{ --help
      puts opts
      exit
    end #}}}
    opts.on('-l', '--list [COUNT]', 'Display [count] tweets from your newsfeed') do |count| #{{{ --list ACCOUNT
      options[:count] = count || 10
      options[:action] ||= :show
    end #}}}
    opts.on('-f', '--follow', 'Display tweets from your newsfeed indefinitely') do #{{{ --follow
      options[:action] = :follow
    end #}}}
    opts.on('-v', '--version', 'Display version info') do #{{{ --version
      options[:action] = :version
    end #}}}
    opts.on('-u', '--user [USER]', 'Display current status for USER (Defaults to your default account)') do |user| #{{{ --user USER
      options[:user] = (user || :default)
      options[:action] = :user_feed
    end #}}}
    opts.on("--set OPTION=VALUE", 'Set OPTION to VALUE') do |optval| #{{{ --set OPTION=VALUE
      options[:action] = :setoption
      options[:optval] = optval
    end #}}}
    opts.on("--update-config", "Update config to latest version") do #{{{ --update-config
      options[:action] = :updateconfig
    end #}}}
  end

  @optparser.parse!
  REQUIRED.each do |req|
    usage unless options[req]
  end
  if MSG_REQUIRED.include?(options[:action])
    options[:msg] = msg
  end
  options
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/twat/argparse.rb', line 79

def include?(key)
  options.include?(key)
end

#msgObject



70
71
72
73
# File 'lib/twat/argparse.rb', line 70

def msg
  usage unless ARGV.length > 0
  ARGV.join(" ")
end

#optionsObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/twat/argparse.rb', line 83

def options
  begin
    @configthingfucken ||= getopts
  # FIXME, actually do something smart, not yell and barf
  rescue OptionParser::InvalidOption
    usage "Unknown option"
  rescue OptionParser::MissingArgument
    usage "Missing argument"
  end
end

#usage(additional = nil) ⇒ Object

TODO delegate specifically instead of shimming everything?



9
10
11
12
13
# File 'lib/twat/argparse.rb', line 9

def usage(additional=nil)
  puts additional if additional
  puts @optparser
  exit
end