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

cli: have a posix mode to handle -- (#11133)

This commit is contained in:
div72 2021-08-11 12:26:17 +03:00 committed by GitHub
parent 18be9e52be
commit 8c2f7901aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 33 deletions

View File

@ -28,6 +28,7 @@ pub mut:
flags []Flag
required_args int
args []string
posix_mode bool
}
// str returns the `string` representation of the `Command`.
@ -103,6 +104,7 @@ pub fn (mut cmd Command) add_command(command Command) {
pub fn (mut cmd Command) setup() {
for mut subcmd in cmd.commands {
subcmd.parent = unsafe { cmd }
subcmd.posix_mode = cmd.posix_mode
subcmd.setup()
}
}
@ -146,11 +148,11 @@ pub fn (mut cmd Command) parse(args []string) {
// `-v`/`--version` flags to the `Command`.
fn (mut cmd Command) add_default_flags() {
if !cmd.disable_help && !cmd.flags.contains('help') {
use_help_abbrev := !cmd.flags.contains('h') && cmd.flags.have_abbrev()
use_help_abbrev := !cmd.flags.contains('h') && cmd.posix_mode
cmd.add_flag(help_flag(use_help_abbrev))
}
if !cmd.disable_version && cmd.version != '' && !cmd.flags.contains('version') {
use_version_abbrev := !cmd.flags.contains('v') && cmd.flags.have_abbrev()
use_version_abbrev := !cmd.flags.contains('v') && cmd.posix_mode
cmd.add_flag(version_flag(use_version_abbrev))
}
}
@ -175,10 +177,10 @@ fn (mut cmd Command) parse_flags() {
for i in 0 .. cmd.flags.len {
unsafe {
mut flag := &cmd.flags[i]
if flag.matches(cmd.args, cmd.flags.have_abbrev()) {
if flag.matches(cmd.args, cmd.posix_mode) {
found = true
flag.found = true
cmd.args = flag.parse(cmd.args, cmd.flags.have_abbrev()) or {
cmd.args = flag.parse(cmd.args, cmd.posix_mode) or {
println('Failed to parse flag `${cmd.args[0]}`: $err')
exit(1)
}

View File

@ -85,6 +85,7 @@ fn test_if_flag_gets_set_with_long_arg() {
mut cmd := cli.Command{
name: 'command'
execute: flag_should_be_set
posix_mode: true
}
cmd.add_flag(cli.Flag{
flag: .string

View File

@ -211,8 +211,8 @@ pub fn (flags []Flag) get_strings(name string) ?[]string {
// parse parses flag values from arguments and return
// an array of arguments with all consumed elements removed.
fn (mut flag Flag) parse(args []string, with_abbrev bool) ?[]string {
if flag.matches(args, with_abbrev) {
fn (mut flag Flag) parse(args []string, posix_mode bool) ?[]string {
if flag.matches(args, posix_mode) {
if flag.flag == .bool {
new_args := flag.parse_bool(args) ?
return new_args
@ -231,16 +231,12 @@ fn (mut flag Flag) parse(args []string, with_abbrev bool) ?[]string {
}
// matches returns `true` if first arg in `args` matches this flag.
fn (mut flag Flag) matches(args []string, with_abbrev bool) bool {
if with_abbrev {
return (flag.name != '' && args[0] == '--$flag.name')
|| (flag.name != '' && args[0].starts_with('--$flag.name='))
|| (flag.abbrev != '' && args[0] == '-$flag.abbrev')
|| (flag.abbrev != '' && args[0].starts_with('-$flag.abbrev='))
} else {
return (flag.name != '' && args[0] == '-$flag.name')
|| (flag.name != '' && args[0].starts_with('-$flag.name='))
}
fn (mut flag Flag) matches(args []string, posix_mode bool) bool {
prefix := if posix_mode { '--' } else { '-' }
return (flag.name != '' && args[0] == '$prefix$flag.name')
|| (flag.name != '' && args[0].starts_with('$prefix$flag.name='))
|| (flag.abbrev != '' && args[0] == '-$flag.abbrev')
|| (flag.abbrev != '' && args[0].starts_with('-$flag.abbrev='))
}
fn (mut flag Flag) parse_raw(args []string) ?[]string {
@ -289,16 +285,6 @@ fn (flags []Flag) contains(name string) bool {
return false
}
fn (flags []Flag) have_abbrev() bool {
mut have_abbrev := false
for flag in flags {
if flag.abbrev != '' {
have_abbrev = true
}
}
return have_abbrev
}
// Check if value is set by command line option. If not, return default value.
fn (flag Flag) get_value_or_default_value() []string {
if flag.value.len == 0 && flag.default_value.len > 0 {

View File

@ -76,7 +76,7 @@ fn (cmd Command) help_message() string {
}
mut abbrev_len := 0
mut name_len := cli.min_description_indent_len
if cmd.flags.have_abbrev() {
if cmd.posix_mode {
for flag in cmd.flags {
abbrev_len = max(abbrev_len, flag.abbrev.len + cli.spacing + 1) // + 1 for '-' in front
name_len = max(name_len, abbrev_len + flag.name.len + cli.spacing + 2) // + 2 for '--' in front
@ -96,14 +96,13 @@ fn (cmd Command) help_message() string {
help += '\nFlags:\n'
for flag in cmd.flags {
mut flag_name := ''
if flag.abbrev != '' && cmd.flags.have_abbrev() {
prefix := if cmd.posix_mode { '--' } else { '-' }
if flag.abbrev != '' {
abbrev_indent := ' '.repeat(abbrev_len - flag.abbrev.len - 1) // - 1 for '-' in front
flag_name = '-$flag.abbrev$abbrev_indent--$flag.name'
} else if cmd.flags.have_abbrev() {
abbrev_indent := ' '.repeat(abbrev_len)
flag_name = '$abbrev_indent--$flag.name'
flag_name = '-$flag.abbrev$abbrev_indent$prefix$flag.name'
} else {
flag_name = '-$flag.name'
abbrev_indent := ' '.repeat(abbrev_len)
flag_name = '$abbrev_indent$prefix$flag.name'
}
mut required := ''
if flag.required {