Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: fix samples, add examples (#2)
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
Showing
8 changed files
with
169 additions
and
26 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 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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} |
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 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 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