Last active
July 26, 2019 01:19
-
-
Save nasum/ae507d8069737a35e71615fe89b6d75a to your computer and use it in GitHub Desktop.
Goでコマンドラインツールを作るときの鉄板ライブラリ ref: https://qiita.com/nasum/items/f1911894802a2b92dc9e
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
$ go run main.go | |
root command | |
$ go run main.go --help | |
command line calculator | |
Usage: | |
culc [flags] | |
Flags: | |
-h, --help help for culc |
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
$ go run main.go help | |
command line calculator | |
Usage: | |
culc [flags] | |
culc [command] | |
Available Commands: | |
help Help about any command | |
sum sum culc | |
Flags: | |
-h, --help help for culc | |
Use "culc [command] --help" for more information about a command. |
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
$ go run main.go sum 1 1 | |
2 |
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
green := color.New(color.FgGreen).SprintFunc() | |
fmt.Println(green(itemOne + itemTwo)) |
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
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/nasum/culc/cmd" | |
) | |
func main() { | |
if err := cmd.RootCmd.Execute(); err != nil { | |
fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], 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
func init() { | |
cobra.OnInitialize() | |
RootCmd.AddCommand( | |
sumCmd(), | |
) | |
} |
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
package cmd | |
import ( | |
"fmt" | |
"log" | |
"strconv" | |
"github.com/spf13/cobra" | |
) | |
func sumCmd() *cobra.Command { | |
cmd := &cobra.Command{ | |
Use: "sum", | |
Short: "sum culc", | |
Args: cobra.RangeArgs(2, 2), | |
RunE: func(cmd *cobra.Command, args []string) error { | |
if len(args) == 0 { | |
return nil | |
} | |
itemOne, err := strconv.Atoi(args[0]) | |
if err != nil { | |
log.Fatal(err) | |
return nil | |
} | |
itemTwo, err := strconv.Atoi(args[1]) | |
if err != nil { | |
log.Fatal(err) | |
return nil | |
} | |
fmt.Println(itemOne + itemTwo) | |
return nil | |
}, | |
} | |
return cmd | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment