A command from struct generator.
go install github.com/donatorsky/go-cmder@latest
go-cmder [flags] struct CommandName
Defines a name and comma-separated list of fields for command constructor. Multiple usage allowed to generate many constructors.
A name is appended to the command name. E.g. given command name MyCmd, then -constructor=WithFoo
generates func NewMyCmdWithFoo
.
Use name default
to generate constructor based on command name. E.g. given command name MyCmd, then -constructor=default
generates func NewMyCmd
.
List of fields can be omitted to generate constructor without fields.
Excludes given struct field from command generation. Multiple usage allowed.
Includes given struct field in command generation.
When present, command is generated only from included fields.
Supersedes -exclude
flag.
Multiple usage allowed.
Includes unexported fields when generating a command.
Generates a mutable command. By default, commands are immutable, meaning that calling a setter returns new command instance.
Generates a command in given file.
By default, command is generated in command_name.go
file.
Sort fields by name when generating a command.
package foobar
//go:generate go-cmder -constructor=default Struct CreateStructCmd
type Struct struct {
Foo string
Bar int
}
Generates create_struct_cmd.go
file:
package foobar
type CreateStructCmd struct {
vFoo string
hasFoo bool
vBar int
hasBar bool
}
func NewCreateStructCmd() CreateStructCmd {
return CreateStructCmd{}
}
func (cmd CreateStructCmd) Foo() string {
return cmd.vFoo
}
func (cmd CreateStructCmd) SetFoo(v string) CreateStructCmd {
cmd.hasFoo = true
cmd.vFoo = v
return cmd
}
func (cmd CreateStructCmd) HasFoo() bool {
return cmd.hasFoo
}
func (cmd CreateStructCmd) Bar() int {
return cmd.vBar
}
func (cmd CreateStructCmd) SetBar(v int) CreateStructCmd {
cmd.hasBar = true
cmd.vBar = v
return cmd
}
func (cmd CreateStructCmd) HasBar() bool {
return cmd.hasBar
}