-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.v
73 lines (63 loc) · 1.51 KB
/
help.v
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
module vargus
// HelpSubcommands is the parsed sub_commands
pub struct HelpSubcommands {
command string
long_desc string
short_desc string
}
const (
help = ['-h', '--help', 'help'] // help commands
)
// get_space calculates space for better spacing in print outputs
pub fn get_space(cmdlen int) string {
return ' '.repeat(40 - cmdlen)
}
// help prints the help info once triggered
fn (c &Commander) help(cmd_str string, desc string, sub_commands []HelpSubcommands, local_flags []FlagArgs, global_flags []FlagArgs) {
// parse usage
mut usage := cmd_str
if sub_commands.len > 0 {
usage += ' [command]'
}
if local_flags.len + global_flags.len > 0 {
usage += ' [-flags]'
}
// show help message
println('\n$desc')
println('\nUsage:\n $usage')
// sub commands
if sub_commands.len > 0 {
println('\nCommands:')
for i in sub_commands {
// calculate spacing
println(' $i.command ${get_space(i.command.len)} $i.short_desc')
}
}
// local flags
if local_flags.len > 0 {
println('\nFlags:')
help_print_flag(local_flags)
}
// global flags
if global_flags.len > 0 {
println('\nGlobal Flags:')
help_print_flag(global_flags)
}
}
// help_print_flag is a helper for printing flags in a cool style
pub fn help_print_flag(flags []FlagArgs) {
for i in flags {
// parse flag argument
mut arg := ''
if i.name != '' {
arg += '--$i.name'
}
if i.short_arg != '' {
if i.name != '' {
arg += ', '
}
arg += '-$i.short_arg'
}
println(' $arg ${get_space(arg.len)} $i.help')
}
}