Class: Twat::ArgParse

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

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



94
95
96
# File 'lib/twat/argparse.rb', line 94

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/twat/argparse.rb', line 15

def getopts
  options = Hash.new
  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('--follow-user USER', 'Follow USER on twitter') do |user| #{{{ --follow-user USER
      options[:action] = :follow_user
      options[:user] = user
    end #}}}
    opts.on('--endpoint ENDPOINT', 'Specify a different endpoint for use with --add') do |endpoint| #{{{ --add ACCOUNT
      options[:endpoint] = endpoint.to_sym
    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!
  # {{{ Validation ---
  # Default action is to send a tweet
  options[:action] ||= :tweet

  # Only specify an endpoint when adding accounts
  if options[:endpoint]
    usage unless options[:action] == :add
  end
  # Similarly, default endpoint to twitter when we do add
  if options[:action] == :add
    options[:endpoint] ||= :twitter
  end

  REQUIRED.each do |req|
    usage unless options[req]
  end
  if MSG_REQUIRED.include?(options[:action])
    options[:msg] = msg
  end
  # }}} Validation End ---
  options
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/twat/argparse.rb', line 98

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

#msgObject



89
90
91
92
# File 'lib/twat/argparse.rb', line 89

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

#optionsObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/twat/argparse.rb', line 102

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