summaryrefslogtreecommitdiff
path: root/src/flag
Commit message (Collapse)AuthorAgeFilesLines
* flag: panic if a flag is defined after being setKeith Randall2023-04-212-3/+41
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As part of developing #57411, we ran into cases where a flag was defined in one package init and Set in another package init, and there was no init ordering implied by the spec between those two packages. Changes in initialization ordering as part of #57411 caused a Set to happen before the definition, which makes the Set silently fail. This CL makes the Set fail loudly in that situation. Currently Set *does* fail kinda quietly in that situation, in that it returns an error. (It seems that no one checks the error from Set, at least for string flags.) Ian suggsted that instead we panic at the definition site if there was previously a Set called on that (at the time undefined) flag. So Set on an undefined flag is ok and returns an error (as before), but defining a flag which has already been Set causes a panic. (The API for flag definition has no way to return an error, and does already panic in some situations like a duplicate definition.) Update #57411 Change-Id: I39b5a49006f9469de0b7f3fe092afe3a352e4fcb Reviewed-on: https://go-review.googlesource.com/c/go/+/480215 Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Keith Randall <khr@google.com>
* flag: add BoolFunc; FlagSet.BoolFuncCarl Johnson2023-03-163-2/+88
| | | | | | | | | | | | | | | | | | Fixes #53747 Based on CL 416514 Change-Id: I1ff79c6290b06dfa8672a473045e8fe80c22afcf GitHub-Last-Rev: 74fba9b3096487c04c8dc1f2237f67f3558212f1 GitHub-Pull-Request: golang/go#59013 Reviewed-on: https://go-review.googlesource.com/c/go/+/476015 Run-TryBot: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
* flag: clarify that the main func at pkg.go.dev is part of a testing suiteZeke Lu2022-10-261-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | flag.Example() has this comment: ... one must execute, typically at the start of main (not init!): flag.Parse() We don't run it here because this is not a main function This example function will be renamed to "main" at pkg.go.dev, which makes the comment confusing. See https://pkg.go.dev/flag#example-package. This change modify the comment to clarify this situation. Change-Id: I17357fdaaefe54791fff8fbbf6a33003af207f88 GitHub-Last-Rev: eeea8ce39cda3321d51c6cfe29fbcb2444fbf9cd GitHub-Pull-Request: golang/go#56411 Reviewed-on: https://go-review.googlesource.com/c/go/+/445315 Run-TryBot: Ian Lance Taylor <iant@google.com> Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
* flag: remove "// BUG" commentZamicol2022-10-171-3/+3
| | | | | | | | | | | | | | | Remove a vestigial " // BUG" comment as there is no bug in the relevant code section and comment predated other changes. Also removed a needless allocation and conformed to the "v, ok := a[x]" standard convention. Tests are passing. Change-Id: Id28ad1baf77447052b54b341f018e573bac0c11a GitHub-Last-Rev: 26084698bfc0972d7e0fec0de4f31cc87e3a5f7a GitHub-Pull-Request: golang/go#56210 Reviewed-on: https://go-review.googlesource.com/c/go/+/442815 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
* flag: test IsBoolFlag when creating the usage messageRob Pike2022-09-192-2/+30
| | | | | | | | | | | | | | | | | | | | | | Although I can't think of any reason to do this, it is possible for a user-defined flag to implement IsBoolFlag but return "false". This is nuts because checking the interface is satisfied should obviously be sufficient, but the documentation kinda implies it's not. And if you try this, you'll discover that the usage message ignores the return value even though the rest of the package plays nice. Bother. So we fix it, as the fix is trivial: call the method when creating the usage message. Fixes #53473 Change-Id: I1ac80a876ad5626eebfc5ef6cb972cd3007afaad Reviewed-on: https://go-review.googlesource.com/c/go/+/431102 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
* flag: use strings.Buildercuiweixie2022-09-061-6/+5
| | | | | | | | | | Change-Id: Iee846c4ac0f111ff97aa618dd42f6b2d14aa4342 Reviewed-on: https://go-review.googlesource.com/c/go/+/428259 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
* flag: highlight support for double dashes in docsSebastian Gassner2022-07-011-1/+2
| | | | | | | | | | | | | | | Updating examples, to show that double dashes are also permitted. This has been easy to miss previously. Change-Id: Ib67b4e39fea90ef4cb9e894709c53baedfc18fc2 GitHub-Last-Rev: f7df57b646d6412c1346e85c3a7353a8df41afc6 GitHub-Pull-Request: golang/go#53628 Reviewed-on: https://go-review.googlesource.com/c/go/+/415374 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com>
* all: gofmt main repoRuss Cox2022-04-111-2/+21
| | | | | | | | | | | | | | | [This CL is part of a sequence implementing the proposal #51082. The design doc is at https://go.dev/s/godocfmt-design.] Run the updated gofmt, which reformats doc comments, on the main repository. Vendored files are excluded. For #51082. Change-Id: I7332f099b60f716295fb34719c98c04eb1a85407 Reviewed-on: https://go-review.googlesource.com/c/go/+/384268 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* flag: recover panic when calling String on zero value in PrintDefaultsAndrew Gerrand2022-04-012-4/+60
| | | | | | | | | | | | | | | | | | When printing the usage message, recover panics when calling String methods on reflect-constructed flag.Value zero values. Collect the panic messages and include them at the end of the PrintDefaults output so that the programmer knows to fix the panic. Fixes #28667 Change-Id: Ic4378a5813a2e26f063d5580d678add65ece8f97 Reviewed-on: https://go-review.googlesource.com/c/go/+/396574 Run-TryBot: Andrew Gerrand <adg@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org> Trust: Andrew Gerrand <adg@golang.org>
* all: untab /* */ doc commentsRuss Cox2022-03-151-58/+58
| | | | | | | | | | | | | | | | | | | | | | | | | | | A long time ago, gofmt insisted on inserting tabs in /* */ comments at the top level of the file, like this: /* Package doc comment. */ package p Gofmt still insists on the tab for comments not at top level, but it has relaxed the rules about top-level comments. A few very old doc comments are indented, left over from the old rule. We are considering formatting doc comments, and so to make everything consistent, standardize on unindented doc comments by removing tabs in the few doc comments that are still indented this way. Also update some cmd/gofmt testdata to match. Change-Id: I293742e39b52f8a48ec41f72ca4acdafa7ce43bc Reviewed-on: https://go-review.googlesource.com/c/go/+/384261 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* flag: add TextVar functionJoe Tsai2022-03-102-0/+91
| | | | | | | | | | | | | | The TextVar function makes it easier to integrate the flag package with any Go type that implements encoding.Text{Marshaler,Unmarshaler}. Fixes #45754 Change-Id: Id23c37d59cf8c9699a7943a22ce27a45eb685c0f Reviewed-on: https://go-review.googlesource.com/c/go/+/313329 Trust: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
* flag: make tests silentDaniel Martí2022-03-072-4/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A few of the tests were printing garbage to stderr, since FlagSet's default Output is os.Stderr: $ go test flag provided but not defined: -x invalid value "1" for flag -v: test error Usage of test: flag needs an argument: -b Usage of test: -b usage PASS ok flag 0.008s Add the remaining SetOutput(io.Discard) method calls. Note that TestUserDefinedFunc was a tricky one. Even with the added SetOutput calls, the last part of the test would still print usage text to stderr. It took me a while to figure out the problem was copying FlagSet. I've filed go.dev/issue/51507 to record this particular sharp edge, and the test code now avoids making FlagSet copies to avoid the bug. Change-Id: I323f24091b98386312aa72df3eb890af6625628d Reviewed-on: https://go-review.googlesource.com/c/go/+/390234 Trust: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: gofmt -w -r 'interface{} -> any' srcRuss Cox2021-12-131-11/+11
| | | | | | | | | | | | | | | | | | | | | | | | And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
* all: use reflect.{Pointer,PointerTo}Cuong Manh Le2021-10-261-1/+1
| | | | | | | | | | | | Updates #47651 Updates #48665 Change-Id: I69a87b45a5cad7a07fbd855040cd9935cf874554 Reviewed-on: https://go-review.googlesource.com/c/go/+/358454 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* flag: use strings.Builder instead of concatenating stringsJames Fennell2021-04-051-9/+11
| | | | | | | | | | | | | | | | | | | There is a single function in the flag package whose implementation uses string concatenation instead of the recommended strings.Builder. The function was last touched before strings.Builder was introduced in Go 1.10, which explains the old style code. This PR updates the implementation. Fixes #45392 Change-Id: Id2d8f1788765a0c4faaeb1e6870914f72b3c8442 GitHub-Last-Rev: 0e12fe304593afc627fc4f1597670efd354809b0 GitHub-Pull-Request: golang/go#45393 Reviewed-on: https://go-review.googlesource.com/c/go/+/307329 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Josh Bleecher Snyder <josharian@gmail.com>
* flag: panic if flag name begins with - or contains =KimMachineGun2021-03-102-6/+101
| | | | | | | | | | | | | | Fixes #41792 Change-Id: I9b4aae8a899e3c3ac9532d27932d275cfb1fab48 GitHub-Last-Rev: f06b1e17674bf77bdc2d3e798df4c4379748c8d2 GitHub-Pull-Request: golang/go#42737 Reviewed-on: https://go-review.googlesource.com/c/go/+/271788 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Rob Pike <r@golang.org>
* all: update references to symbols moved from io/ioutil to ioRuss Cox2020-10-201-3/+2
| | | | | | | | | | | | | | | | | | | The old ioutil references are still valid, but update our code to reflect best practices and get used to the new locations. Code compiled with the bootstrap toolchain (cmd/asm, cmd/dist, cmd/compile, debug/elf) must remain Go 1.4-compatible and is excluded. Also excluded vendored code. For #41190. Change-Id: I6d86f2bf7bc37a9d904b6cee3fe0c7af6d94d5b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/263142 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
* flag: add FuncCarl Johnson2020-09-163-3/+110
| | | | | | | | | | | Fixes #39557 Change-Id: Ida578f7484335e8c6bf927255f75377eda63b563 GitHub-Last-Rev: b97294f7669c24011e5b093179d65636512a84cd GitHub-Pull-Request: golang/go#39880 Reviewed-on: https://go-review.googlesource.com/c/go/+/240014 Reviewed-by: Russ Cox <rsc@golang.org> Trust: Ian Lance Taylor <iant@golang.org>
* flag: fix TestExitCode on Plan 9David du Colombier2020-04-031-0/+5
| | | | | | | | | | | | | | | | | | CL 221427 added TestExitCode. This test is failing on Plan 9 because ExitCode is always equal to 1 on error since Plan 9 use error strings. This change fixes TestExitCode by checking that ExitCode is equal to 1 on error instead of the specific value. Fixes #38237. Change-Id: Ie269722e731e275e5bfc51644c1fa6be76525f1f Reviewed-on: https://go-review.googlesource.com/c/go/+/227158 Run-TryBot: David du Colombier <0intro@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
* flag: exit 0 when -h or -help invoked but undefinedShang Jian Ding2020-04-032-1/+65
| | | | | | | | | | | | | | | | flag treats -h or -help as a special case to print a nice help message, but exit with a status code of 2. This update makes that status code 0. Fixes #37533 Change-Id: I7e0bd29944ce46607fb7cfc6740734f7444a151a GitHub-Last-Rev: 83f64d757bc3a9957c49caa5de74d05a96724771 GitHub-Pull-Request: golang/go#37530 Reviewed-on: https://go-review.googlesource.com/c/go/+/221427 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* flag: changed flag variable name in package doc, for clarityyuz2020-03-031-2/+2
| | | | | | | | | Changed the flag variable name to nFlag instead of flagname, because flagname was confusing. Change-Id: I20dd4c4b4f605395d427a125ba4fd14580e5d766 Reviewed-on: https://go-review.googlesource.com/c/go/+/221678 Reviewed-by: Rob Pike <r@golang.org>
* flag: update comment to refer to Output, not outIan Lance Taylor2020-02-271-1/+1
| | | | | | | | | | | | The out method was renamed to Output in CL 70391 for #17628 and #21888. Fixes #37514 Change-Id: I99be47b5030ccbbf10a056df9fcc3c97cb99b015 Reviewed-on: https://go-review.googlesource.com/c/go/+/221383 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* flag: clarify that a flag cannot be re-definedAgniva De Sarker2019-11-111-0/+3
| | | | | | | | Fixes #31694 Change-Id: Ifb2ad2dc41c449668c0f6a4d4cfb9b583e5591f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/206126 Reviewed-by: Rob Pike <r@golang.org>
* cmd/go: further reduce init workDaniel Martí2019-03-311-7/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The first biggest offender was crypto/des.init at ~1%. It's cryptographically broken and the init function is relatively expensive, which is unfortunate as both crypto/tls and crypto/x509 (and by extension, cmd/go) import it. Hide the work behind sync.Once. The second biggest offender was flag.sortFlags at just under 1%, used by the Visit flagset methods. It allocated two slices, which made a difference as cmd/go iterates over multiple flagsets during init. Use a single slice with a direct sort.Interface implementation. Another big offender is initializing global maps. Reducing this work in cmd/go/internal/imports and net/textproto gives us close to another whole 1% in saved work. The former can use map literals, and the latter can hide the work behind sync.Once. Finally, compress/flate used newHuffmanBitWriter as part of init, which allocates many objects and slices. Yet it only used one of the slice fields. Allocating just that slice saves a surprising ~0.3%, since we generated a lot of unnecessary garbage. All in all, these little pieces amount to just over 3% saved CPU time. name old time/op new time/op delta ExecGoEnv-8 3.61ms ± 1% 3.50ms ± 0% -3.02% (p=0.000 n=10+10) Updates #26775. Updates #29382. Change-Id: I915416e88a874c63235ba512617c8aef35c0ca8b Reviewed-on: https://go-review.googlesource.com/c/go/+/166459 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* flag: improve docs for PrintDefaults; clarify how to change output destinationAaron Cannon2019-01-211-0/+2
| | | | | | | | | | | | | The existing docs only mention that it is possible to change the output destination of PrintDefaults from the default of standard error, but fail to mention how to actually do so. This change fixes this lack by directing users to CommandLine.SetOutput. Fixes #15024 Change-Id: Ieaa7edbebd23d4ea6fa7e53d97a87143d590bdb3 Reviewed-on: https://go-review.googlesource.com/c/145203 Reviewed-by: Rob Pike <r@golang.org>
* flag: return a consistent parse error if the flag value is invalidRob Pike2018-10-192-0/+96
| | | | | | | | | | | | | | | | | Return a consistently formatted error string that reports either a parse error or a range error. Before: invalid boolean value "3" for -debug: strconv.ParseBool: parsing "3": invalid syntax After: invalid boolean value "3" for -debug: parse error Fixes #26822 Change-Id: I60992bf23da32a4c0cf32472a8af486a3c9674ad Reviewed-on: https://go-review.googlesource.com/c/143257 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: use strings.ReplaceAll and bytes.ReplaceAll where applicableBrad Fitzpatrick2018-09-261-1/+1
| | | | | | | | | | | | | I omitted vendor directories and anything necessary for bootstrapping. (Tested by bootstrapping with Go 1.4) Updates #27864 Change-Id: I7d9b68d0372d3a34dee22966cca323513ece7e8a Reviewed-on: https://go-review.googlesource.com/137856 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* flag: add a Value exampleTerin Stock2018-06-211-0/+44
| | | | | | | | Change-Id: I579cc9f4f8e5be5fd6447a99614797ab7bc53611 Reviewed-on: https://go-review.googlesource.com/120175 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* flag: fix headers in documentation, againDominik Honnef2018-06-141-0/+3
| | | | | | | | Godoc expects text after a header, not code. Change-Id: I99f412ad15e10bf9cea0dbd19019ed6ce477736c Reviewed-on: https://go-review.googlesource.com/117595 Reviewed-by: Rob Pike <r@golang.org>
* flag: removed the colon after "Usage" in the documentationhellozee2018-06-071-2/+2
| | | | | | | | | | | | Removing the colon will make the "Usage" and "Command line flag syntax" in the docs, a header when interpreted by godoc. Fixes #25749 Change-Id: Ifc5572e171db1aaef9775b1d6c86091a8f2528fd GitHub-Last-Rev: 1b579734308cbcb96f17d945b9c7af70e259cb5b GitHub-Pull-Request: golang/go#25750 Reviewed-on: https://go-review.googlesource.com/116555 Reviewed-by: Rob Pike <r@golang.org>
* flag: correct zero values when printing defaultsjimmyfrasche2018-04-012-11/+6
| | | | | | | | | | | | | | | | | | | | When the flag package first begin printing nonzero defaults, the test was against a fixed set of string representations of zero values. This worked until the string representation of a time.Duration changed from "0" to "0s", causing the zero Duration to register as nonzero. The flag package then added reflect-based code that fell back to the old test. This failed to work when a nonzero default for a flag happened to be the string representation of one the original fixed set of zero values in the original test. This change removes the original test, allowing the reflect-based code to be the only deciding factor. Fixes #23543 Change-Id: I582ce554d6729e336fdd96fb27340674c15350d8 Reviewed-on: https://go-review.googlesource.com/103867 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* flag: document use of FlagSet name parameterIan Lance Taylor2018-03-281-1/+2
| | | | | | | | Fixes #24225 Change-Id: I876ac1b9d3615283f7b88cfa0b965ef81a57f056 Reviewed-on: https://go-review.googlesource.com/102955 Reviewed-by: Rob Pike <r@golang.org>
* flag: clarify comment to avoid shell syntax confusionIan Lance Taylor2017-12-061-2/+3
| | | | | | | | | Updates #22961 Change-Id: Ib2f41aefb4f6470598d8637611da5491156ea840 Reviewed-on: https://go-review.googlesource.com/82015 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* flag: add (*FlagSet).Name, (*FlagSet).ErrorHandling, export (*FlagSet).OutputTim Cooper2017-10-312-7/+53
| | | | | | | | | | | | | Allows code that operates on a FlagSet to know the name and error handling behavior of the FlagSet without having to call FlagSet.Init. Fixes #17628 Fixes #21888 Change-Id: Ib0fe4c8885f9ccdacf5a7fb761d5ecb23f3bb055 Reviewed-on: https://go-review.googlesource.com/70391 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* flag: simplify switch-case in isZeroValueGabriel Aszalos2017-10-171-5/+1
| | | | | | | | | | | Simplifies the switch statement in the isZeroValue function by merging the case branches. Change-Id: I5b27939b62808dadac0cef632567b17e0e2b9a1d Reviewed-on: https://go-review.googlesource.com/71390 Run-TryBot: Gabriel Aszalos <gabriel.aszalos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* flag: align multi-line usage stringsTim Cooper2017-09-282-4/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously, a multi-line flag usage string would not be indented with the rest of the usage strings. This made long usage strings difficult to read. For example, the usage for flag.String("A", "", "1\n2\n3") would be printed as: -A 1 2 3 But will now be printed as: -A 1 2 3 Also fixes a slight error in the FlagSet.PrintDefaults documentation. Fixes #20799 Change-Id: I4379c6b7590fdb93a2809a01046a0f6ae32c3e5d Reviewed-on: https://go-review.googlesource.com/66711 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
* flag: simplify arg logic in parseOnealexpantyukhin2017-09-051-1/+1
| | | | | | | | | | | Fixes #21763. Change-Id: I59ee4f24c8064df64d9ede11aac02bc7ce4995b3 Reviewed-on: https://go-review.googlesource.com/61491 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
* flag: document that custom usage functions are free to call os.ExitRob Pike2017-08-291-1/+6
| | | | | | | | | | | | | Some custom usage functions call it for clarity; others rely on the default behavior, which makes an explicit call redundant. Document that it's safe to be explicit. Fixes #21671. Change-Id: I08e9f47265582821cfd35995dff0c589cd85809d Reviewed-on: https://go-review.googlesource.com/59792 Reviewed-by: Dominik Honnef <dominik@honnef.co> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: remove some double spaces from commentsDaniel Martí2017-08-261-4/+4
| | | | | | | | | | | Went mainly for the ones that make no sense, such as the ones mid-sentence or after commas. Change-Id: Ie245d2c19cc7428a06295635cf6a9482ade25ff0 Reviewed-on: https://go-review.googlesource.com/57293 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
* flag: make default Usage prefer CommandLine's output over just os.StderrBrad Fitzpatrick2017-08-083-2/+19
| | | | | | | | | | | | | | | | | CommandLine (exported in Go 1.2) has default output of os.Stderr. Before it was exported, it made sense to have the global Usage func (the implicit usage func if CommandLine.Usage is nil) hard-code os.Stderr has its output. But once CommandLine was exported, Usage should use it if provided. Fixes #20998 Change-Id: I9e1c0415a563a982634b9808199c9ee175d72f4c Reviewed-on: https://go-review.googlesource.com/48390 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* flag: validate Int and Uint values to be in rangeBrad Fitzpatrick2017-03-212-2/+19
| | | | | | | | | | Fixes #19230 Change-Id: I38df9732b88f0328506e74f1a46f52adf47db1e5 Reviewed-on: https://go-review.googlesource.com/38419 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* flag: arrange for FlagSet.Usage to be non-nil by defaultRuss Cox2016-10-262-6/+16
| | | | | | | | | | | | This allows callers to invoke f.Usage() themselves and get the default usage handler instead of a panic (from calling a nil function). Fixes #16955. Change-Id: Ie337fd9e1f85daf78c5eae7b5c41d5ad8c1f89bf Reviewed-on: https://go-review.googlesource.com/31576 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* flag: use strconv instead of fmt in values' String funcsbogem2016-09-101-7/+7
| | | | | | | | | | | | The existing implementation of flag values with fmt package uses more memory and works slower than the implementation with strconv package. Change-Id: I9e749179f66d5c50cafe98186641bcdbc546d2db Reviewed-on: https://go-review.googlesource.com/28914 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* flag: improve comment for calling String with zero valueIan Lance Taylor2016-08-231-1/+2
| | | | | | | | | | Update #16694. Change-Id: Id6be1535d8a146b3dac3bee429ce407a51272032 Reviewed-on: https://go-review.googlesource.com/27634 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* flag: document that Value.String must work on the zero valueIan Lance Taylor2016-08-231-0/+1
| | | | | | | | | | | | | Otherwise flag.PrintDefaults will fail when it tries to determine whether the default is the zero value. Fixes #16694. Change-Id: I253fbf11ffc0a9069fd48c2c3cf3074df53e3a03 Reviewed-on: https://go-review.googlesource.com/27003 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* flag: recognize "0s" as the zero value for a flag.DurationIan Lance Taylor2016-05-312-3/+18
| | | | | | | | | | | | | | Implemented by using a reflect-based approach to recognize the zero value of any non-interface type that implements flag.Value. Interface types will fall back to the old code. Fixes #15904. Change-Id: I594c3bfb30e9ab1aca3e008ef7f70be20aa41a0b Reviewed-on: https://go-review.googlesource.com/23581 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* flag: update test case (fix build)Robert Griesemer2016-04-211-1/+1
| | | | | | | Change-Id: I2275dc703be4fda3feedf76483148eab853b43b8 Reviewed-on: https://go-review.googlesource.com/22360 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* all: single space after period.Brad Fitzpatrick2016-03-021-6/+6
| | | | | | | | | | | | | | | | | | | | The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* all: make copyright headers consistent with one space after periodBrad Fitzpatrick2016-03-011-1/+1
| | | | | | | | | | | | | | | | | | | | | | This is a subset of https://golang.org/cl/20022 with only the copyright header lines, so the next CL will be smaller and more reviewable. Go policy has been single space after periods in comments for some time. The copyright header template at: https://golang.org/doc/contribute.html#copyright also uses a single space. Make them all consistent. Change-Id: Icc26c6b8495c3820da6b171ca96a74701b4a01b0 Reviewed-on: https://go-review.googlesource.com/20111 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* flag: add comment stating that Set is called in sequence across the command lineRob Pike2015-07-161-0/+2
| | | | | | | | No code changes. Change-Id: I3b78b1048318a4b80747fde8cab919282fc444a8 Reviewed-on: https://go-review.googlesource.com/12285 Reviewed-by: Andrew Gerrand <adg@golang.org>