2019-11-21 15:03:12 +03:00
|
|
|
module cli
|
|
|
|
|
2022-03-04 13:28:11 +03:00
|
|
|
import term
|
|
|
|
|
2020-08-21 00:14:53 +03:00
|
|
|
type FnCommandCallback = fn (cmd Command) ?
|
2020-07-18 14:24:10 +03:00
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// str returns the `string` representation of the callback.
|
2020-07-18 14:24:10 +03:00
|
|
|
pub fn (f FnCommandCallback) str() string {
|
|
|
|
return 'FnCommandCallback=>' + ptr_str(f)
|
|
|
|
}
|
2020-04-16 15:50:04 +03:00
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// Command is a structured representation of a single command
|
|
|
|
// or chain of commands.
|
2019-11-21 15:03:12 +03:00
|
|
|
pub struct Command {
|
|
|
|
pub mut:
|
2020-07-18 14:24:10 +03:00
|
|
|
name string
|
|
|
|
usage string
|
|
|
|
description string
|
2022-04-03 10:12:47 +03:00
|
|
|
man_description string
|
2020-07-18 14:24:10 +03:00
|
|
|
version string
|
|
|
|
pre_execute FnCommandCallback
|
|
|
|
execute FnCommandCallback
|
|
|
|
post_execute FnCommandCallback
|
|
|
|
disable_help bool
|
2022-04-03 10:12:47 +03:00
|
|
|
disable_man bool
|
2019-11-21 15:03:12 +03:00
|
|
|
disable_version bool
|
2020-07-18 14:24:10 +03:00
|
|
|
disable_flags bool
|
2021-02-21 17:17:50 +03:00
|
|
|
sort_flags bool
|
|
|
|
sort_commands bool
|
2020-07-18 14:24:10 +03:00
|
|
|
parent &Command = 0
|
|
|
|
commands []Command
|
|
|
|
flags []Flag
|
2020-08-21 00:14:53 +03:00
|
|
|
required_args int
|
2020-07-18 14:24:10 +03:00
|
|
|
args []string
|
2021-08-11 12:26:17 +03:00
|
|
|
posix_mode bool
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// str returns the `string` representation of the `Command`.
|
2020-07-18 14:24:10 +03:00
|
|
|
pub fn (cmd Command) str() string {
|
|
|
|
mut res := []string{}
|
|
|
|
res << 'Command{'
|
|
|
|
res << ' name: "$cmd.name"'
|
|
|
|
res << ' usage: "$cmd.usage"'
|
|
|
|
res << ' version: "$cmd.version"'
|
|
|
|
res << ' description: "$cmd.description"'
|
2022-04-03 10:12:47 +03:00
|
|
|
res << ' man_description: "$cmd.man_description"'
|
2020-07-18 14:24:10 +03:00
|
|
|
res << ' disable_help: $cmd.disable_help'
|
2022-04-03 10:12:47 +03:00
|
|
|
res << ' disable_man: $cmd.disable_man'
|
2020-07-18 14:24:10 +03:00
|
|
|
res << ' disable_flags: $cmd.disable_flags'
|
|
|
|
res << ' disable_version: $cmd.disable_version'
|
|
|
|
res << ' sort_flags: $cmd.sort_flags'
|
|
|
|
res << ' sort_commands: $cmd.sort_commands'
|
|
|
|
res << ' cb execute: $cmd.execute'
|
|
|
|
res << ' cb pre_execute: $cmd.pre_execute'
|
|
|
|
res << ' cb post_execute: $cmd.post_execute'
|
2022-05-20 18:30:16 +03:00
|
|
|
if unsafe { cmd.parent == 0 } {
|
2020-07-18 14:24:10 +03:00
|
|
|
res << ' parent: &Command(0)'
|
|
|
|
} else {
|
|
|
|
res << ' parent: &Command{$cmd.parent.name ...}'
|
|
|
|
}
|
|
|
|
res << ' commands: $cmd.commands'
|
|
|
|
res << ' flags: $cmd.flags'
|
2020-08-21 00:14:53 +03:00
|
|
|
res << ' required_args: $cmd.required_args'
|
2020-07-18 14:24:10 +03:00
|
|
|
res << ' args: $cmd.args'
|
|
|
|
res << '}'
|
|
|
|
return res.join('\n')
|
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// is_root returns `true` if this `Command` has no parents.
|
2020-07-18 14:24:10 +03:00
|
|
|
pub fn (cmd Command) is_root() bool {
|
|
|
|
return isnil(cmd.parent)
|
|
|
|
}
|
2020-07-02 12:10:03 +03:00
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// root returns the root `Command` of the command chain.
|
2020-07-18 14:24:10 +03:00
|
|
|
pub fn (cmd Command) root() Command {
|
|
|
|
if cmd.is_root() {
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
return cmd.parent.root()
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// full_name returns the full `string` representation of all commands int the chain.
|
2019-11-21 15:03:12 +03:00
|
|
|
pub fn (cmd Command) full_name() string {
|
2020-07-18 14:24:10 +03:00
|
|
|
if cmd.is_root() {
|
2019-11-21 15:03:12 +03:00
|
|
|
return cmd.name
|
|
|
|
}
|
2020-07-18 14:24:10 +03:00
|
|
|
return cmd.parent.full_name() + ' $cmd.name'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// add_commands adds the `commands` array of `Command`s as sub-commands.
|
2020-07-18 14:24:10 +03:00
|
|
|
pub fn (mut cmd Command) add_commands(commands []Command) {
|
|
|
|
for command in commands {
|
|
|
|
cmd.add_command(command)
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// add_command adds `command` as a sub-command of this `Command`.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut cmd Command) add_command(command Command) {
|
2020-08-21 00:14:53 +03:00
|
|
|
mut subcmd := command
|
|
|
|
if cmd.commands.contains(subcmd.name) {
|
2022-03-04 13:28:11 +03:00
|
|
|
eprintln_exit('Command with the name `$subcmd.name` already exists')
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
2021-05-07 15:58:48 +03:00
|
|
|
subcmd.parent = unsafe { cmd }
|
2020-08-21 00:14:53 +03:00
|
|
|
cmd.commands << subcmd
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// setup ensures that all sub-commands of this `Command`
|
|
|
|
// is linked as a chain.
|
2021-01-05 14:25:25 +03:00
|
|
|
pub fn (mut cmd Command) setup() {
|
|
|
|
for mut subcmd in cmd.commands {
|
2021-05-07 15:58:48 +03:00
|
|
|
subcmd.parent = unsafe { cmd }
|
2021-08-11 12:26:17 +03:00
|
|
|
subcmd.posix_mode = cmd.posix_mode
|
2021-01-05 14:25:25 +03:00
|
|
|
subcmd.setup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// add_flags adds the array `flags` to this `Command`.
|
2020-07-18 14:24:10 +03:00
|
|
|
pub fn (mut cmd Command) add_flags(flags []Flag) {
|
|
|
|
for flag in flags {
|
|
|
|
cmd.add_flag(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// add_flag adds `flag` to this `Command`.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut cmd Command) add_flag(flag Flag) {
|
2020-08-21 00:14:53 +03:00
|
|
|
if cmd.flags.contains(flag.name) {
|
2022-03-04 13:28:11 +03:00
|
|
|
eprintln_exit('Flag with the name `$flag.name` already exists')
|
2020-07-18 14:24:10 +03:00
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
cmd.flags << flag
|
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// parse parses `args` into this structured `Command`.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut cmd Command) parse(args []string) {
|
2020-05-28 14:32:43 +03:00
|
|
|
if !cmd.disable_flags {
|
|
|
|
cmd.add_default_flags()
|
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
cmd.add_default_commands()
|
2020-07-02 12:10:03 +03:00
|
|
|
if cmd.sort_flags {
|
2020-08-12 07:11:40 +03:00
|
|
|
cmd.flags.sort(a.name < b.name)
|
2020-07-02 12:10:03 +03:00
|
|
|
}
|
|
|
|
if cmd.sort_commands {
|
2020-08-12 07:11:40 +03:00
|
|
|
cmd.commands.sort(a.name < b.name)
|
2020-07-02 12:10:03 +03:00
|
|
|
}
|
2019-11-30 12:37:34 +03:00
|
|
|
cmd.args = args[1..]
|
2020-05-28 14:32:43 +03:00
|
|
|
if !cmd.disable_flags {
|
|
|
|
cmd.parse_flags()
|
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
cmd.parse_commands()
|
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// add_default_flags adds the commonly used `-h`/`--help` and
|
|
|
|
// `-v`/`--version` flags to the `Command`.
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut cmd Command) add_default_flags() {
|
2020-07-01 11:54:34 +03:00
|
|
|
if !cmd.disable_help && !cmd.flags.contains('help') {
|
2021-08-11 12:26:17 +03:00
|
|
|
use_help_abbrev := !cmd.flags.contains('h') && cmd.posix_mode
|
2020-08-21 00:14:53 +03:00
|
|
|
cmd.add_flag(help_flag(use_help_abbrev))
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
2020-08-21 00:14:53 +03:00
|
|
|
if !cmd.disable_version && cmd.version != '' && !cmd.flags.contains('version') {
|
2021-08-11 12:26:17 +03:00
|
|
|
use_version_abbrev := !cmd.flags.contains('v') && cmd.posix_mode
|
2020-08-21 00:14:53 +03:00
|
|
|
cmd.add_flag(version_flag(use_version_abbrev))
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
2022-04-03 10:12:47 +03:00
|
|
|
if !cmd.disable_man && !cmd.flags.contains('man') {
|
|
|
|
cmd.add_flag(man_flag())
|
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// add_default_commands adds the command functions of the
|
|
|
|
// commonly used `help` and `version` flags to the `Command`.
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut cmd Command) add_default_commands() {
|
2020-07-18 14:24:10 +03:00
|
|
|
if !cmd.disable_help && !cmd.commands.contains('help') && cmd.is_root() {
|
2019-11-21 15:03:12 +03:00
|
|
|
cmd.add_command(help_cmd())
|
|
|
|
}
|
2020-08-21 00:14:53 +03:00
|
|
|
if !cmd.disable_version && cmd.version != '' && !cmd.commands.contains('version') {
|
2019-11-21 15:03:12 +03:00
|
|
|
cmd.add_command(version_cmd())
|
|
|
|
}
|
2022-04-03 10:12:47 +03:00
|
|
|
if !cmd.disable_man && !cmd.commands.contains('man') && cmd.is_root() {
|
|
|
|
cmd.add_command(man_cmd())
|
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut cmd Command) parse_flags() {
|
2019-11-21 15:03:12 +03:00
|
|
|
for {
|
|
|
|
if cmd.args.len < 1 || !cmd.args[0].starts_with('-') {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
mut found := false
|
2020-07-18 14:24:10 +03:00
|
|
|
for i in 0 .. cmd.flags.len {
|
2020-10-05 14:08:30 +03:00
|
|
|
unsafe {
|
2020-12-21 22:23:40 +03:00
|
|
|
mut flag := &cmd.flags[i]
|
2021-08-11 12:26:17 +03:00
|
|
|
if flag.matches(cmd.args, cmd.posix_mode) {
|
2020-12-21 22:23:40 +03:00
|
|
|
found = true
|
|
|
|
flag.found = true
|
2021-08-11 12:26:17 +03:00
|
|
|
cmd.args = flag.parse(cmd.args, cmd.posix_mode) or {
|
2022-03-04 13:28:11 +03:00
|
|
|
eprintln_exit('Failed to parse flag `${cmd.args[0]}`: $err')
|
2020-12-21 22:23:40 +03:00
|
|
|
}
|
|
|
|
break
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2022-03-04 13:28:11 +03:00
|
|
|
eprintln_exit('Command `$cmd.name` has no flag `${cmd.args[0]}`')
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 11:54:34 +03:00
|
|
|
fn (mut cmd Command) parse_commands() {
|
|
|
|
global_flags := cmd.flags.filter(it.global)
|
2019-11-21 15:03:12 +03:00
|
|
|
cmd.check_help_flag()
|
|
|
|
cmd.check_version_flag()
|
2022-04-03 10:12:47 +03:00
|
|
|
cmd.check_man_flag()
|
2020-07-18 14:24:10 +03:00
|
|
|
for i in 0 .. cmd.args.len {
|
2019-11-21 15:03:12 +03:00
|
|
|
arg := cmd.args[i]
|
2020-07-18 14:24:10 +03:00
|
|
|
for j in 0 .. cmd.commands.len {
|
2019-11-21 15:03:12 +03:00
|
|
|
mut command := cmd.commands[j]
|
|
|
|
if command.name == arg {
|
|
|
|
for flag in global_flags {
|
|
|
|
command.add_flag(flag)
|
|
|
|
}
|
2019-11-30 12:37:34 +03:00
|
|
|
command.parse(cmd.args[i..])
|
2019-11-21 15:03:12 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 18:30:39 +03:00
|
|
|
if cmd.is_root() && isnil(cmd.execute) {
|
2020-10-17 19:06:27 +03:00
|
|
|
if !cmd.disable_help {
|
|
|
|
cmd.execute_help()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 11:54:34 +03:00
|
|
|
// if no further command was found, execute current command
|
2020-10-07 06:39:13 +03:00
|
|
|
if cmd.required_args > 0 {
|
|
|
|
if cmd.required_args > cmd.args.len {
|
2022-03-04 13:28:11 +03:00
|
|
|
eprintln_exit('Command `$cmd.name` needs at least $cmd.required_args arguments')
|
2020-08-21 00:14:53 +03:00
|
|
|
}
|
2020-10-07 06:39:13 +03:00
|
|
|
}
|
|
|
|
cmd.check_required_flags()
|
2022-03-04 13:28:11 +03:00
|
|
|
|
|
|
|
cmd.handle_cb(cmd.pre_execute, 'preexecution')
|
|
|
|
cmd.handle_cb(cmd.execute, 'execution')
|
|
|
|
cmd.handle_cb(cmd.post_execute, 'postexecution')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut cmd Command) handle_cb(cb FnCommandCallback, label string) {
|
|
|
|
if !isnil(cb) {
|
|
|
|
cb(*cmd) or {
|
|
|
|
label_message := term.ecolorize(term.bright_red, 'cli $label error:')
|
|
|
|
eprintln_exit('$label_message $err')
|
2020-03-10 18:11:17 +03:00
|
|
|
}
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:24:10 +03:00
|
|
|
fn (cmd Command) check_help_flag() {
|
|
|
|
if !cmd.disable_help && cmd.flags.contains('help') {
|
2020-12-21 22:23:40 +03:00
|
|
|
help_flag := cmd.flags.get_bool('help') or { return } // ignore error and handle command normally
|
2019-11-21 15:03:12 +03:00
|
|
|
if help_flag {
|
2020-07-18 14:24:10 +03:00
|
|
|
cmd.execute_help()
|
2019-11-21 15:03:12 +03:00
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 10:12:47 +03:00
|
|
|
fn (cmd Command) check_man_flag() {
|
|
|
|
if !cmd.disable_man && cmd.flags.contains('man') {
|
|
|
|
man_flag := cmd.flags.get_bool('man') or { return } // ignore error and handle command normally
|
|
|
|
if man_flag {
|
|
|
|
cmd.execute_man()
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:24:10 +03:00
|
|
|
fn (cmd Command) check_version_flag() {
|
2020-08-21 00:14:53 +03:00
|
|
|
if !cmd.disable_version && cmd.version != '' && cmd.flags.contains('version') {
|
2020-12-21 22:23:40 +03:00
|
|
|
version_flag := cmd.flags.get_bool('version') or { return } // ignore error and handle command normally
|
2019-11-21 15:03:12 +03:00
|
|
|
if version_flag {
|
2020-12-21 22:23:40 +03:00
|
|
|
version_cmd := cmd.commands.get('version') or { return } // ignore error and handle command normally
|
2021-03-01 02:18:14 +03:00
|
|
|
version_cmd.execute(version_cmd) or { panic(err) }
|
2019-11-21 15:03:12 +03:00
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:24:10 +03:00
|
|
|
fn (cmd Command) check_required_flags() {
|
2019-11-21 15:03:12 +03:00
|
|
|
for flag in cmd.flags {
|
2021-02-19 13:43:18 +03:00
|
|
|
if flag.required && flag.value.len == 0 {
|
2019-11-21 15:03:12 +03:00
|
|
|
full_name := cmd.full_name()
|
2022-03-04 13:28:11 +03:00
|
|
|
eprintln_exit('Flag `$flag.name` is required by `$full_name`')
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 00:15:48 +03:00
|
|
|
// execute_help executes the callback registered
|
|
|
|
// for the `-h`/`--help` flag option.
|
2021-01-07 22:33:15 +03:00
|
|
|
pub fn (cmd Command) execute_help() {
|
2020-07-18 14:24:10 +03:00
|
|
|
if cmd.commands.contains('help') {
|
2020-12-21 22:23:40 +03:00
|
|
|
help_cmd := cmd.commands.get('help') or { return } // ignore error and handle command normally
|
2021-03-01 02:18:14 +03:00
|
|
|
help_cmd.execute(help_cmd) or { panic(err) }
|
2020-07-18 14:24:10 +03:00
|
|
|
} else {
|
|
|
|
print(cmd.help_message())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 10:12:47 +03:00
|
|
|
// execute_help executes the callback registered
|
|
|
|
// for the `-man` flag option.
|
|
|
|
pub fn (cmd Command) execute_man() {
|
|
|
|
if cmd.commands.contains('man') {
|
|
|
|
man_cmd := cmd.commands.get('man') or { return }
|
|
|
|
man_cmd.execute(man_cmd) or { panic(err) }
|
|
|
|
} else {
|
|
|
|
print(cmd.manpage())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 12:10:03 +03:00
|
|
|
fn (cmds []Command) get(name string) ?Command {
|
2019-11-21 15:03:12 +03:00
|
|
|
for cmd in cmds {
|
|
|
|
if cmd.name == name {
|
2020-07-02 12:10:03 +03:00
|
|
|
return cmd
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 23:01:44 +03:00
|
|
|
return error('Command `$name` not found in $cmds')
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 12:10:03 +03:00
|
|
|
fn (cmds []Command) contains(name string) bool {
|
2019-11-21 15:03:12 +03:00
|
|
|
for cmd in cmds {
|
|
|
|
if cmd.name == name {
|
2020-07-02 12:10:03 +03:00
|
|
|
return true
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-02 12:10:03 +03:00
|
|
|
return false
|
|
|
|
}
|
2022-03-04 13:28:11 +03:00
|
|
|
|
|
|
|
[noreturn]
|
|
|
|
fn eprintln_exit(message string) {
|
|
|
|
eprintln(message)
|
|
|
|
exit(1)
|
|
|
|
}
|