2019-11-21 15:03:12 +03: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 20:09:24 +03:00
|
|
|
parent: 0
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:04:53 +03:00
|
|
|
fn help_func(help_cmd Command) {
|
|
|
|
cmd := help_cmd.parent
|
2019-11-21 15:03:12 +03: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-17 00:01:04 +03:00
|
|
|
abbrev_indent := ' '.repeat(max(ABBREV_INDENT-(flag.abbrev.len+1), 1))
|
2019-11-21 15:03:12 +03:00
|
|
|
flag_name = '-${flag.abbrev}${abbrev_indent}--${flag.name}'
|
|
|
|
} else {
|
2020-04-17 00:01:04 +03:00
|
|
|
abbrev_indent := ' '.repeat(max(ABBREV_INDENT-(flag.abbrev.len), 1))
|
2019-11-21 15:03:12 +03:00
|
|
|
flag_name = '${abbrev_indent}--${flag.name}'
|
|
|
|
}
|
|
|
|
mut required := ''
|
|
|
|
if flag.required {
|
|
|
|
required = ' (required)'
|
|
|
|
}
|
|
|
|
|
|
|
|
base_indent := ' '.repeat(BASE_INDENT)
|
2020-04-17 00:01:04 +03:00
|
|
|
description_indent := ' '.repeat(max(DESCRIPTION_INDENT-flag_name.len, 1))
|
2019-11-21 15:03:12 +03: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-17 00:01:04 +03:00
|
|
|
description_indent := ' '.repeat(max(DESCRIPTION_INDENT-command.name.len, 1))
|
2019-11-21 15:03:12 +03:00
|
|
|
|
|
|
|
help += '${base_indent}${command.name}${description_indent}${command.description}\n'
|
|
|
|
}
|
|
|
|
help += '\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
print(help)
|
|
|
|
}
|
2020-04-17 00:01:04 +03:00
|
|
|
|
|
|
|
fn max(a, b int) int {
|
|
|
|
return if a > b {a} else {b}
|
|
|
|
}
|