2019-11-21 13:03:12 +01:00
|
|
|
module cli
|
|
|
|
|
|
|
|
const (
|
|
|
|
BASE_INDENT = 2
|
|
|
|
ABBREV_INDENT = 5
|
|
|
|
DESCRIPTION_INDENT = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
fn help_flag() Flag {
|
|
|
|
return Flag{
|
|
|
|
flag: .bool,
|
|
|
|
name: 'help',
|
|
|
|
abbrev: 'h',
|
|
|
|
description: 'Prints help information',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn help_cmd() Command {
|
|
|
|
return Command{
|
|
|
|
name: 'help',
|
|
|
|
description: 'Prints help information',
|
|
|
|
execute: help_func,
|
2020-01-02 18:09:24 +01:00
|
|
|
parent: 0
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:04:53 +02:00
|
|
|
fn help_func(help_cmd Command) {
|
|
|
|
cmd := help_cmd.parent
|
2019-11-21 13:03:12 +01:00
|
|
|
full_name := cmd.full_name()
|
|
|
|
|
|
|
|
mut help := ''
|
|
|
|
help += 'Usage: ${full_name}'
|
|
|
|
if cmd.flags.len > 0 { help += ' [FLAGS]'}
|
|
|
|
if cmd.commands.len > 0 { help += ' [COMMANDS]'}
|
|
|
|
help += '\n\n'
|
|
|
|
|
|
|
|
if cmd.description != '' {
|
|
|
|
help += '${cmd.description}\n\n'
|
|
|
|
}
|
|
|
|
if cmd.flags.len > 0 {
|
|
|
|
help += 'Flags:\n'
|
|
|
|
for flag in cmd.flags {
|
|
|
|
mut flag_name := ''
|
|
|
|
if flag.abbrev != '' {
|
2020-04-16 23:01:04 +02:00
|
|
|
abbrev_indent := ' '.repeat(max(ABBREV_INDENT-(flag.abbrev.len+1), 1))
|
2019-11-21 13:03:12 +01:00
|
|
|
flag_name = '-${flag.abbrev}${abbrev_indent}--${flag.name}'
|
|
|
|
} else {
|
2020-04-16 23:01:04 +02:00
|
|
|
abbrev_indent := ' '.repeat(max(ABBREV_INDENT-(flag.abbrev.len), 1))
|
2019-11-21 13:03:12 +01:00
|
|
|
flag_name = '${abbrev_indent}--${flag.name}'
|
|
|
|
}
|
|
|
|
mut required := ''
|
|
|
|
if flag.required {
|
|
|
|
required = ' (required)'
|
|
|
|
}
|
|
|
|
|
|
|
|
base_indent := ' '.repeat(BASE_INDENT)
|
2020-04-16 23:01:04 +02:00
|
|
|
description_indent := ' '.repeat(max(DESCRIPTION_INDENT-flag_name.len, 1))
|
2019-11-21 13:03:12 +01:00
|
|
|
help += '${base_indent}${flag_name}${description_indent}${flag.description}${required}\n'
|
|
|
|
}
|
|
|
|
help += '\n'
|
|
|
|
}
|
|
|
|
if cmd.commands.len > 0 {
|
|
|
|
help += 'Commands:\n'
|
|
|
|
for command in cmd.commands {
|
|
|
|
base_indent := ' '.repeat(BASE_INDENT)
|
2020-04-16 23:01:04 +02:00
|
|
|
description_indent := ' '.repeat(max(DESCRIPTION_INDENT-command.name.len, 1))
|
2019-11-21 13:03:12 +01:00
|
|
|
|
|
|
|
help += '${base_indent}${command.name}${description_indent}${command.description}\n'
|
|
|
|
}
|
|
|
|
help += '\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
print(help)
|
|
|
|
}
|
2020-04-16 23:01:04 +02:00
|
|
|
|
|
|
|
fn max(a, b int) int {
|
|
|
|
return if a > b {a} else {b}
|
|
|
|
}
|