This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.rb
94 lines (77 loc) · 1.75 KB
/
options.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
88
89
90
91
92
93
94
module GitSwitch
class Options
attr_reader :args
def initialize(args)
@args = args
end
def flags
@flags ||= args.select do |arg|
arg.match(/\A\-[cglv]{1}\z/) ||
arg.match(/\A\-\-(config|global|list|verbose|version){1}\z/)
end
end
def valid_args?
if config?
return true
elsif list?
return true
elsif version?
return true
elsif verbose?
return true
elsif no_flags?
return true
elsif one_flag? && !flag_only?
return true
elsif usage?
return true
else
puts "Invalid args"
return false
end
end
def usage?
args.count == 0
end
def config?
config_flag? && args.count == 1
end
def list?
list_flag? && args.count == 1
end
def version?
version_flag? && args.count == 1
end
def global?
(flags.include? '-g') || (flags.include? '--global')
end
def verbose?
verbose_flag? && args.count > 1
end
private
def config_flag?
(flags.include? '-c') || (flags.include? '--config')
end
def list_flag?
(flags.include? '-l') || (flags.include? '--list')
end
def version_flag?
((flags.include? '-v') && args.count == 1) || (flags.include? '--version')
end
def verbose_flag?
((flags.include? '-v') && args.count > 1) || (flags.include? '--verbose')
end
def no_flags?
args.length == 1 && flag_count(args) == 0
end
def one_flag?
args.length == 2 && flag_count(args) == 1
end
def flag_only?
list_flag? || config_flag? || version_flag?
end
def flag_count(args)
args.count {|a| a.start_with? '-'}
end
end
end