From def39983fdfa3d6fb849384b13af3bd3efde9e69 Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Fri, 10 Nov 2023 00:49:44 -0500 Subject: [PATCH] Add hello world example and add more options --- command.go | 7 +++++-- examples/hello-world/cmd/main.go | 28 ++++++++++++++++++++++++++++ examples/hello-world/go.mod | 5 +++++ examples/hello-world/go.sum | 2 ++ option.go | 24 ++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 examples/hello-world/cmd/main.go create mode 100644 examples/hello-world/go.mod create mode 100644 examples/hello-world/go.sum diff --git a/command.go b/command.go index 65178fb..2ab1822 100644 --- a/command.go +++ b/command.go @@ -2,6 +2,9 @@ package cmd import "go.fifitido.net/cmd/flags" +type RunFunc func(args []string) +type RunErrFunc func(args []string) error + type Command struct { Name string ShortDescription string @@ -10,8 +13,8 @@ type Command struct { Arguments []*Argument Flags []flags.Flag Subcommands []*Command - Run func(args []string) - RunE func(args []string) error + Run RunFunc + RunE RunErrFunc isRoot bool } diff --git a/examples/hello-world/cmd/main.go b/examples/hello-world/cmd/main.go new file mode 100644 index 0000000..57064a5 --- /dev/null +++ b/examples/hello-world/cmd/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "os" + + "go.fifitido.net/cmd" +) + +var root = cmd.NewRoot( + cmd.WithShortDescription("Example command"), + cmd.WithLongDescription(`An example command to show how to use go.fifitido.net/cmd + +this example is just a simple hello world program to show +the basics of the library.`), + cmd.WithArgument("name", false), + cmd.WithRunFunc(func(args []string) { + if len(args) == 0 { + fmt.Println("Hello World!") + } else { + fmt.Printf("Hello %s!\n", args[0]) + } + }), +) + +func main() { + root.Execute(os.Args) +} diff --git a/examples/hello-world/go.mod b/examples/hello-world/go.mod new file mode 100644 index 0000000..667afe3 --- /dev/null +++ b/examples/hello-world/go.mod @@ -0,0 +1,5 @@ +module go.fifitido.net/cmd/examples/hello-world + +go 1.21.3 + +require go.fifitido.net/cmd v0.0.0-20231110043437-c32ce2efcd5f diff --git a/examples/hello-world/go.sum b/examples/hello-world/go.sum new file mode 100644 index 0000000..8622aee --- /dev/null +++ b/examples/hello-world/go.sum @@ -0,0 +1,2 @@ +go.fifitido.net/cmd v0.0.0-20231110043437-c32ce2efcd5f h1:v+sjO+t6cGEtDhStOnuypYBrkOMO4suiDVxL93rOUCs= +go.fifitido.net/cmd v0.0.0-20231110043437-c32ce2efcd5f/go.mod h1:8SaDxCG1m6WwShUlZApSbNleCvV7oTfqZIyYu4aAqO0= diff --git a/option.go b/option.go index 89783d5..27317b6 100644 --- a/option.go +++ b/option.go @@ -4,6 +4,18 @@ import "go.fifitido.net/cmd/flags" type Option func(*Command) +func WithShortDescription(s string) Option { + return func(c *Command) { + c.ShortDescription = s + } +} + +func WithLongDescription(s string) Option { + return func(c *Command) { + c.LongDescription = s + } +} + func WithArgument(name string, required bool) Option { return func(c *Command) { c.Arguments = append(c.Arguments, &Argument{name, required}) @@ -39,3 +51,15 @@ func WithSubcommands(ss []*Command) Option { c.Subcommands = append(c.Subcommands, ss...) } } + +func WithRunFunc(r RunFunc) Option { + return func(c *Command) { + c.Run = r + } +} + +func WithRunErrFunc(r RunErrFunc) Option { + return func(c *Command) { + c.RunE = r + } +}