diff --git a/CHANGELOG.md b/CHANGELOG.md index 76256c7..b4d4c4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +# [0.13.3] - 2025-06-26 + +### Changed + +- usage is now printed on errors. +- help is printed in compact mode. This should make it easier to page through help on the root command. + # [0.13.0] - 2025-06-23 ### Added diff --git a/main.go b/main.go index 6d0148b..e0e94b2 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "io" "os" "path/filepath" + "runtime/debug" + "strings" "time" "github.com/alecthomas/kong" @@ -16,6 +18,19 @@ import ( kongdotenv "github.com/titusjaka/kong-dotenv-go" ) +var version string // Version of the CLI, set at build time. + +// VersionFlag is a custom flag type that prints the version and exits. +type VersionFlag string + +func (v VersionFlag) Decode(_ *kong.DecodeContext) error { return nil } // nolint: revive +func (v VersionFlag) IsBool() bool { return true } // nolint: revive +func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error { // nolint: revive, unparam + fmt.Printf("gobs-cli version: %s\n", vars["version"]) + app.Exit(0) + return nil +} + // ObsConfig holds the configuration for connecting to the OBS WebSocket server. type ObsConfig struct { Host string `flag:"host" help:"Host to connect to." default:"localhost" env:"OBS_HOST" short:"H"` @@ -31,7 +46,7 @@ type StyleConfig struct { } // CLI is the main command line interface structure. -// It embeds the ObsConfig struct to inherit its fields and flags. +// It embeds ObsConfig and StyleConfig to provide configuration options. type CLI struct { ObsConfig `embed:"" help:"OBS WebSocket configuration."` StyleConfig `embed:"" help:"Style configuration."` @@ -81,10 +96,25 @@ func main() { var cli CLI ctx := kong.Parse( &cli, - kong.Name("GOBS-CLI"), + kong.Name("gobs-cli"), kong.Description("A command line tool to interact with OBS Websocket."), kong.Configuration(kongdotenv.ENVFileReader, ".env", filepath.Join(userConfigDir, "gobs-cli", "config.env")), - ) + kong.UsageOnError(), + kong.ConfigureHelp(kong.HelpOptions{ + Compact: true, + }), + kong.Vars{ + "version": func() string { + if version == "" { + info, ok := debug.ReadBuildInfo() + if !ok { + return "(unable to read build info)" + } + version = strings.Split(info.Main.Version, "-")[0] + } + return version + }(), + }) client, err := connectObs(cli.ObsConfig) ctx.FatalIfErrorf(err) diff --git a/version.go b/version.go index 480f5d5..ad9a80d 100644 --- a/version.go +++ b/version.go @@ -2,38 +2,8 @@ package main import ( "fmt" - "runtime/debug" - "strings" - - "github.com/alecthomas/kong" ) -var version string - -// VersionFlag is a custom flag type for displaying version information. -type VersionFlag string - -// Decode implements the kong.Flag interface. -func (v VersionFlag) Decode(_ *kong.DecodeContext) error { return nil } - -// IsBool implements the kong.Flag interface. -func (v VersionFlag) IsBool() bool { return true } - -// BeforeApply implements the kong.Flag interface. -func (v VersionFlag) BeforeApply(app *kong.Kong, _ kong.Vars) error { // nolint: unparam - if version == "" { - info, ok := debug.ReadBuildInfo() - if !ok { - return fmt.Errorf("failed to read build info") - } - version = strings.Split(info.Main.Version, "-")[0] - } - - fmt.Printf("gobs-cli version: %s\n", version) - app.Exit(0) // Exit the application after printing the version - return nil -} - // ObsVersionCmd handles the version command. type ObsVersionCmd struct{} // size = 0x0