1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/cli/command.v

219 lines
4.4 KiB
V
Raw Normal View History

2019-11-21 15:03:12 +03:00
module cli
fn nil() voidptr { return 0 }
2020-04-16 15:50:04 +03:00
2019-11-21 15:03:12 +03:00
pub struct Command {
pub mut:
name string
description string
version string
pre_execute fn(cmd Command)
execute fn(cmd Command)
post_execute fn(cmd Command)
2019-11-21 15:03:12 +03:00
disable_help bool
disable_version bool
disable_flags bool
2019-11-21 15:03:12 +03:00
sort_flags bool = true
sort_commands bool = true
parent &Command = nil()
2019-11-21 15:03:12 +03:00
commands []Command
flags []Flag
args []string
}
pub fn (cmd Command) full_name() string {
if isnil(cmd.parent) {
return cmd.name
}
return cmd.parent.full_name() + ' ${cmd.name}'
}
pub fn (cmd Command) root() Command {
if isnil(cmd.parent) {
return cmd
}
return cmd.parent.root()
}
2020-05-17 14:51:18 +03:00
pub fn (mut cmd Command) add_command(command Command) {
2019-11-21 15:03:12 +03:00
cmd.commands << command
}
2020-05-17 14:51:18 +03:00
pub fn (mut cmd Command) add_flag(flag Flag) {
2019-11-21 15:03:12 +03:00
cmd.flags << flag
}
2020-05-17 14:51:18 +03:00
pub fn (mut cmd Command) parse(args []string) {
if !cmd.disable_flags {
cmd.add_default_flags()
}
2019-11-21 15:03:12 +03:00
cmd.add_default_commands()
if cmd.sort_flags {
cmd.flags.sort()
}
if cmd.sort_commands {
cmd.commands.sort()
}
2019-11-30 12:37:34 +03:00
cmd.args = args[1..]
for i in 0..cmd.commands.len {
2019-11-21 15:03:12 +03:00
cmd.commands[i].parent = cmd
}
if !cmd.disable_flags {
cmd.parse_flags()
}
2019-11-21 15:03:12 +03:00
cmd.parse_commands()
}
2020-05-17 14:51:18 +03:00
fn (mut cmd Command) add_default_flags() {
if !cmd.disable_help && !cmd.flags.contains('help') {
cmd.add_flag(help_flag(!cmd.flags.contains('h')))
2019-11-21 15:03:12 +03:00
}
if !cmd.disable_version && cmd.version != '' && !cmd.flags.contains('version') {
cmd.add_flag(version_flag(!cmd.flags.contains('v')))
2019-11-21 15:03:12 +03:00
}
}
2020-05-17 14:51:18 +03:00
fn (mut cmd Command) add_default_commands() {
2019-11-21 15:03:12 +03:00
if !cmd.disable_help && !cmd.commands.contains('help') {
cmd.add_command(help_cmd())
}
if !cmd.disable_version && cmd.version != '' && !cmd.commands.contains('version') {
cmd.add_command(version_cmd())
}
}
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
for i in 0..cmd.flags.len {
2019-11-21 15:03:12 +03:00
mut flag := &cmd.flags[i]
if flag.matches(cmd.args) {
found = true
flag.found = true
cmd.args = flag.parse(cmd.args) or {
2019-11-21 15:03:12 +03:00
println('failed to parse flag ${cmd.args[0]}: ${err}')
exit(1)
}
break
}
}
if !found {
println('invalid flag: ${cmd.args[0]}')
exit(1)
}
}
}
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()
for i in 0..cmd.args.len {
2019-11-21 15:03:12 +03:00
arg := cmd.args[i]
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
}
}
}
// if no further command was found, execute current command
2019-11-21 15:03:12 +03:00
if int(cmd.execute) == 0 {
if !cmd.disable_help {
help_cmd := cmd.commands.get('help') or { return } // ignore error and handle command normally
help_cmd.execute(help_cmd)
2019-11-21 15:03:12 +03:00
}
} else {
cmd.check_required_flags()
2020-03-10 18:11:17 +03:00
if int(cmd.pre_execute) > 0 {
cmd.pre_execute(*cmd)
2020-03-10 18:11:17 +03:00
}
cmd.execute(*cmd)
2020-05-17 14:51:18 +03:00
2020-03-10 18:11:17 +03:00
if int(cmd.post_execute) > 0 {
cmd.post_execute(*cmd)
2020-03-10 18:11:17 +03:00
}
2019-11-21 15:03:12 +03:00
}
}
fn (mut cmd Command) check_help_flag() {
2019-11-21 15:03:12 +03:00
if cmd.disable_help {
return
}
if cmd.flags.contains('help') {
help_flag := cmd.flags.get_bool('help') or { return } // ignore error and handle command normally
if help_flag {
help_cmd := cmd.commands.get('help') or { return } // ignore error and handle command normally
help_cmd.execute(help_cmd)
2019-11-21 15:03:12 +03:00
exit(0)
}
}
}
fn (mut cmd Command) check_version_flag() {
2019-11-21 15:03:12 +03:00
if cmd.disable_version {
return
}
if cmd.version != '' && cmd.flags.contains('version') {
version_flag := cmd.flags.get_bool('version') or { return } // ignore error and handle command normally
if version_flag {
version_cmd := cmd.commands.get('version') or { return } // ignore error and handle command normally
version_cmd.execute(version_cmd)
2019-11-21 15:03:12 +03:00
exit(0)
}
}
}
fn (mut cmd Command) check_required_flags() {
2019-11-21 15:03:12 +03:00
for flag in cmd.flags {
if flag.required && flag.value == '' {
full_name := cmd.full_name()
println('flag \'${flag.name}\' is required by \'${full_name}\'')
exit(1)
}
}
}
fn (cmds []Command) get(name string) ?Command {
2019-11-21 15:03:12 +03:00
for cmd in cmds {
if cmd.name == name {
return cmd
2019-11-21 15:03:12 +03:00
}
}
return error('command \'${name}\' not found.')
2019-11-21 15:03:12 +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 {
return true
2019-11-21 15:03:12 +03:00
}
}
return false
}
fn (mut cmds []Command) sort() {
cmds.sort_with_compare(fn(a &Command, b &Command) int {
return compare_strings(&a.name, &b.name)
})
2019-11-30 12:37:34 +03:00
}