You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
The text was updated successfully, but these errors were encountered:
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
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
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
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?
flag
package often leads to long flag names.What did you expect to see?
flag
package should automatically recognize shorthands where possible.flag.FlagSet
(which may be the globalflag.CommandLine
) holds the flags:verbose
andlength
then automagically an invocation with-v
and-l
should work.-v
to the defined flagverbose
and-l
to thelength
.-ver
and-len
should also resolve toverbose
andlength
.item
andid
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 thatflag -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:
bool acceptAbbrevs
in theFlagSet
structure, defaulting tofalse
to keep parity with the existing way,FlagSet
and global), e.g.flag.AcceptAbbrevs()
bool
is set, resolve provided flags against defined names while allowing abbreviationsA hypothetical code sample:
My Question
Your thoughts?
flag
.github.com/KarelKubat/flagnames
to scratch my itch.The text was updated successfully, but these errors were encountered: