Skip to content

Commit f32338f

Browse files
committed
cobra classfile: initial version
1 parent b336d2a commit f32338f

File tree

8 files changed

+218
-0
lines changed

8 files changed

+218
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*.so
99
*.dylib
1010

11+
.DS_Store
12+
1113
# Test binary, built with `go test -c`
1214
*.test
1315

classfile.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cobra
18+
19+
import (
20+
"reflect"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
const (
26+
GopPackage = true
27+
)
28+
29+
// -----------------------------------------------------------------------------
30+
31+
// Command is the worker class of Cobra classfile.
32+
type Command struct {
33+
cobra.Command
34+
}
35+
36+
func (p *Command) cobraCmd() *cobra.Command {
37+
return &p.Command
38+
}
39+
40+
// Main is required by Go+ compiler as the entry of a Cobra command.
41+
func (p *Command) Main(fname string) {
42+
p.Command.Use = fname
43+
}
44+
45+
// Use sets the one-line usage message.
46+
func (p *Command) Use(use string) {
47+
p.Command.Use = use
48+
}
49+
50+
// Short sets the short description shown in the 'help' output.
51+
func (p *Command) Short(s string) {
52+
p.Command.Short = s
53+
}
54+
55+
// Long sets the long message shown in the 'help <this-command>' output.
56+
func (p *Command) Long(l string) {
57+
p.Command.Long = l
58+
}
59+
60+
// Run sets the actual work function. Most commands will only implement this.
61+
func (p *Command) Run(fn func()) {
62+
p.Command.Run = func(cmd *cobra.Command, args []string) {
63+
fn()
64+
}
65+
}
66+
67+
// -----------------------------------------------------------------------------
68+
69+
// App is the project class of Cobra classfile.
70+
type App struct {
71+
Command
72+
}
73+
74+
func (p *App) initApp(projname string) *Command {
75+
p.Use(projname)
76+
return &p.Command
77+
}
78+
79+
type iAppProto interface {
80+
initApp(projname string) *Command
81+
Classprojname() string
82+
}
83+
84+
type iCommandProto interface {
85+
cobraCmd() *cobra.Command
86+
Main(fname string)
87+
Classfname() string
88+
}
89+
90+
// Gopt_App_Main is required by Go+ compiler as the entry of a Cobra project.
91+
func Gopt_App_Main(app iAppProto, cmds ...iCommandProto) {
92+
root := app.initApp(app.Classprojname())
93+
for _, cmd := range cmds {
94+
reflect.ValueOf(cmd).Elem().Field(1).Set(reflect.ValueOf(app)) // (*command).App = app
95+
cmd.Main(cmd.Classfname())
96+
root.AddCommand(cmd.cobraCmd())
97+
}
98+
root.Execute()
99+
}
100+
101+
// -----------------------------------------------------------------------------

demo/godemo/gop_autogen.go

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/godemo/main_cmd.gox

Whitespace-only changes.

demo/godemo/version_cmd.gox

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
short "print Go version"
2+
3+
long `Version prints the build information for Go binary files.
4+
5+
Go version reports the Go version used to build each of the named files.
6+
7+
If no files are named on the command line, go version prints its own
8+
version information.
9+
10+
If a directory is named, go version walks that directory, recursively,
11+
looking for recognized Go binaries and reporting their versions.
12+
By default, go version does not report unrecognized files found
13+
during a directory scan. The -v flag causes it to report unrecognized files.
14+
15+
The -m flag causes go version to print each file's embedded
16+
module version information, when available. In the output, the module
17+
information consists of multiple lines following the version line, each
18+
indented by a leading tab character.
19+
20+
See also: go doc runtime/debug.BuildInfo.
21+
`
22+
23+
run => {
24+
echo "go1.0"
25+
}

go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/goplus/cobra
2+
3+
go 1.19
4+
5+
require github.com/spf13/cobra v1.9.1
6+
7+
require (
8+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
9+
github.com/spf13/pflag v1.0.6 // indirect
10+
)

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
2+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
6+
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
7+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
8+
github.com/spf13/pflag v1.0.6/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=

gop.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gop 1.2
2+
3+
project _cmd.gox App github.com/goplus/cobra
4+
5+
class _cmd.gox Command

0 commit comments

Comments
 (0)