-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a very basic Command struct (#1)
- Loading branch information
1 parent
94ce974
commit 20ff302
Showing
7 changed files
with
137 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Package cli provides a tiny, simple and minimalistic CLI framework for building Go CLI tools. | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
// Command represents a CLI command. | ||
type Command struct { | ||
// Run is the function actually implementing the command, the command and arguments to it, are passed into the function, flags | ||
// are parsed out before the arguments are passed to Run, so `args` here are the command line arguments minus flags. | ||
Run func(cmd *Command, args []string) error | ||
|
||
// flags is the set of flags for this command. | ||
flags *pflag.FlagSet | ||
|
||
// Stdin is an [io.Reader] from which command input is read. | ||
// | ||
// It defaults to [os.Stdin] but can be overridden as desired e.g. for testing. | ||
Stdin io.Reader | ||
|
||
// Stdout is an [io.Writer] to which normal command output is written. | ||
// | ||
// It defaults to [os.Stdout] but can be overridden as desired e.g. for testing. | ||
Stdout io.Writer | ||
|
||
// Stderr is an [io.Writer] to which error command output is written. | ||
// | ||
// It defaults to [os.Stderr] but can be overridden as desired e.g. for testing. | ||
Stderr io.Writer | ||
|
||
// Name is the name of the command. | ||
Name string | ||
|
||
// Short is the one line summary for the command, shown inline in the -h/--help output. | ||
Short string | ||
|
||
// Long is the long form description for the command, shown when -h/--help is called on the command itself. | ||
Long string | ||
|
||
// Example is examples of how to use the command, free form text. | ||
Example string | ||
} | ||
|
||
// Execute parses the flags and arguments, and invokes the Command's Run | ||
// function, returning any error. | ||
// | ||
// The arguments should not include the command name. | ||
// | ||
// If the flags fail to parse, an error will be returned and the Run function | ||
// will not be called. | ||
// | ||
// err := cmd.Execute(os.Args[1:]) | ||
func (c *Command) Execute(args []string) error { | ||
if c.flags == nil { | ||
c.flags = pflag.NewFlagSet(c.Name, pflag.ExitOnError) | ||
} | ||
if err := c.flags.Parse(args); err != nil { | ||
return fmt.Errorf("failed to parse command flags: %w", err) | ||
} | ||
|
||
argsWithoutFlags := c.flags.Args() | ||
|
||
return c.Run(c, argsWithoutFlags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/FollowTheProcess/cli" | ||
"github.com/FollowTheProcess/test" | ||
) | ||
|
||
func TestExecute(t *testing.T) { | ||
stderr := &bytes.Buffer{} | ||
stdout := &bytes.Buffer{} | ||
|
||
testCmd := &cli.Command{ | ||
Run: func(cmd *cli.Command, args []string) error { | ||
fmt.Fprintf(cmd.Stdout, "Oooh look, it ran, here are some args: %v\n", args) | ||
return nil | ||
}, | ||
Stdout: stdout, | ||
Stderr: stderr, | ||
Name: "test", | ||
Short: "A simple test command", | ||
Long: "Much longer description blah blah blah", | ||
} | ||
|
||
err := testCmd.Execute([]string{"arg1", "arg2", "arg3"}) | ||
test.Ok(t, err) | ||
|
||
want := "Oooh look, it ran, here are some args: [arg1 arg2 arg3]\n" | ||
test.Equal(t, stdout.String(), want) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
module github.com/FollowTheProcess/cli | ||
|
||
go 1.22 | ||
go 1.22 | ||
|
||
require ( | ||
github.com/FollowTheProcess/test v0.9.0 | ||
github.com/spf13/pflag v1.0.5 | ||
) | ||
|
||
require github.com/google/go-cmp v0.6.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/FollowTheProcess/test v0.9.0 h1:X7/cqUi6qwbB8Z8PGHI5Hk4RJJF/wRZ+3Mko3QlL/VU= | ||
github.com/FollowTheProcess/test v0.9.0/go.mod h1:9oxZcKkTAgz3bZMiHPtYCytdPcvICS+AAp5mzZzB2oA= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= |