Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: flag: option to accept abbreviations of flags #53133

Closed
KarelKubat opened this issue May 29, 2022 · 1 comment
Closed

proposal: flag: option to accept abbreviations of flags #53133

KarelKubat opened this issue May 29, 2022 · 1 comment

Comments

@KarelKubat
Copy link

gopls version

Build info

golang.org/x/tools/gopls v0.8.4
golang.org/x/tools/gopls@v0.8.4 h1:zGZsAXAb0LLws/Z+2BCWR17dkPHhIO2GYwYSSkeXX5c=
github.com/BurntSushi/toml@v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/google/go-cmp@v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
golang.org/x/exp/typeparams@v0.0.0-20220218215828-6cf2b201936e h1:qyrTQ++p1afMkO4DPEeLGq/3oTsdlvdH4vqZUBWzUKM=
golang.org/x/mod@v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sys@v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/text@v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/tools@v0.1.11-0.20220513164230-dfee1649af67 h1:CJwk4qG1fov4WP7/DWhhb7OQVZlQKAl1rEMnDF+ceGU=
golang.org/x/vuln@v0.0.0-20220503210553-a5481fb0c8be h1:jokAF1mfylAi1iTQx7C44B7vyXUcSEMw8eDv0PzNu8s=
honnef.co/go/tools@v0.3.0 h1:2LdYUZ7CIxnYgskbUZfY7FPggmqnh6shBqfWa8Tn3XU=
mvdan.cc/gofumpt@v0.3.0 h1:kTojdZo9AcEYbQYhGuLf/zszYthRdhDNDUi2JKTxas4=
mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.18

go env

GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/karelk/Library/Caches/go-build"
GOENV="/Users/karelk/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/karelk/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/karelk/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/local/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/local/lib/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18.2"
GCCGO="gccgo"
AR="ar"
CC="/usr/bin/clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/c8/27sy8g9d47ngtrxhb_x34nkw00_w91/T/go-build943201535=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

  • Defining flags using the flag package often leads to long flag names.

What did you expect to see?

  • The flag package should automatically recognize shorthands where possible.
  • Example: if a flag.FlagSet (which may be the global flag.CommandLine) holds the flags: verbose and length then automagically an invocation with -v and -l should work.
    • The package should resolve -v to the defined flag verbose and -l to the length.
    • Arbitrary lengths should be possible: -ver and -len should also resolve to verbose and length.
    • Only when a name can't be uniquely resolved to an existing flag, should an error be raised.
  • For ambiguous names the flags should be shortened until they are unique. E.g., defined flags item and id couldn't be abbreviated to -i, but instead to -it and -id.

What did you see instead?

The standard way that package flag works is that upon invocation, an error occurs that flag -i is given but not defined. It would be better if the package tried to resolve the flag in the set of defined names.

Implementation thoughts

I wrote a functional mockup at https://github.com/KarelKubat/flagnames. This has the drawback that the arguments that are passed into flag.Parse() are patched from abbreviated forms into whatever flags are defined. That works fine, but isn't too elegant.

A better approach would be to:

  • Define a bool acceptAbbrevs in the FlagSet structure, defaulting to false to keep parity with the existing way,
  • Provide accessors (per-FlagSet and global), e.g. flag.AcceptAbbrevs()
  • When the bool is set, resolve provided flags against defined names while allowing abbreviations
  • Only issue an error when a provided flag:
    • Doesn't occur as a prefix in any defined flag name
    • Could resolve to multiple defined flags

A hypothetical code sample:

var verboseFlag = flag.Bool("verbose", false, "increases verbosity")
flag.AcceptAbbrevs().  // allow -v, -ve, -ver, -verb etc.
flag.Parse()

My Question

Your thoughts?

  • I'm happy to propose this change to package flag.
  • But if you think it's not worth while then please let me know and I'll happily continue using github.com/KarelKubat/flagnames to scratch my itch.
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels May 29, 2022
@gopherbot gopherbot added this to the Unreleased milestone May 29, 2022
@seankhliao seankhliao removed gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels May 29, 2022
@seankhliao seankhliao changed the title x/tools/gopls: Make package flag accept abbreviations for flag names proposal: flag: option to accept abbreviations of flags May 29, 2022
@seankhliao
Copy link
Member

Duplicate of #40138

@seankhliao seankhliao marked this as a duplicate of #40138 May 29, 2022
@golang golang locked and limited conversation to collaborators May 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants