From 3d82d9df9c0fe9a5f03ec7fb4b6991b5deb70e3d Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Sat, 30 Sep 2023 10:12:13 -0400 Subject: [PATCH 1/4] Add some simple command line completion --- go.mod | 6 ++++++ go.sum | 16 ++++++++++++++++ main.go | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/go.mod b/go.mod index da9cb42..225dd2f 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,12 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/hashicorp/errwrap v1.0.0 // indirect + github.com/hashicorp/go-multierror v1.0.0 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/posener/complete v1.2.3 // indirect + github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab // indirect + github.com/willabides/kongplete v0.3.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index bad7c46..df3496a 100644 --- a/go.sum +++ b/go.sum @@ -2,18 +2,34 @@ github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2o github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4= github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U= github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo= +github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= +github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab h1:ZjX6I48eZSFetPb41dHudEyVr5v953N15TsNZXlkcWY= +github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab/go.mod h1:/PfPXh0EntGc3QAAyUaviy4S9tzy4Zp0e2ilq4voC6E= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/willabides/kongplete v0.3.0 h1:8dJZ0r2a2YnSdYCQk9TjQDKzLrj1zUvIOPIG3bOV75c= +github.com/willabides/kongplete v0.3.0/go.mod h1:VPdrG6LY+tP0LMkSBuTgIQ8c6+P8wvIDHVJzDdDh9Fw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index f9a8066..e4336c5 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "github.com/alecthomas/kong" "github.com/oalders/is/types" + "github.com/posener/complete" + "github.com/willabides/kongplete" ) func main() { @@ -21,6 +23,17 @@ func main() { Version kong.VersionFlag `help:"Print version to screen"` } + parser := kong.Must(&API, + kong.Name("is"), + kong.Description("A shell-like example app."), + kong.UsageOnError(), + ) + + // Run kongplete.Complete to handle completion requests + kongplete.Complete(parser, + kongplete.WithPredictor("file", complete.PredictFiles("*")), + ) + ctx := kong.Parse(&API, kong.Vars{ "version": "0.5.0", From 89431f3e1e32364ee410f2723f440354df23c228 Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Thu, 13 Jun 2024 19:13:00 -0400 Subject: [PATCH 2/4] Add install-completions command to the API --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index e4336c5..f3d3c3f 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,8 @@ func main() { There ThereCmd `cmd:"" help:"Check if command exists. e.g. \"is there git\""` User UserCmd `cmd:"" help:"Info about current user. e.g. \"is user sudoer\""` Version kong.VersionFlag `help:"Print version to screen"` + + InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions"` } parser := kong.Must(&API, From f187810df3364290bd8248004884453a14c5bdc5 Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Tue, 18 Jun 2024 09:30:07 -0400 Subject: [PATCH 3/4] Expand help text for install-completions --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index f3d3c3f..b18d361 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ func main() { User UserCmd `cmd:"" help:"Info about current user. e.g. \"is user sudoer\""` Version kong.VersionFlag `help:"Print version to screen"` - InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions"` + InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions. e.g. \"is install-completions\" and then run the command which is printed to your terminal"` } parser := kong.Must(&API, From 2ff7fad52f4f830241774584370b361e26c4cb51 Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Tue, 18 Jun 2024 09:52:44 -0400 Subject: [PATCH 4/4] Show usage on error --- main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index b18d361..747913e 100644 --- a/main.go +++ b/main.go @@ -22,13 +22,14 @@ func main() { User UserCmd `cmd:"" help:"Info about current user. e.g. \"is user sudoer\""` Version kong.VersionFlag `help:"Print version to screen"` - InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions. e.g. \"is install-completions\" and then run the command which is printed to your terminal"` + InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions. e.g. \"is install-completions\" and then run the command which is printed to your terminal"` //nolint:lll } parser := kong.Must(&API, kong.Name("is"), kong.Description("A shell-like example app."), kong.UsageOnError(), + kong.Vars{"version": "0.5.0"}, ) // Run kongplete.Complete to handle completion requests @@ -36,12 +37,11 @@ func main() { kongplete.WithPredictor("file", complete.PredictFiles("*")), ) - ctx := kong.Parse(&API, - kong.Vars{ - "version": "0.5.0", - }) + ctx, err := parser.Parse(os.Args[1:]) + parser.FatalIfErrorf(err) + runContext := types.Context{Debug: API.Debug} - err := ctx.Run(&runContext) + err = ctx.Run(&runContext) ctx.FatalIfErrorf(err) if runContext.Success {