Class: Twurl::CLI
- Inherits:
-
Object
show all
- Defined in:
- lib/twurl/cli.rb
Defined Under Namespace
Modules: AvailableOptions
Constant Summary
collapse
- SUPPORTED_COMMANDS =
%w(authorize accounts bearer_tokens alias set)
- DEFAULT_COMMAND =
'request'
- PATH_PATTERN =
/^\/\w+/
- PROTOCOL_PATTERN =
/^\w+:\/\//
- README =
File.dirname(__FILE__) + '/../../README.md'
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.output ⇒ Object
Returns the value of attribute output.
11
12
13
|
# File 'lib/twurl/cli.rb', line 11
def output
@output
end
|
Class Method Details
.dispatch(options) ⇒ Object
.parse_options(args) ⇒ Object
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
111
112
113
114
|
# File 'lib/twurl/cli.rb', line 43
def parse_options(args)
Twurl.options = Options.new
Twurl.options.args = args.dup
Twurl.options.trace = false
Twurl.options.data = {}
Twurl.options. = {}
Twurl.options.upload = {}
Twurl.options.upload['file'] = []
option_parser = OptionParser.new do |o|
o.extend AvailableOptions
o.banner = <<-BANNER
Usage: twurl authorize --consumer-key key --consumer-secret secret
twurl [options] /1.1/statuses/home_timeline.json
Supported Commands: #{SUPPORTED_COMMANDS.sort.join(', ')}
BANNER
o.section "Getting started:" do
tutorial
end
o.section "Authorization options:" do
username
consumer_key
consumer_secret
access_token
token_secret
end
o.section "Common options:" do
trace
data
raw_data
host
quiet
request_method
help
version
proxy
file
filefield
base64
json_format
timeout
connection_timeout
app_only
end
end
begin
arguments = option_parser.parse!(args)
rescue OptionParser::InvalidOption
raise Exception "ERROR: undefined option"
rescue Twurl::Exception
raise
rescue
raise Exception "ERROR: invalid argument"
end
Twurl.options.command = extract_command!(arguments)
Twurl.options.path = (arguments)
Twurl.options.subcommands = arguments
if Twurl.options.command == DEFAULT_COMMAND and Twurl.options.path.nil? and Twurl.options.args.empty?
CLI.puts option_parser
raise Exception, "No path found"
end
Twurl.options
end
|
.print(*args, &block) ⇒ Object
124
125
126
127
|
# File 'lib/twurl/cli.rb', line 124
def print(*args, &block)
output.print(*args, &block)
output.flush if output.respond_to?(:flush)
end
|
.prompt_for(label) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/twurl/cli.rb', line 134
def prompt_for(label)
system "stty -echo"
CLI.print "#{label}: "
result = STDIN.gets.chomp
CLI.puts
result
rescue Interrupt
exit
ensure
system "stty echo"
end
|
.puts(*args, &block) ⇒ Object
129
130
131
132
|
# File 'lib/twurl/cli.rb', line 129
def puts(*args, &block)
output.puts(*args, &block)
output.flush if output.respond_to?(:flush)
end
|
.run(args) ⇒ Object
13
14
15
16
17
18
19
20
|
# File 'lib/twurl/cli.rb', line 13
def run(args)
begin
options = parse_options(args)
rescue Twurl::Exception => exception
abort(exception.message)
end
dispatch(options)
end
|