-
Notifications
You must be signed in to change notification settings - Fork 358
Teach pflag to handle unambiguous abbreviated long-args #372
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
base: master
Are you sure you want to change the base?
Conversation
opencoff
commented
Dec 30, 2022
- add a new abbreviation generator (abbrev.go) with tests
- teach flag.go to build an abbrev table for long args and look it up and add tests to verify new behavior
- Fix "spurious new line" error from use of Println()
* add a new abbreviation generator (abbrev.go) with tests * teach flag.go to build an abbrev table for long args and look it up and add tests to verify new behavior * Fix "spurious new line" error from use of Println()
@@ -7,7 +7,7 @@ package pflag_test | |||
import ( | |||
"fmt" | |||
|
|||
"github.com/spf13/pflag" | |||
"github.com/opencoff/pflag" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems to be a left behind debug
@@ -16,7 +16,7 @@ pflag is a drop-in replacement of Go's native flag package. If you import | |||
pflag under the name "flag" then all code should continue to function | |||
with no changes. | |||
|
|||
import flag "github.com/spf13/pflag" | |||
import flag "github.com/opencoff/pflag" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
@@ -1314,7 +1314,7 @@ func TestPrintDefaults(t *testing.T) { | |||
func TestVisitAllFlagOrder(t *testing.T) { | |||
fs := NewFlagSet("TestVisitAllFlagOrder", ContinueOnError) | |||
fs.SortFlags = false | |||
// https://github.com/spf13/pflag/issues/120 | |||
// https://github.com/opencoff/pflag/issues/120 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
@@ -1,3 +1,3 @@ | |||
module github.com/spf13/pflag | |||
module github.com/opencoff/pflag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
} | ||
|
||
// make an assert() function for use in environment 't' and return it | ||
func newAsserter(t *testing.T) func(cond bool, msg string, args ...interface{}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Helper is missing here
func newAsserter(t *testing.T) func(cond bool, msg string, args ...interface{}) { | |
func newAsserter(t *testing.T) func(cond bool, msg string, args ...interface{}) { | |
t.Helper() | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks (mostly, but not entirely) ok, given that line comments are addressed appropriately.
I'm not convinced the feature is a good idea, though.
My main reason to hesitate is that this is makes it a lot easier for users of pflag to, unintendedly, introduce changes in their CLI contracts that are breaking to their users. Most obviously, adding a new flag that shares a prefix with an existing one, will break all scripts (or shell history) that use an abbreviation which is no longer unambiguous.
One could argue that it's not pflag's responsibility to ensure our users don't break their users, but having this feature as is - enabled and non-optional - effectively makes it harder for our users not to. I'm not convinced of the value of this feature either way, but I think it needs to, at the very least, be behind a feature flag and turned off by default.
I don't know if pflag already has a mechanism for users to toggle features on or off. If there is no such mechanism, building one is probably a good scope for a separate PR independent of this one.
// abbrev.go - generate abbreviations from a wordlist | ||
// | ||
// (c) 2014 Sudhi Herle <sw-at-herle.net> | ||
// | ||
// Placed in the Public Domain | ||
// This software does not come with any express or implied | ||
// warranty; it is provided "as is". No claim is made to its | ||
// suitability for any purpose. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this code copied from somewhere else? If so where?
Might just be me, but I also find the combination of a copyright header and "added to the public domain" confusing. Is it copyright protected or not? What are the expectations on anyone finding this piece of code and wanting to use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original inspiration came from perl's abbrev.pm; IIRC I picked the same terms as that module.
// Abbrev() returns: | ||
// { | ||
// "hello": "hello", | ||
// "hell": "hell" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should read "hell": "hello",
?
case 2: | ||
delete(table, ab) | ||
default: | ||
goto next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. No fan of this. Why not use a (possibly labeled) break
?
|
||
go 1.12 | ||
go 1.24.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is, if intended, a (significantly) breaking change. Is it necessary?
Your points re defaulting to "on" leading to breakages is valid. Perhaps there is no point in pursuing this PR. |