2
2
3
3
require 'optparse'
4
4
options = { }
5
- VERSION = 1.0
5
+ VERSION = 1.1
6
6
7
- OptionParser . new do |opts |
7
+ optparse = OptionParser . new do |opts |
8
8
opts . banner = 'Usage: macchanger [options] device'
9
9
10
10
opts . on ( '-v' , '--version' , 'Displays MacChanger version' ) do
@@ -24,8 +24,7 @@ OptionParser.new do |opts|
24
24
opts . on ( '-s' , '--show' , 'Show the MAC address, macchanger -s en0' ) do |s |
25
25
options [ :show ] = s
26
26
end
27
-
28
- end . parse!
27
+ end
29
28
30
29
class MacChanger
31
30
def self . show ( device )
@@ -39,7 +38,7 @@ class MacChanger
39
38
40
39
def self . valid? ( mac )
41
40
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'
43
42
end
44
43
end
45
44
@@ -71,11 +70,18 @@ class MacChanger
71
70
end
72
71
end
73
72
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'
75
76
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
81
87
end
0 commit comments