cli: have a posix mode to handle -- (#11133)
parent
18be9e52be
commit
8c2f7901aa
|
@ -28,6 +28,7 @@ pub mut:
|
||||||
flags []Flag
|
flags []Flag
|
||||||
required_args int
|
required_args int
|
||||||
args []string
|
args []string
|
||||||
|
posix_mode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns the `string` representation of the `Command`.
|
// 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() {
|
pub fn (mut cmd Command) setup() {
|
||||||
for mut subcmd in cmd.commands {
|
for mut subcmd in cmd.commands {
|
||||||
subcmd.parent = unsafe { cmd }
|
subcmd.parent = unsafe { cmd }
|
||||||
|
subcmd.posix_mode = cmd.posix_mode
|
||||||
subcmd.setup()
|
subcmd.setup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,11 +148,11 @@ pub fn (mut cmd Command) parse(args []string) {
|
||||||
// `-v`/`--version` flags to the `Command`.
|
// `-v`/`--version` flags to the `Command`.
|
||||||
fn (mut cmd Command) add_default_flags() {
|
fn (mut cmd Command) add_default_flags() {
|
||||||
if !cmd.disable_help && !cmd.flags.contains('help') {
|
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))
|
cmd.add_flag(help_flag(use_help_abbrev))
|
||||||
}
|
}
|
||||||
if !cmd.disable_version && cmd.version != '' && !cmd.flags.contains('version') {
|
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))
|
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 {
|
for i in 0 .. cmd.flags.len {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut flag := &cmd.flags[i]
|
mut flag := &cmd.flags[i]
|
||||||
if flag.matches(cmd.args, cmd.flags.have_abbrev()) {
|
if flag.matches(cmd.args, cmd.posix_mode) {
|
||||||
found = true
|
found = true
|
||||||
flag.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')
|
println('Failed to parse flag `${cmd.args[0]}`: $err')
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,6 +85,7 @@ fn test_if_flag_gets_set_with_long_arg() {
|
||||||
mut cmd := cli.Command{
|
mut cmd := cli.Command{
|
||||||
name: 'command'
|
name: 'command'
|
||||||
execute: flag_should_be_set
|
execute: flag_should_be_set
|
||||||
|
posix_mode: true
|
||||||
}
|
}
|
||||||
cmd.add_flag(cli.Flag{
|
cmd.add_flag(cli.Flag{
|
||||||
flag: .string
|
flag: .string
|
||||||
|
|
|
@ -211,8 +211,8 @@ pub fn (flags []Flag) get_strings(name string) ?[]string {
|
||||||
|
|
||||||
// parse parses flag values from arguments and return
|
// parse parses flag values from arguments and return
|
||||||
// an array of arguments with all consumed elements removed.
|
// an array of arguments with all consumed elements removed.
|
||||||
fn (mut flag Flag) parse(args []string, with_abbrev bool) ?[]string {
|
fn (mut flag Flag) parse(args []string, posix_mode bool) ?[]string {
|
||||||
if flag.matches(args, with_abbrev) {
|
if flag.matches(args, posix_mode) {
|
||||||
if flag.flag == .bool {
|
if flag.flag == .bool {
|
||||||
new_args := flag.parse_bool(args) ?
|
new_args := flag.parse_bool(args) ?
|
||||||
return new_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.
|
// matches returns `true` if first arg in `args` matches this flag.
|
||||||
fn (mut flag Flag) matches(args []string, with_abbrev bool) bool {
|
fn (mut flag Flag) matches(args []string, posix_mode bool) bool {
|
||||||
if with_abbrev {
|
prefix := if posix_mode { '--' } else { '-' }
|
||||||
return (flag.name != '' && args[0] == '--$flag.name')
|
return (flag.name != '' && args[0] == '$prefix$flag.name')
|
||||||
|| (flag.name != '' && args[0].starts_with('--$flag.name='))
|
|| (flag.name != '' && args[0].starts_with('$prefix$flag.name='))
|
||||||
|| (flag.abbrev != '' && args[0] == '-$flag.abbrev')
|
|| (flag.abbrev != '' && args[0] == '-$flag.abbrev')
|
||||||
|| (flag.abbrev != '' && args[0].starts_with('-$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) parse_raw(args []string) ?[]string {
|
fn (mut flag Flag) parse_raw(args []string) ?[]string {
|
||||||
|
@ -289,16 +285,6 @@ fn (flags []Flag) contains(name string) bool {
|
||||||
return false
|
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.
|
// Check if value is set by command line option. If not, return default value.
|
||||||
fn (flag Flag) get_value_or_default_value() []string {
|
fn (flag Flag) get_value_or_default_value() []string {
|
||||||
if flag.value.len == 0 && flag.default_value.len > 0 {
|
if flag.value.len == 0 && flag.default_value.len > 0 {
|
||||||
|
|
|
@ -76,7 +76,7 @@ fn (cmd Command) help_message() string {
|
||||||
}
|
}
|
||||||
mut abbrev_len := 0
|
mut abbrev_len := 0
|
||||||
mut name_len := cli.min_description_indent_len
|
mut name_len := cli.min_description_indent_len
|
||||||
if cmd.flags.have_abbrev() {
|
if cmd.posix_mode {
|
||||||
for flag in cmd.flags {
|
for flag in cmd.flags {
|
||||||
abbrev_len = max(abbrev_len, flag.abbrev.len + cli.spacing + 1) // + 1 for '-' in front
|
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
|
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'
|
help += '\nFlags:\n'
|
||||||
for flag in cmd.flags {
|
for flag in cmd.flags {
|
||||||
mut flag_name := ''
|
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
|
abbrev_indent := ' '.repeat(abbrev_len - flag.abbrev.len - 1) // - 1 for '-' in front
|
||||||
flag_name = '-$flag.abbrev$abbrev_indent--$flag.name'
|
flag_name = '-$flag.abbrev$abbrev_indent$prefix$flag.name'
|
||||||
} else if cmd.flags.have_abbrev() {
|
|
||||||
abbrev_indent := ' '.repeat(abbrev_len)
|
|
||||||
flag_name = '$abbrev_indent--$flag.name'
|
|
||||||
} else {
|
} else {
|
||||||
flag_name = '-$flag.name'
|
abbrev_indent := ' '.repeat(abbrev_len)
|
||||||
|
flag_name = '$abbrev_indent$prefix$flag.name'
|
||||||
}
|
}
|
||||||
mut required := ''
|
mut required := ''
|
||||||
if flag.required {
|
if flag.required {
|
||||||
|
|
Loading…
Reference in New Issue