Skip to content

Commit 3c52e33

Browse files
committed
feat: cli example
1 parent bb7aa14 commit 3c52e33

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/example/clidemo/cmd/cmd.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func NewDefaultMsCliCommand() *cobra.Command {
12+
return NewDefaultMsCliCommandWithArgs(os.Args, os.Stdin, os.Stdout, os.Stderr)
13+
}
14+
15+
func NewDefaultMsCliCommandWithArgs(args []string, in io.Reader, out, errout io.Writer) *cobra.Command {
16+
cmd := NewMsCliCommand(in, out, errout)
17+
18+
return cmd
19+
}
20+
21+
func NewMsCliCommand(in io.Reader, out, errout io.Writer) *cobra.Command {
22+
cmds := &cobra.Command{
23+
Use: "msctl",
24+
Short: "msctl 是一个学习 go cli 实现的练习项目",
25+
Long: "主要是用来熟悉 cobra 以及 go 语言的使用,方便日后编写自己的 cli 工具",
26+
Run: func(cmd *cobra.Command, args []string) {},
27+
}
28+
29+
return cmds
30+
}
31+
32+
func Execute() {
33+
if err := NewDefaultMsCliCommand().Execute(); err != nil {
34+
fmt.Println(err)
35+
os.Exit(1)
36+
}
37+
}

src/example/clidemo/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/marsonshine/mscli
2+
3+
go 1.19
4+
5+
require github.com/spf13/cobra v1.6.1
6+
7+
require (
8+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
9+
github.com/spf13/pflag v1.0.5 // indirect
10+
)

src/example/clidemo/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
3+
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
6+
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
7+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

src/example/clidemo/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/marsonshine/mscli/cmd"
4+
5+
func main() {
6+
cmd.Execute()
7+
}

0 commit comments

Comments
 (0)