Skip to content

Commit 04cb843

Browse files
committed
[#3] fail gracefully
1 parent a6082e6 commit 04cb843

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

bin/macchanger

+17-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
require 'optparse'
44
options = {}
5-
VERSION = 1.0
5+
VERSION = 1.1
66

7-
OptionParser.new do |opts|
7+
optparse = OptionParser.new do |opts|
88
opts.banner = 'Usage: macchanger [options] device'
99

1010
opts.on('-v', '--version', 'Displays MacChanger version') do
@@ -24,8 +24,7 @@ OptionParser.new do |opts|
2424
opts.on('-s', '--show', 'Show the MAC address, macchanger -s en0') do |s|
2525
options[:show] = s
2626
end
27-
28-
end.parse!
27+
end
2928

3029
class MacChanger
3130
def self.show(device)
@@ -39,7 +38,7 @@ class MacChanger
3938

4039
def self.valid?(mac)
4140
unless mac.match(/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/)
42-
fail ArgumentError, 'Mac address is not valid'
41+
fail OptionParser::InvalidArgument, 'Mac address is not valid'
4342
end
4443
end
4544

@@ -71,11 +70,18 @@ class MacChanger
7170
end
7271
end
7372

74-
options[:device] = ARGV[0] or fail ArgumentError, "Device can't be blank"
73+
begin
74+
optparse.parse!
75+
options[:device] = ARGV[0] or fail OptionParser::MissingArgument, 'device'
7576

76-
if options[:show]
77-
puts "Your mac address is: #{MacChanger.show(options[:device])}"
78-
else
79-
MacChanger.valid?(options[:mac]) unless options[:random]
80-
MacChanger.start(options)
77+
if options[:show]
78+
puts "Your mac address is: #{MacChanger.show(options[:device])}"
79+
else
80+
fail OptionParser::InvalidOption, 'MAC address or random option' if options[:mac].nil? && options[:random].nil?
81+
MacChanger.valid?(options[:mac]) unless options[:random]
82+
MacChanger.start(options)
83+
end
84+
rescue OptionParser::InvalidArgument, OptionParser::MissingArgument, OptionParser::InvalidOption => error
85+
puts error
86+
puts optparse
8187
end

0 commit comments

Comments
 (0)