Skip to content

Commit

Permalink
docs: fix samples, add examples (#2)
Browse files Browse the repository at this point in the history
Fixes README samples (clix -> cli), adds proper examples in the _examples dir.

Also removes the testify dependency, to keep this package as clean as possible
  • Loading branch information
juliusmh committed May 13, 2020
1 parent b8f4629 commit 0304317
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 26 deletions.
43 changes: 23 additions & 20 deletions README.md
Expand Up @@ -3,9 +3,9 @@
Cli-X is a command line library for Go, inspired by
[`spf13/cobra`](https://github.com/spf13/cobra).

- :package: **`struct` based API**: Similar to `cobra`, `clix` features a `struct` based
- :package: **`struct` based API**: Similar to `cobra`, `go-clix/cli` features a `struct` based
API for easy composition and discovery of available options.
- :children_crossing: [**Subcommands**](#subcommands): `clix.Command` can be nested for a `git`
- :children_crossing: [**Subcommands**](#subcommands): `cli.Command` can be nested for a `git`
like experience.
- :pushpin: [**Flags**](#flags): Every command has it's own set of flags. POSIX compliant
using `spf13/pflag`.
Expand Down Expand Up @@ -34,12 +34,12 @@ import (

func main() {
// create the root command
rootCmd := clix.Command{
rootCmd := cli.Command{
Use: "greet",
Short: "print a message",
Run: func(cmd *clix.Command, args []string) error {
Run: func(cmd *cli.Command, args []string) error {
fmt.Println("Hello from Cli-X!")
}
},
}

// run and check for errors
Expand All @@ -57,19 +57,21 @@ Every command may have children:
```go
// use a func to return a Command instead of
// a global variable and `init()`
func applyCmd() *clix.Command {
cmd := &clix.Command{
func applyCmd() *cli.Command {
cmd := &cli.Command{
Use: "apply",
Short: "apply the changes"
}

cmd.Run = func(cmd *clix.Command, args []string) error {
cmd.Run = func(cmd *cli.Command, args []string) error {
fmt.Println("applied", args[0])
}

return cmd
}

func main() {
rootCmd := &clix.Comand{
rootCmd := &cli.Comand{
Use: "kubectl",
Short: "Kubernetes management tool",
}
Expand All @@ -95,20 +97,21 @@ func main() {
A `pflag.FlagSet` can be accessed per command using `*Command.Flags()`:

```go
func applyCmd() *clix.Command {
cmd := &clix.Command{
func applyCmd() *cli.Command {
cmd := &cli.Command{
Use: "apply",
Short: "apply the changes"
}

force := cmd.Flags().BoolP("force", "f", false, "skip checks")

cmd.Run = func(cmd *clix.Command, args []string) error {
cmd.Run = func(cmd *cli.Command, args []string) error {
fmt.Println("applied", args[0])
if *force {
fmt.Println("The force was with us.")
}
}
return cmd
}
```

Expand All @@ -117,8 +120,8 @@ func applyCmd() *clix.Command {
To make the `apply` subcommand also available as `make` and `do`:

```go
func applyCmd() *clix.Command {
cmd := &clix.Command{
func applyCmd() *cli.Command {
cmd := &cli.Command{
Use: "apply",
Aliases: []string{"make", "do"},
Short: "apply the changes"
Expand Down Expand Up @@ -161,12 +164,12 @@ import (
"github.com/go-clix/cli"
)

func logsCmd() *clix.Command {
cmd := &clix.Command{
func logsCmd() *cli.Command {
cmd := &cli.Command{
Use: "logs",
Short: "show logs of a pod",
Predictors: map[string]complete.Predictor{
"output": clix.PredictSet("lines", "json", "logfmt"),
"output": cli.PredictSet("lines", "json", "logfmt"),
},
}

Expand Down Expand Up @@ -200,12 +203,12 @@ import (
"github.com/go-clix/cli"
)

func applyCmd() *clix.Command {
cmd := &clix.Command{
func applyCmd() *cli.Command {
cmd := &cli.Command{
Use: "logs",
Short: "show logs of a pod",
// we expect one argument which can be anything
Args: clix.ArgsExact(1),
Args: cli.ArgsExact(1),
}
}
```
31 changes: 31 additions & 0 deletions _examples/aliases/main.go
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"os"

"github.com/go-clix/cli"
)

// Keep in mind that in `--help` outputs the command will still be called `apply`.


func applyCmd() *cli.Command {
return &cli.Command{
Use: "apply",
Aliases: []string{"make", "do"},
Short: "apply the changes",
}
}

func main() {
rootCmd := &cli.Command{
Use: "aliases",
Short: "Subcommand has aliases.",
}
rootCmd.AddCommand(applyCmd())
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
26 changes: 26 additions & 0 deletions _examples/basic/main.go
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"os"

"github.com/go-clix/cli"
)

func main(){
// create the root command
rootCmd := cli.Command{
Use: "greet",
Short: "print a message",
Run: func(cmd *cli.Command, args []string) error {
fmt.Println("Hello from Cli-X!")
return nil
},
}

// run and check for errors
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
47 changes: 47 additions & 0 deletions _examples/flags/main.go
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"os"

"github.com/go-clix/cli"
)

// A `pflag.FlagSet` can be accessed per command using `*Command.Flags()`:

func applyCmd() *cli.Command {
cmd := &cli.Command{
Use: "apply",
Short: "apply the changes",
}

force := cmd.Flags().BoolP("force", "f", false, "skip checks")

cmd.Run = func(cmd *cli.Command, args []string) error {
fmt.Println("applied", args[0])
if *force {
fmt.Println("The force was with us.")
}
return nil
}
return cmd
}


func main() {
rootCmd := &cli.Command{
Use: "kubectl",
Short: "Kubernetes management tool",
}

// add the child command
rootCmd.AddCommand(
applyCmd(),
)

// run and check for errors
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
36 changes: 36 additions & 0 deletions _examples/subcommands/main.go
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"os"

"github.com/go-clix/cli"
)

// use a func to return a Command instead of
// a global variable and `init()`
func applyCmd() *cli.Command {
cmd := &cli.Command{
Use: "apply",
Short: "apply the changes",
}

cmd.Run = func(cmd *cli.Command, args []string) error {
fmt.Println("applied", args[0])
return nil
}

return cmd
}

func main() {
rootCmd := &cli.Command{
Use: "subcommands",
Short: "This command has sub commands.",
}
rootCmd.AddCommand(applyCmd())
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions childs.go → children.go
Expand Up @@ -4,8 +4,8 @@ import "fmt"

// AddCommand adds the supplied commands as subcommands.
// This command is set as the parent of the new children.
func (c *Command) AddCommand(childs ...*Command) {
for _, child := range childs {
func (c *Command) AddCommand(children ...*Command) {
for _, child := range children {
child.parentPtr = c
c.children = append(c.children, child)
}
Expand Down
7 changes: 4 additions & 3 deletions flags_test.go
@@ -1,9 +1,8 @@
package cli

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestStripFlags(t *testing.T) {
Expand Down Expand Up @@ -73,6 +72,8 @@ func TestStripFlags(t *testing.T) {

for _, test := range tests {
got := stripFlags(test.input, c)
assert.Equal(t, test.output, got)
if !reflect.DeepEqual(test.output, got) {
t.Fatalf("want %+v but got %+v", got, test.output)
}
}
}
1 change: 0 additions & 1 deletion go.mod
Expand Up @@ -5,5 +5,4 @@ go 1.14
require (
github.com/posener/complete v1.2.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.4.0
)

0 comments on commit 0304317

Please sign in to comment.