arbi 1.0.6 → 1.0.7a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/arbi.rb +68 -58
  2. metadata +10 -21
@@ -10,10 +10,9 @@ end
10
10
 
11
11
  module Arbi
12
12
 
13
- require 'getopt/long'
14
- include Getopt
13
+ require 'optparse'
15
14
 
16
- VERSION = "1.0.5"
15
+ VERSION = "1.0.7a"
17
16
 
18
17
  @cmd = []
19
18
 
@@ -62,16 +61,6 @@ module Arbi
62
61
  @server = TCPServer.new(@address, @port)
63
62
  end
64
63
 
65
- def show_help
66
- puts "Arbi Server, USAGE:"
67
- puts "\t#{$0} [switches]"
68
- puts "\t\t--bind-address|-a\tAddress to bind, default to \"127.0.0.1\""
69
- puts "\t\t --port|-p\tPort to use for server, default to 40"
70
- puts "\t\t --version|-v\tPrint version and exit"
71
- puts "\t\t --help|-h\tPrint this help and exit"
72
- exit 0
73
- end
74
-
75
64
  def start
76
65
  @servert = Thread.new do
77
66
  while(session = @server.accept)
@@ -100,22 +89,35 @@ module Arbi
100
89
  private
101
90
 
102
91
  def parse_args
103
- begin
104
- opts = Arbi::Long.getopts(
105
- ["--bind-address", "-a", Arbi::REQUIRED],
106
- ["--port", "-p", Arbi::REQUIRED],
107
- ["--help"],
108
- ["--version"]
109
- )
110
- rescue Getopt::Long::Error => e
111
- puts "Arguments error: #{e}"
112
- exit 0
113
- end
92
+ OptionParser.new do |o|
93
+
94
+ o.program_name = 'arbid'
95
+ o.banner = 'Arbi server, USAGE:'
96
+
97
+ o.on('-a', '--bind-address ADDR', 'Address to bind, default to "127.0.0.1"') do |addr|
98
+ @address = addr
99
+ end
114
100
 
115
- @address = opts['a'] if opts['a']
116
- @port = opts['p'] if opts['p']
117
- show_help if opts['h']
118
- Arbi::show_version if opts['v']
101
+ o.on('-p', '--port PORT', 'Port to use for server, default to 40') do |port|
102
+ @port = port
103
+ end
104
+
105
+ o.on('-V', '--version', 'Print version and exit') do
106
+ puts Arbi::show_version
107
+ exit 0
108
+ end
109
+
110
+ o.on_tail('-h', '--help', 'Print this help and exit') do
111
+ puts o.to_s
112
+ exit 0
113
+ end
114
+
115
+ end.parse!
116
+
117
+ rescue OptionParser::MissingArgument
118
+ puts "At least one argument is required for this option."
119
+ puts "See help for details."
120
+ exit -1
119
121
  end
120
122
 
121
123
  def new_client session
@@ -174,6 +176,10 @@ module Arbi
174
176
  @command = "help\r\nquit\r\n"
175
177
  parse_args
176
178
  @sock = TCPSocket.new(@address, @port)
179
+
180
+ rescue Errno::ECONNREFUSED
181
+ puts "You have to start arbid demon first, or specify a correct port."
182
+ exit -2
177
183
  end
178
184
 
179
185
  def start
@@ -210,40 +216,44 @@ module Arbi
210
216
  close
211
217
  end
212
218
 
213
- def show_help
214
- puts "Arbi client:"
215
- puts "\tUSAGE: #{$0} [switches]"
216
- puts "\t\t --help|-h\tshow this helps"
217
- puts "\t\t--version|-v\tshow version of arbi"
218
- puts "\t\t--address|-a\tset the address to connect, default to 'localhost'"
219
- puts "\t\t --port|-p\tset the port to connect, default to 40"
220
- puts "\t\t--command|-c\tset commands to execute, defaults is 'help'"
221
- exit 0
222
- end
223
-
224
219
  private
225
220
 
226
221
  def parse_args
227
- begin
228
- opts = Arbi::Long.getopts(
229
- ["--help"],
230
- ["--version"],
231
- ["--address", "-a", Arbi::REQUIRED],
232
- ["--port", "-p", Arbi::REQUIRED],
233
- ["--commands", "-c", Arbi::REQUIRED]
234
- )
235
- rescue Arbi::Long::Error => e
236
- p ARGV
237
- $stderr.puts "Arguments error: #{e}"
238
- exit 1
239
- end
222
+ OptionParser.new do |o|
223
+
224
+ o.program_name = 'arbi'
225
+ o.banner = 'Arbi client, USAGE:'
226
+
227
+ o.on('-c', '--command COMMANDS', Array, 'Set commands to execute, default is \'help\'') do |cmd|
228
+ cmd.delete('quit')
229
+ @command = (cmd&cmd).push('quit').inject(''){|str, cmd| str<<"#{cmd}\r\n"}
230
+ end
231
+
232
+ o.on('-a', '--address ADDR', 'Set address to connect, default to "127.0.0.1"') do |addr|
233
+ @address = addr
234
+ end
235
+
236
+ o.on('-p', '--port PORT', 'Set port to connect, default to 40') do |port|
237
+ @port = port
238
+ end
239
+
240
+ o.on('-V', '--version', 'Print version and exit') do
241
+ puts Arbi::show_version
242
+ exit 0
243
+ end
244
+
245
+ o.on_tail('-h', '--help', 'Print this help and exit') do
246
+ @help = o.to_s
247
+ puts @help
248
+ exit 0
249
+ end
250
+
251
+ end.parse!
240
252
 
241
- Arbi::show_version if opts["v"]
242
- show_help if opts["h"]
243
- @address = opts["a"] if opts["a"]
244
- @port = opts["p"] if opts["p"]
245
- @command = "#{opts["c"]}".gsub(/,+/, ',').gsub(/\s+/, '').split(/,/).
246
- uniq.delete_if{|x|x=~/^quit$/i}.push("quit\r\n").join("\r\n") if opts["c"]
253
+ rescue OptionParser::MissingArgument
254
+ puts "At least one argument is required for this option."
255
+ puts "See help for details."
256
+ exit -1
247
257
  end
248
258
  end
249
259
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arbi
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ prerelease: true
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 6
9
- version: 1.0.6
8
+ - 7a
9
+ version: 1.0.7a
10
10
  platform: ruby
11
11
  authors:
12
12
  - shura
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-08 00:00:00 +02:00
17
+ date: 2010-09-09 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: getopt
21
+ name: sys-filesystem
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -30,19 +30,6 @@ dependencies:
30
30
  version: "0"
31
31
  type: :runtime
32
32
  version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: sys-filesystem
35
- prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
44
- type: :runtime
45
- version_requirements: *id002
46
33
  description: ""
47
34
  email: shura1991@gmail.com
48
35
  executables:
@@ -83,11 +70,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
70
  required_rubygems_version: !ruby/object:Gem::Requirement
84
71
  none: false
85
72
  requirements:
86
- - - ">="
73
+ - - ">"
87
74
  - !ruby/object:Gem::Version
88
75
  segments:
89
- - 0
90
- version: "0"
76
+ - 1
77
+ - 3
78
+ - 1
79
+ version: 1.3.1
91
80
  requirements: []
92
81
 
93
82
  rubyforge_project: