2019-11-21 15:03:12 +03:00
|
|
|
module cli
|
|
|
|
|
2020-04-26 14:49:31 +03:00
|
|
|
import term
|
|
|
|
import strings
|
2020-04-20 15:50:15 +03:00
|
|
|
|
2019-11-21 15:03:12 +03:00
|
|
|
const (
|
2020-07-18 14:24:10 +03:00
|
|
|
base_indent_len = 2
|
2020-06-29 18:59:55 +03:00
|
|
|
min_description_indent_len = 20
|
2020-07-18 14:24:10 +03:00
|
|
|
spacing = 2
|
2019-11-21 15:03:12 +03:00
|
|
|
)
|
|
|
|
|
2020-07-01 11:54:34 +03:00
|
|
|
fn help_flag(with_abbrev bool) Flag {
|
2020-07-18 14:24:10 +03:00
|
|
|
sabbrev := if with_abbrev { 'h' } else { '' }
|
2019-11-21 15:03:12 +03:00
|
|
|
return Flag{
|
2020-07-18 14:24:10 +03:00
|
|
|
flag: .bool
|
|
|
|
name: 'help'
|
|
|
|
abbrev: sabbrev
|
2020-08-21 00:14:53 +03:00
|
|
|
description: 'Prints help information.'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn help_cmd() Command {
|
|
|
|
return Command{
|
2020-07-18 14:24:10 +03:00
|
|
|
name: 'help'
|
|
|
|
usage: '<command>'
|
2020-08-21 00:14:53 +03:00
|
|
|
description: 'Prints help information.'
|
2020-07-18 14:24:10 +03:00
|
|
|
execute: print_help_for_command
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:24:10 +03:00
|
|
|
fn print_help_for_command(help_cmd Command) ? {
|
|
|
|
if help_cmd.args.len > 0 {
|
|
|
|
mut cmd := help_cmd.parent
|
|
|
|
for arg in help_cmd.args {
|
|
|
|
mut found := false
|
|
|
|
for sub_cmd in cmd.commands {
|
|
|
|
if sub_cmd.name == arg {
|
2021-04-25 21:40:38 +03:00
|
|
|
cmd = unsafe { &sub_cmd }
|
2020-07-18 14:24:10 +03:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
args := help_cmd.args.join(' ')
|
2020-09-30 15:57:22 +03:00
|
|
|
println('Invalid command: $args')
|
2020-07-18 14:24:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print(cmd.help_message())
|
|
|
|
} else {
|
|
|
|
if help_cmd.parent != 0 {
|
|
|
|
print(help_cmd.parent.help_message())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
|
2020-07-18 14:24:10 +03:00
|
|
|
fn (cmd Command) help_message() string {
|
2019-11-21 15:03:12 +03:00
|
|
|
mut help := ''
|
2020-07-18 14:24:10 +03:00
|
|
|
help += 'Usage: $cmd.full_name()'
|
|
|
|
if cmd.flags.len > 0 {
|
|
|
|
help += ' [flags]'
|
|
|
|
}
|
|
|
|
if cmd.commands.len > 0 {
|
|
|
|
help += ' [commands]'
|
|
|
|
}
|
|
|
|
if cmd.usage.len > 0 {
|
|
|
|
help += ' $cmd.usage'
|
2020-09-16 23:01:44 +03:00
|
|
|
} else {
|
|
|
|
for i in 0 .. cmd.required_args {
|
|
|
|
help += ' <arg$i>'
|
|
|
|
}
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
2020-09-16 23:01:44 +03:00
|
|
|
help += '\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
if cmd.description != '' {
|
2020-09-16 23:01:44 +03:00
|
|
|
help += '\n$cmd.description\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
2020-06-29 18:59:55 +03:00
|
|
|
mut abbrev_len := 0
|
2021-01-25 12:26:20 +03:00
|
|
|
mut name_len := cli.min_description_indent_len
|
2021-08-11 12:26:17 +03:00
|
|
|
if cmd.posix_mode {
|
2020-07-18 14:24:10 +03:00
|
|
|
for flag in cmd.flags {
|
2021-08-12 09:25:28 +03:00
|
|
|
if flag.abbrev != '' {
|
|
|
|
abbrev_len = max(abbrev_len, flag.abbrev.len + cli.spacing + 1) // + 1 for '-' in front
|
|
|
|
}
|
2021-01-25 12:26:20 +03:00
|
|
|
name_len = max(name_len, abbrev_len + flag.name.len + cli.spacing + 2) // + 2 for '--' in front
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
|
|
|
for command in cmd.commands {
|
2021-01-25 12:26:20 +03:00
|
|
|
name_len = max(name_len, command.name.len + cli.spacing)
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for flag in cmd.flags {
|
2021-08-12 09:25:28 +03:00
|
|
|
if flag.abbrev != '' {
|
|
|
|
abbrev_len = max(abbrev_len, flag.abbrev.len + cli.spacing + 1) // + 1 for '-' in front
|
|
|
|
}
|
2021-01-25 12:26:20 +03:00
|
|
|
name_len = max(name_len, abbrev_len + flag.name.len + cli.spacing + 1) // + 1 for '-' in front
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
|
|
|
for command in cmd.commands {
|
2021-01-25 12:26:20 +03:00
|
|
|
name_len = max(name_len, command.name.len + cli.spacing)
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
2020-06-29 18:59:55 +03:00
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
if cmd.flags.len > 0 {
|
2020-09-16 23:01:44 +03:00
|
|
|
help += '\nFlags:\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
for flag in cmd.flags {
|
|
|
|
mut flag_name := ''
|
2021-08-11 12:26:17 +03:00
|
|
|
prefix := if cmd.posix_mode { '--' } else { '-' }
|
|
|
|
if flag.abbrev != '' {
|
2020-06-29 18:59:55 +03:00
|
|
|
abbrev_indent := ' '.repeat(abbrev_len - flag.abbrev.len - 1) // - 1 for '-' in front
|
2021-08-11 12:26:17 +03:00
|
|
|
flag_name = '-$flag.abbrev$abbrev_indent$prefix$flag.name'
|
2020-07-18 14:24:10 +03:00
|
|
|
} else {
|
2021-08-11 12:26:17 +03:00
|
|
|
abbrev_indent := ' '.repeat(abbrev_len)
|
|
|
|
flag_name = '$abbrev_indent$prefix$flag.name'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
mut required := ''
|
|
|
|
if flag.required {
|
|
|
|
required = ' (required)'
|
|
|
|
}
|
2021-01-25 12:26:20 +03:00
|
|
|
base_indent := ' '.repeat(cli.base_indent_len)
|
2020-06-29 18:59:55 +03:00
|
|
|
description_indent := ' '.repeat(name_len - flag_name.len)
|
2021-01-23 11:33:22 +03:00
|
|
|
help += '$base_indent$flag_name$description_indent' +
|
2021-01-25 12:26:20 +03:00
|
|
|
pretty_description(flag.description + required, cli.base_indent_len + name_len) +
|
|
|
|
'\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if cmd.commands.len > 0 {
|
2020-09-16 23:01:44 +03:00
|
|
|
help += '\nCommands:\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
for command in cmd.commands {
|
2021-01-25 12:26:20 +03:00
|
|
|
base_indent := ' '.repeat(cli.base_indent_len)
|
2020-06-29 18:59:55 +03:00
|
|
|
description_indent := ' '.repeat(name_len - command.name.len)
|
2021-01-23 11:33:22 +03:00
|
|
|
help += '$base_indent$command.name$description_indent' +
|
|
|
|
pretty_description(command.description, name_len) + '\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-18 14:24:10 +03:00
|
|
|
return help
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
2020-04-17 00:01:04 +03:00
|
|
|
|
2020-04-20 15:50:15 +03:00
|
|
|
// pretty_description resizes description text depending on terminal width.
|
|
|
|
// Essentially, smart wrap-around
|
2020-06-29 18:59:55 +03:00
|
|
|
fn pretty_description(s string, indent_len int) string {
|
2020-04-20 15:50:15 +03:00
|
|
|
width, _ := term.get_terminal_size()
|
|
|
|
// Don't prettify if the terminal is that small, it won't be pretty anyway.
|
2020-07-18 14:24:10 +03:00
|
|
|
if indent_len > width {
|
2020-04-20 15:50:15 +03:00
|
|
|
return s
|
|
|
|
}
|
2020-06-29 18:59:55 +03:00
|
|
|
indent := ' '.repeat(indent_len)
|
|
|
|
chars_per_line := width - indent_len
|
2020-04-20 15:50:15 +03:00
|
|
|
// Give us enough room, better a little bigger than smaller
|
|
|
|
mut acc := strings.new_builder(((s.len / chars_per_line) + 1) * (width + 1))
|
2020-06-29 15:47:20 +03:00
|
|
|
for k, line in s.split('\n') {
|
2020-07-18 14:24:10 +03:00
|
|
|
if k != 0 {
|
2021-02-22 14:18:11 +03:00
|
|
|
acc.write_string('\n$indent')
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
2020-06-29 15:47:20 +03:00
|
|
|
mut i := chars_per_line - 2
|
|
|
|
mut j := 0
|
|
|
|
for ; i < line.len; i += chars_per_line - 2 {
|
2020-07-22 20:53:49 +03:00
|
|
|
for line[i] != ` ` {
|
2020-07-18 14:24:10 +03:00
|
|
|
i--
|
|
|
|
}
|
2020-06-29 15:47:20 +03:00
|
|
|
// indent was already done the first iteration
|
2020-07-18 14:24:10 +03:00
|
|
|
if j != 0 {
|
2021-02-22 14:18:11 +03:00
|
|
|
acc.write_string(indent)
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
2020-06-29 15:47:20 +03:00
|
|
|
acc.writeln(line[j..i].trim_space())
|
|
|
|
j = i
|
|
|
|
}
|
|
|
|
// We need this even though it should never happen
|
|
|
|
if j != 0 {
|
2021-02-22 14:18:11 +03:00
|
|
|
acc.write_string(indent)
|
2020-06-29 15:47:20 +03:00
|
|
|
}
|
2021-02-22 14:18:11 +03:00
|
|
|
acc.write_string(line[j..].trim_space())
|
2020-04-20 15:50:15 +03:00
|
|
|
}
|
|
|
|
return acc.str()
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:23:03 +03:00
|
|
|
fn max(a int, b int) int {
|
2020-07-18 14:24:10 +03:00
|
|
|
res := if a > b { a } else { b }
|
|
|
|
return res
|
2020-04-17 00:01:04 +03:00
|
|
|
}
|