v: remove the automatic passing of structs with more than 8 fields by reference (related #17159) (#22547)

This commit is contained in:
yuyi 2024-10-17 16:04:23 +08:00 committed by GitHub
parent 5f413f48f8
commit e97036a25b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
45 changed files with 146 additions and 175 deletions

View file

@ -57,7 +57,7 @@ pub mut:
type Defaults = CommandFlag | bool
// str returns the `string` representation of the `Command`.
pub fn (cmd Command) str() string {
pub fn (cmd &Command) str() string {
mut res := []string{}
res << 'Command{'
res << ' name: "${cmd.name}"'
@ -88,20 +88,20 @@ pub fn (cmd Command) str() string {
}
// is_root returns `true` if this `Command` has no parents.
pub fn (cmd Command) is_root() bool {
pub fn (cmd &Command) is_root() bool {
return isnil(cmd.parent)
}
// root returns the root `Command` of the command chain.
pub fn (cmd Command) root() Command {
pub fn (cmd &Command) root() Command {
if cmd.is_root() {
return cmd
return *cmd
}
return cmd.parent.root()
}
// full_name returns the full `string` representation of all commands int the chain.
pub fn (cmd Command) full_name() string {
pub fn (cmd &Command) full_name() string {
if cmd.is_root() {
return cmd.name
}
@ -317,7 +317,7 @@ fn (mut cmd Command) handle_cb(cb FnCommandCallback, label string) {
}
}
fn (cmd Command) check_help_flag() {
fn (cmd &Command) check_help_flag() {
if cmd.defaults.parsed.help.flag && cmd.flags.contains('help') {
help_flag := cmd.flags.get_bool('help') or { return } // ignore error and handle command normally
if help_flag {
@ -327,7 +327,7 @@ fn (cmd Command) check_help_flag() {
}
}
fn (cmd Command) check_man_flag() {
fn (cmd &Command) check_man_flag() {
if cmd.defaults.parsed.man.flag && cmd.flags.contains('man') {
man_flag := cmd.flags.get_bool('man') or { return } // ignore error and handle command normally
if man_flag {
@ -337,7 +337,7 @@ fn (cmd Command) check_man_flag() {
}
}
fn (cmd Command) check_version_flag() {
fn (cmd &Command) check_version_flag() {
if cmd.defaults.parsed.version.flag && cmd.version != '' && cmd.flags.contains('version') {
version_flag := cmd.flags.get_bool('version') or { return } // ignore error and handle command normally
if version_flag {
@ -347,7 +347,7 @@ fn (cmd Command) check_version_flag() {
}
}
fn (cmd Command) check_required_flags() {
fn (cmd &Command) check_required_flags() {
for flag in cmd.flags {
if flag.required && flag.value.len == 0 {
full_name := cmd.full_name()
@ -358,7 +358,7 @@ fn (cmd Command) check_required_flags() {
// execute_help executes the callback registered
// for the `-h`/`--help` flag option.
pub fn (cmd Command) execute_help() {
pub fn (cmd &Command) execute_help() {
if cmd.commands.contains('help') {
help_cmd := cmd.commands.get('help') or { return } // ignore error and handle command normally
if !isnil(help_cmd.execute) {
@ -371,7 +371,7 @@ pub fn (cmd Command) execute_help() {
// execute_help executes the callback registered
// for the `-man` flag option.
pub fn (cmd Command) execute_man() {
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) }

View file

@ -43,7 +43,7 @@ pub fn (flags []Flag) get_all_found() []Flag {
// get_bool returns `true` if the flag is set.
// get_bool returns an error if the `FlagType` is not boolean.
pub fn (flag Flag) get_bool() !bool {
pub fn (flag &Flag) get_bool() !bool {
if flag.flag != .bool {
return error('${flag.name}: Invalid flag type `${flag.flag}`, expected `bool`')
}
@ -62,7 +62,7 @@ pub fn (flags []Flag) get_bool(name string) !bool {
// get_int returns the `int` value argument of the flag.
// get_int returns an error if the `FlagType` is not integer.
pub fn (flag Flag) get_int() !int {
pub fn (flag &Flag) get_int() !int {
if flag.flag != .int {
return error('${flag.name}: Invalid flag type `${flag.flag}`, expected `int`')
}
@ -78,7 +78,7 @@ pub fn (flag Flag) get_int() !int {
// get_ints returns the array of `int` value argument of the flag specified in `name`.
// get_ints returns an error if the `FlagType` is not integer.
pub fn (flag Flag) get_ints() ![]int {
pub fn (flag &Flag) get_ints() ![]int {
if flag.flag != .int_array {
return error('${flag.name}: Invalid flag type `${flag.flag}`, expected `int_array`')
}
@ -114,7 +114,7 @@ pub fn (flags []Flag) get_ints(name string) ![]int {
// get_float returns the `f64` value argument of the flag.
// get_float returns an error if the `FlagType` is not floating point.
pub fn (flag Flag) get_float() !f64 {
pub fn (flag &Flag) get_float() !f64 {
if flag.flag != .float {
return error('${flag.name}: Invalid flag type `${flag.flag}`, expected `float`')
}
@ -130,7 +130,7 @@ pub fn (flag Flag) get_float() !f64 {
// get_floats returns the `f64` value argument of the flag.
// get_floats returns an error if the `FlagType` is not floating point.
pub fn (flag Flag) get_floats() ![]f64 {
pub fn (flag &Flag) get_floats() ![]f64 {
if flag.flag != .float_array {
return error('${flag.name}: Invalid flag type `${flag.flag}`, expected `float_array`')
}
@ -166,7 +166,7 @@ pub fn (flags []Flag) get_floats(name string) ![]f64 {
// get_string returns the `string` value argument of the flag.
// get_string returns an error if the `FlagType` is not string.
pub fn (flag Flag) get_string() !string {
pub fn (flag &Flag) get_string() !string {
if flag.flag != .string {
return error('${flag.name}: Invalid flag type `${flag.flag}`, expected `string`')
}
@ -182,7 +182,7 @@ pub fn (flag Flag) get_string() !string {
// get_strings returns the array of `string` value argument of the flag.
// get_strings returns an error if the `FlagType` is not string.
pub fn (flag Flag) get_strings() ![]string {
pub fn (flag &Flag) get_strings() ![]string {
if flag.flag != .string_array {
return error('${flag.name}: Invalid flag type `${flag.flag}`, expected `string_array`')
}
@ -285,7 +285,7 @@ fn (flags []Flag) contains(name string) bool {
}
// 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 default value is set and no value provide, use default value.
return flag.default_value

View file

@ -43,7 +43,7 @@ pub fn print_help_for_command(cmd Command) ! {
}
// help_message returns a generated help message as a `string` for the `Command`.
pub fn (cmd Command) help_message() string {
pub fn (cmd &Command) help_message() string {
mut help := ''
help += 'Usage: ${cmd.full_name()}'
if cmd.flags.len > 0 {

View file

@ -38,7 +38,7 @@ pub fn print_manpage_for_command(cmd Command) ! {
// manpage returns a `string` containing the mdoc(7) manpage for
// this `Command`
pub fn (cmd Command) manpage() string {
pub fn (cmd &Command) manpage() string {
mut mdoc := '.Dd ${time.now().strftime('%B %d, %Y')}\n'
mdoc += '.Dt ${cmd.full_name().replace(' ', '-').to_upper()} 1\n'
mdoc += '.Os\n.Sh NAME\n.Nm ${cmd.full_name().replace(' ', '-')}\n.Nd ${cmd.description}\n'

View file

@ -36,6 +36,6 @@ fn print_version_for_command(cmd Command) ! {
}
// version returns a generated version `string` for the `Command`.
pub fn (cmd Command) version() string {
pub fn (cmd &Command) version() string {
return '${cmd.name} version ${cmd.version}'
}