-
Notifications
You must be signed in to change notification settings - Fork 2
/
clicr.cr
195 lines (174 loc) · 5.53 KB
/
clicr.cr
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require "./command"
class Clicr
@sub : Subcommand
property help_callback : Proc(String, Nil) = ->(msg : String) do
STDOUT.puts msg
exit 0
end
property error_callback : Proc(String, Nil) = ->(msg : String) do
STDERR.puts msg
exit 1
end
property help_footer : Proc(String, String) = ->(command : String) do
"\n'#{command} --help' to show the help."
end
property argument_required : Proc(String, String, String) = ->(command : String, argument : String) do
"argument required for '#{command}': #{argument}"
end
property unknown_argument : Proc(String, String, String) = ->(command : String, argument : String) do
"unknown argument for '#{command}': #{argument}"
end
property unknown_command : Proc(String, String, String) = ->(command : String, sub_command : String) do
"unknown command for '#{command}': '#{sub_command}'"
end
property unknown_option : Proc(String, String, String) = ->(command : String, option : String) do
"unknown option for '#{command}': #{option}"
end
property invalid_option_value : Proc(String, String, Exception, String) = ->(command : String, option : String, ex : Exception) do
"invalid option value for '#{command}': #{option} (#{ex})"
end
getter help_option : String
protected getter args : Array(String)
# CLI arguments
protected getter arguments = Array(String).new
def initialize(
*,
@name : String = Path[PROGRAM_NAME].basename,
label : String? = nil,
description : String? = nil,
@usage_name : String = "Usage: ",
@commands_name : String = "COMMANDS",
@options_name : String = "OPTIONS",
@help_option : String = "help",
args : Array(String) = ARGV,
action = nil,
arguments = nil,
commands = nil,
options = nil
)
@sub = Command.create(
name: @name,
short: "",
label: label,
description: description,
action: action,
arguments: arguments,
commands: commands,
options: options,
)
@args = args.dup
end
def run(@args : Array(String) = @args)
@sub.exec @name, self
end
protected def parse_options(command_name : String, command : Command, & : String | Char, String? ->) : Subcommand | Clicr | Nil
while arg = @args.shift?
if string_option = arg.lchop? "--"
if @help_option == string_option
return help command_name, command
else
var, equal_sign, val = string_option.partition '='
if equal_sign.empty?
yield var, nil
else
yield var, val
end
end
elsif option = arg.lchop? '-'
if @help_option[0] === option[0]
return help command_name, command
else
option.each_char do |char_opt|
yield char_opt, nil
end
end
elsif cmd_arguments = command.arguments
if !cmd_arguments.is_a?(Array) && @arguments.size + 1 > cmd_arguments.size
return @error_callback.call(
@unknown_argument.call(command_name, arg) + @help_footer.call(command_name)
)
end
@arguments << arg
else
next if arg.empty?
command.sub_commands.try &.each do |sub_command|
if sub_command.name == arg || sub_command.short == arg
return sub_command
end
end
return @error_callback.call(
@unknown_command.call(command_name, arg) + @help_footer.call(command_name)
)
end
end
if (cmd_arguments = command.arguments) && cmd_arguments.is_a?(Tuple) && cmd_arguments.size > @arguments.size
@error_callback.call(
@argument_required.call(command_name, cmd_arguments[-1]) + @help_footer.call(command_name)
)
else
self
end
end
protected def help(command_name : String, command : Command, reason : String? = nil) : Nil
@help_callback.call(String.build do |io|
io << @usage_name << command_name << ' '
command.arguments.try &.each do |arg|
io << arg << ' '
end
if command.sub_commands
io << @commands_name << ' '
end
io << '[' << @options_name << "]\n"
if description = command.description || command.label
io << '\n' << description << '\n'
end
if sub_commands = command.sub_commands
io << '\n' << @commands_name
array = Array({String, String?}).new
sub_commands.each do |sub_command|
name = sub_command.name
if short = sub_command.short
name += ", " + short
end
array << {name, (sub_command.label || sub_command.description)}
end
align io, array
io.puts
end
if options = command.options
io << '\n' << @options_name
array = Array({String, String?}).new
options.each do |name, option|
key = "--" + name.to_s
if short = option.short
key += ", -" + short
end
if option.string_option?
if default = option.default
key += ' ' + default
else
key += " #{option.type}"
end
end
array << {key, option.label}
end
align io, array
io.puts
end
io << @help_footer.call command_name
end
)
end
private def align(io, array : Array({String, String?})) : Nil
max_size = array.max_of { |arg, _| arg.size }
array.each do |name, help|
io << "\n " << name
if help
(max_size - name.size).times do
io << ' '
end
io << " " << help
end
end
end
end