Skip to content

Commit

Permalink
cmd/wit-bindgen-go: reserve -v flag as alias for --verbose; add versi…
Browse files Browse the repository at this point in the history
…on subcommand

'wit-bindgen-go version' now equivalent to 'wit-bindgen-go --version'
  • Loading branch information
ydnar committed Oct 30, 2024
1 parent f0d683f commit be437e2
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions cmd/wit-bindgen-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,58 @@ import (
"context"
"fmt"
"os"
"runtime/debug"

"github.com/urfave/cli/v3"

"github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go/cmd/generate"
"github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go/cmd/wit"
"github.com/bytecodealliance/wasm-tools-go/internal/witcli"
)

var (
version = ""
revision = ""
versionString = ""
)

func init() {
build, ok := debug.ReadBuildInfo()
if !ok {
return
}
version = build.Main.Version
for _, s := range build.Settings {
switch s.Key {
case "vcs.revision":
revision = s.Value
}
}
if version == "" {
version = "(none)"
}
versionString = version
if revision != "" {
versionString += " (" + revision + ")"
func main() {
err := Command.Run(context.Background(), os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

func main() {
cmd := &cli.Command{
Name: "wit-bindgen-go",
Usage: "inspect or manipulate WebAssembly Interface Types for Go",
Commands: []*cli.Command{
generate.Command,
wit.Command,
var Command = &cli.Command{
Name: "wit-bindgen-go",
Usage: "inspect or manipulate WebAssembly Interface Types for Go",
Commands: []*cli.Command{
generate.Command,
wit.Command,
version,
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force-wit",
Usage: "force loading WIT via wasm-tools",
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force-wit",
Usage: "force loading WIT via wasm-tools",
},
&cli.BoolFlag{
Name: "version",
Usage: "print the version",
HideDefault: true,
},
Version: versionString,
}
},
Version: witcli.Version(),
HideVersion: true,
Action: action,
}

err := cmd.Run(context.Background(), os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
func action(ctx context.Context, cmd *cli.Command) error {
if cmd.Bool("version") {
return version.Run(ctx, nil)
}
return cli.ShowAppHelp(cmd)
}

var version = &cli.Command{
Name: "version",
Usage: "print the version",
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Printf("%s version %s\n", cmd.Root().Name, witcli.Version())
return nil
},
}

0 comments on commit be437e2

Please sign in to comment.