Skip to content

Commit

Permalink
Merge pull request #82 from rsteube/shorthand-reflection
Browse files Browse the repository at this point in the history
Shorthand reflection
  • Loading branch information
rsteube authored Sep 15, 2020
2 parents 5f8fe1c + 2c2d875 commit fa44e5a
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
go-current:
docker:
- image: circleci/golang:1.12
environment:
GO111MODULE: "on"
working_directory: *workspace
steps:
- checkout
Expand All @@ -45,6 +47,8 @@ jobs:
go-previous:
docker:
- image: circleci/golang:1.11
environment:
GO111MODULE: "on"
working_directory: *workspace
steps:
- checkout
Expand Down
11 changes: 9 additions & 2 deletions bash/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/rsteube/carapace/common"
"github.com/rsteube/carapace/uid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -112,7 +113,9 @@ func snippetFlagList(flags *pflag.FlagSet) string {

flags.VisitAll(func(flag *pflag.Flag) {
if !flag.Hidden {
flagValues = append(flagValues, "--"+flag.Name)
if !common.IsShorthandOnly(flag) {
flagValues = append(flagValues, "--"+flag.Name)
}
if flag.Shorthand != "" {
flagValues = append(flagValues, "-"+flag.Shorthand)
}
Expand All @@ -132,7 +135,11 @@ func snippetFlagCompletion(flag *pflag.Flag, action string) (snippet string) {

var names string
if flag.Shorthand != "" {
names = fmt.Sprintf("-%v | --%v", flag.Shorthand, flag.Name)
if common.IsShorthandOnly(flag) {
names = fmt.Sprintf("-%v", flag.Shorthand)
} else {
names = fmt.Sprintf("-%v | --%v", flag.Shorthand, flag.Name)
}
} else {
names = "--" + flag.Name
}
Expand Down
20 changes: 20 additions & 0 deletions common/pflag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package common

import (
"reflect"

"github.com/spf13/pflag"
)

// uses reflection to check for pflag.Flag.Shorthandonly to support both spf13/pflag
// and cornfeedhobos shorthand change (needed for carapace-bin)
// won't be necessary if https://github.com/spf13/pflag/pull/256 should ever be merged
func IsShorthandOnly(flag *pflag.Flag) bool {
ValueIface := reflect.ValueOf(flag)
Field := ValueIface.Elem().FieldByName("ShorthandOnly")
if !Field.IsValid() {
return false
} else {
return Field.Bool()
}
}
10 changes: 7 additions & 3 deletions elvish/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/rsteube/carapace/common"
"github.com/rsteube/carapace/uid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -119,13 +120,16 @@ func snippetPositionalCompletion(action string) string {
}

func snippetFlagCompletion(flag *pflag.Flag, action string) (snippet string) {
spec := []string{
fmt.Sprintf(`&long='%v'`, flag.Name),
fmt.Sprintf(`&desc='%v'`, replacer.Replace(flag.Usage)),
spec := []string{}
if !common.IsShorthandOnly(flag) {
spec = append(spec, fmt.Sprintf(`&long='%v'`, flag.Name))
}
if flag.Shorthand != "" {
spec = append(spec, fmt.Sprintf(`&short='%v'`, flag.Shorthand))
}

spec = append(spec, fmt.Sprintf(`&desc='%v'`, replacer.Replace(flag.Usage)))

if flag.NoOptDefVal == "" {
spec = append(spec, `&arg-required=$true`, fmt.Sprintf(`&completer=[_]{ %v }`, action))
}
Expand Down
5 changes: 3 additions & 2 deletions example/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func init() {
actionCmd.Flags().StringP("values", "v", "", "values flag")
actionCmd.Flags().StringP("values_described", "d", "", "values with description flag")
actionCmd.Flags().StringP("custom", "c", "", "custom flag")
actionCmd.Flags().StringP("signal", "s", "", "kill signals")
//actionCmd.Flags().StringS("shorthandonly", "s", "", "shorthandonly flag")
actionCmd.Flags().StringP("kill", "k", "", "kill signals")

carapace.Gen(actionCmd).FlagCompletion(carapace.ActionMap{
"files": carapace.ActionFiles(".go"),
Expand All @@ -39,7 +40,7 @@ func init() {
"values": carapace.ActionValues("values", "example"),
"values_described": carapace.ActionValuesDescribed("values", "valueDescription", "example", "exampleDescription"),
"custom": carapace.Action{Zsh: "_most_recent_file 2"},
"signal": carapace.ActionKillSignals(),
"kill": carapace.ActionKillSignals(),
})

carapace.Gen(actionCmd).PositionalCompletion(
Expand Down
68 changes: 34 additions & 34 deletions example/cmd/root_test.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions fish/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/rsteube/carapace/common"
"github.com/rsteube/carapace/uid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -114,6 +115,8 @@ func snippetFlagCompletion(cmd *cobra.Command, flag *pflag.Flag, action *string)

if flag.Shorthand == "" { // no shorthannd
snippet = fmt.Sprintf(`complete -c '%v' -f -n '_%v_state %v' -l '%v' -d '%v'%v`, cmd.Root().Name(), cmd.Root().Name(), uid.Command(cmd), flag.Name, replacer.Replace(flag.Usage), suffix)
} else if common.IsShorthandOnly(flag) {
snippet = fmt.Sprintf(`complete -c '%v' -f -n '_%v_state %v' -s '%v' -d '%v'%v`, cmd.Root().Name(), cmd.Root().Name(), uid.Command(cmd), flag.Shorthand, replacer.Replace(flag.Usage), suffix)
} else {
snippet = fmt.Sprintf(`complete -c '%v' -f -n '_%v_state %v' -l '%v' -s '%v' -d '%v'%v`, cmd.Root().Name(), cmd.Root().Name(), uid.Command(cmd), flag.Name, flag.Shorthand, replacer.Replace(flag.Usage), suffix)
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cornfeedhobo/pflag v1.0.2-0.20200824165833-dd6f6588b61d h1:048MNx9ATpMoBgDkB8w8jQMBcK0xUR01ycNZ8Oo4674=
github.com/cornfeedhobo/pflag v1.0.2-0.20200824165833-dd6f6588b61d/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
Expand Down
7 changes: 6 additions & 1 deletion powershell/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"strings"

"github.com/rsteube/carapace/common"
"github.com/rsteube/carapace/uid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -83,6 +84,8 @@ func snippetFlagActions(cmd *cobra.Command, actions map[string]string) string {
match := fmt.Sprintf(`^(--%v)$`, flag.Name)
if flag.Shorthand != "" {
match = fmt.Sprintf(`^(-%v|--%v)$`, flag.Shorthand, flag.Name)
} else if common.IsShorthandOnly(flag) {
match = fmt.Sprintf(`^(-%v)$`, flag.Shorthand)
}
var action = ""
if a, ok := actions[uid.Flag(cmd, flag)]; ok { // TODO cleanup
Expand All @@ -106,7 +109,9 @@ func snippetTODO(cmd *cobra.Command) string {
if len(flag.Shorthand) > 0 {
result += fmt.Sprintf("\n [CompletionResult]::new('-%s ', '-%s', [CompletionResultType]::ParameterName, '%s')", flag.Shorthand, flag.Shorthand, usage)
}
result += fmt.Sprintf("\n [CompletionResult]::new('--%s ', '--%s', [CompletionResultType]::ParameterName, '%s')", flag.Name, flag.Name, usage)
if !common.IsShorthandOnly(flag) {
result += fmt.Sprintf("\n [CompletionResult]::new('--%s ', '--%s', [CompletionResultType]::ParameterName, '%s')", flag.Name, flag.Name, usage)
}
}
})

Expand Down
3 changes: 3 additions & 0 deletions zsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/rsteube/carapace/common"
"github.com/rsteube/carapace/uid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -112,6 +113,8 @@ func snippetFlagCompletion(flag *pflag.Flag, action *string) (snippet string) {

if flag.Shorthand == "" { // no shorthannd
snippet = fmt.Sprintf(`"%v--%v[%v]%v"`, multimark, flag.Name, replacer.Replace(flag.Usage), suffix)
} else if common.IsShorthandOnly(flag) {
snippet = fmt.Sprintf(`"%v-%v[%v]%v"`, multimark, flag.Shorthand, replacer.Replace(flag.Usage), suffix)
} else {
snippet = fmt.Sprintf(`"(%v-%v %v--%v)"{%v-%v,%v--%v}"[%v]%v"`, multimark, flag.Shorthand, multimark, flag.Name, multimarkEscaped, flag.Shorthand, multimarkEscaped, flag.Name, replacer.Replace(flag.Usage), suffix)
}
Expand Down

0 comments on commit fa44e5a

Please sign in to comment.