-
Notifications
You must be signed in to change notification settings - Fork 364
Add support for time.Time flags #348
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
Conversation
|
This is exactly what I am looking for. Would it be possible to review and merge this? Maybe also suggestion for small improvement, would it be possible to offer some reasonable standard default ordering? or this: |
|
I was thinking of adding a constant slice with a suggested default time format set, but opted to not include it in this PR since its easy to define such a constant in whatever code you use and more importantly I wanted to avoid any long winded discussions on what the actual order and included formats should be. |
AnthonyMBonafide
left a comment
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 looks great! Can't wait to get this out and into Cobra.
|
@johnSchnake tagging you (a bit late) for this PR as requested in spf13/cobra#742 |
This PR implements support for `time.Time` flags which can be used to accept timestamps as input directly (see spf13/cobra#742). The implementation allows defining a list of acceptable timestamp formats which are tested in order, i.e., devs using this flag type would be responsible for ordering their formats correctly should there be an overlap. Merge spf13#348
|
hate being that guy...but: any news on when will this be in? |
|
Yeah sadly seems that the repo is just sitting here now anymore nothing happening :/ i tried to make some noise in the related issue on the cobra side but nothing yet |
|
Anything needed from the community side to cross the finish line with this PR? Happy to test, review, anything... it is a bit of a shame that the de-facto standard CLI library doesn't have a |
|
Good to see this has been approved... Can it be merged? |
@Kapparina i wish it was this easy :( |
AnthonyMBonafide
left a comment
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 looks good to me.
|
Anyone knows who needs to be chased for the approved PR to be merged in this repo? Is it only @spf13 or do more people have merge rights? |
|
@tomasaschan Sorry for the direct ping, but going by recent activity you seem to have the ability to review and approve PRs. Any chance you'd have some time to look at this 🙇 |
time_test.go
Outdated
| devnull, _ := os.Open(os.DevNull) | ||
| os.Stderr = devnull |
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.
Why is this needed? What in this test would otherwise write to stderr?
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.
For this one going back by memory I believe only reason I've added it was because of copy paste from the test file I modified to make it easier to stick to the repo style.
That is one of these
- https://github.com/search?q=repo%3Aspf13%2Fpflag+%09os.Stderr+%3D+devnull&type=code
I've checked now and as far as I can tell non of the occurrences of this in the repo are doing anything.
Removed it for time_test.go in d15848d
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.
Thanks for the quick response to the review request 🙇
time_test.go
Outdated
| testCases := []struct { | ||
| input string | ||
| success bool | ||
| expected string |
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.
Nit: I think I'd prefer if this was a time.Time rather than a string, to make it easier to add other test cases without having to dig into how comparisons are done and what format to specify dates in. But I'm not going to block on that :)
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.
Updated in c5ce22e
7e4fa0a to
1992c5a
Compare
|
Note during rebase to master I had to rename |
|
What a coincidence, I stumbled upon this issue earlier today as I was interested in this feature and it seems like this is nearing completion! Fantastic! Thank you Frank for staying persistent on this PR. |
tomasaschan
left a comment
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.
A couple of more nits; I added suggestions for them so I can commit them with you as co-author and get this merged, I hope you don't mind :)
e8e90db to
e3be2eb
Compare
|
Thanks for your contribution! |
Yeah no worries I am happy to have this modified to fit the new direction 👍 Thanks for the quick support and review after the ping 🙇 |
|
The test flag set unfortunately produces a default value for the empty time struct, without option to get rid of it: I'd submit a follow-up PR with an |
This was missed in spf13#348, there previously was no way to omit the default value. Treating zero timestamp values as canary for absence of a default is in line with other flag types, e.g. zero ints.
This PR contains the following updates: | Package | Change | Age | Confidence | |---|---|---|---| | [github.com/spf13/pflag](https://github.com/spf13/pflag) | `v1.0.6` -> `v1.0.7` | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>spf13/pflag (github.com/spf13/pflag)</summary> ### [`v1.0.7`](https://github.com/spf13/pflag/releases/tag/v1.0.7) [Compare Source](spf13/pflag@v1.0.6...v1.0.7) #### What's Changed - Fix defaultIsZeroValue check for generic Value types by [@​MidnightRocket](https://github.com/MidnightRocket) in spf13/pflag#422 - feat: Use structs for errors returned by pflag. by [@​eth-p](https://github.com/eth-p) in spf13/pflag#425 - Fix typos by [@​co63oc](https://github.com/co63oc) in spf13/pflag#428 - fix [#​423](spf13/pflag#423) : Add helper function and some documentation to parse shorthand go test flags. by [@​valdar](https://github.com/valdar) in spf13/pflag#424 - add support equivalent to golang flag.TextVar(), also fixes the test failure as described in [#​368](spf13/pflag#368) by [@​hujun-open](https://github.com/hujun-open) in spf13/pflag#418 - add support for Func() and BoolFunc() [#​426](spf13/pflag#426) by [@​LeGEC](https://github.com/LeGEC) in spf13/pflag#429 - fix: correct argument length check in FlagSet.Parse by [@​ShawnJeffersonWang](https://github.com/ShawnJeffersonWang) in spf13/pflag#409 - fix usage message for func flags, fix arguments order by [@​LeGEC](https://github.com/LeGEC) in spf13/pflag#431 - Add support for time.Time flags by [@​max-frank](https://github.com/max-frank) in spf13/pflag#348 #### New Contributors - [@​MidnightRocket](https://github.com/MidnightRocket) made their first contribution in spf13/pflag#422 - [@​eth-p](https://github.com/eth-p) made their first contribution in spf13/pflag#425 - [@​co63oc](https://github.com/co63oc) made their first contribution in spf13/pflag#428 - [@​valdar](https://github.com/valdar) made their first contribution in spf13/pflag#424 - [@​hujun-open](https://github.com/hujun-open) made their first contribution in spf13/pflag#418 - [@​LeGEC](https://github.com/LeGEC) made their first contribution in spf13/pflag#429 - [@​ShawnJeffersonWang](https://github.com/ShawnJeffersonWang) made their first contribution in spf13/pflag#409 - [@​max-frank](https://github.com/max-frank) made their first contribution in spf13/pflag#348 **Full Changelog**: spf13/pflag@v1.0.6...v1.0.7 </details> --- ### Configuration 📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS40My41IiwidXBkYXRlZEluVmVyIjoiNDEuNDMuNSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/794 Reviewed-by: earl-warren <[email protected]> Co-authored-by: Renovate Bot <[email protected]> Co-committed-by: Renovate Bot <[email protected]>
This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/spf13/pflag](https://github.com/spf13/pflag) | require | patch | `v1.0.6` -> `v1.0.10` | --- ### Release Notes <details> <summary>spf13/pflag (github.com/spf13/pflag)</summary> ### [`v1.0.10`](https://github.com/spf13/pflag/releases/tag/v1.0.10) [Compare Source](spf13/pflag@v1.0.9...v1.0.10) #### What's Changed - fix deprecation comment for (FlagSet.)ParseErrorsWhitelist by [@​thaJeztah](https://github.com/thaJeztah) in [#​447](spf13/pflag#447) - remove uses of errors.Is, which requires go1.13, move go1.16/go1.21 tests to separate file by [@​thaJeztah](https://github.com/thaJeztah) in [#​448](spf13/pflag#448) #### New Contributors - [@​thaJeztah](https://github.com/thaJeztah) made their first contribution in [#​447](spf13/pflag#447) **Full Changelog**: <spf13/pflag@v1.0.9...v1.0.10> ### [`v1.0.9`](https://github.com/spf13/pflag/releases/tag/v1.0.9) [Compare Source](spf13/pflag@v1.0.8...v1.0.9) #### What's Changed - fix: Restore ParseErrorsWhitelist name for now by [@​tomasaschan](https://github.com/tomasaschan) in [#​446](spf13/pflag#446) **Full Changelog**: <spf13/pflag@v1.0.8...v1.0.9> ### [`v1.0.8`](https://github.com/spf13/pflag/releases/tag/v1.0.8) [Compare Source](spf13/pflag@v1.0.7...v1.0.8) ####⚠️ Breaking Change This version, while only a patch bump, includes a (very minor) breaking change: the `flag.ParseErrorsWhitelist` struct and corresponding `FlagSet.parseErrorsWhitelist` field have been renamed to `ParseErrorsAllowlist`. This should result in compilation errors in any code that uses these fields, which can be fixed by adjusting the names at call sites. There is no change in semantics or behavior of the struct or field referred to by these names. If your code compiles without errors after bumping to/past v1.0.8, you are not affected by this change. The breaking change was reverted in v1.0.9, by means of re-introducing the old names with deprecation warnings. The plan is still to remove them in a future release, so if your code does depend on the old names, please change them to use the new names at your earliest convenience. #### What's Changed - Remove Redundant "Unknown-Flag" Error by [@​vaguecoder](https://github.com/vaguecoder) in [#​364](spf13/pflag#364) - Switching from whitelist to Allowlist terminology by [@​dubrie](https://github.com/dubrie) in [#​261](spf13/pflag#261) - Omit zero time.Time default from usage line by [@​mologie](https://github.com/mologie) in [#​438](spf13/pflag#438) - implement CopyToGoFlagSet by [@​pohly](https://github.com/pohly) in [#​330](spf13/pflag#330) - flag: Emulate stdlib behavior and do not print ErrHelp by [@​tmc](https://github.com/tmc) in [#​407](spf13/pflag#407) - Print Default Values of String-to-String in Sorted Order by [@​vaguecoder](https://github.com/vaguecoder) in [#​365](spf13/pflag#365) - fix: Don't print ErrHelp in ParseAll by [@​tomasaschan](https://github.com/tomasaschan) in [#​443](spf13/pflag#443) - Reset args on re-parse even if empty by [@​tomasaschan](https://github.com/tomasaschan) in [#​444](spf13/pflag#444) #### New Contributors - [@​vaguecoder](https://github.com/vaguecoder) made their first contribution in [#​364](spf13/pflag#364) - [@​dubrie](https://github.com/dubrie) made their first contribution in [#​261](spf13/pflag#261) - [@​mologie](https://github.com/mologie) made their first contribution in [#​438](spf13/pflag#438) - [@​pohly](https://github.com/pohly) made their first contribution in [#​330](spf13/pflag#330) - [@​tmc](https://github.com/tmc) made their first contribution in [#​407](spf13/pflag#407) - [@​tomasaschan](https://github.com/tomasaschan) made their first contribution in [#​443](spf13/pflag#443) **Full Changelog**: <spf13/pflag@v1.0.7...v1.0.8> ### [`v1.0.7`](https://github.com/spf13/pflag/releases/tag/v1.0.7) [Compare Source](spf13/pflag@v1.0.6...v1.0.7) #### What's Changed - Fix defaultIsZeroValue check for generic Value types by [@​MidnightRocket](https://github.com/MidnightRocket) in [#​422](spf13/pflag#422) - feat: Use structs for errors returned by pflag. by [@​eth-p](https://github.com/eth-p) in [#​425](spf13/pflag#425) - Fix typos by [@​co63oc](https://github.com/co63oc) in [#​428](spf13/pflag#428) - fix [#​423](spf13/pflag#423) : Add helper function and some documentation to parse shorthand go test flags. by [@​valdar](https://github.com/valdar) in [#​424](spf13/pflag#424) - add support equivalent to golang flag.TextVar(), also fixes the test failure as described in [#​368](spf13/pflag#368) by [@​hujun-open](https://github.com/hujun-open) in [#​418](spf13/pflag#418) - add support for Func() and BoolFunc() [#​426](spf13/pflag#426) by [@​LeGEC](https://github.com/LeGEC) in [#​429](spf13/pflag#429) - fix: correct argument length check in FlagSet.Parse by [@​ShawnJeffersonWang](https://github.com/ShawnJeffersonWang) in [#​409](spf13/pflag#409) - fix usage message for func flags, fix arguments order by [@​LeGEC](https://github.com/LeGEC) in [#​431](spf13/pflag#431) - Add support for time.Time flags by [@​max-frank](https://github.com/max-frank) in [#​348](spf13/pflag#348) #### New Contributors - [@​MidnightRocket](https://github.com/MidnightRocket) made their first contribution in [#​422](spf13/pflag#422) - [@​eth-p](https://github.com/eth-p) made their first contribution in [#​425](spf13/pflag#425) - [@​co63oc](https://github.com/co63oc) made their first contribution in [#​428](spf13/pflag#428) - [@​valdar](https://github.com/valdar) made their first contribution in [#​424](spf13/pflag#424) - [@​hujun-open](https://github.com/hujun-open) made their first contribution in [#​418](spf13/pflag#418) - [@​LeGEC](https://github.com/LeGEC) made their first contribution in [#​429](spf13/pflag#429) - [@​ShawnJeffersonWang](https://github.com/ShawnJeffersonWang) made their first contribution in [#​409](spf13/pflag#409) - [@​max-frank](https://github.com/max-frank) made their first contribution in [#​348](spf13/pflag#348) **Full Changelog**: <spf13/pflag@v1.0.6...v1.0.7> </details> --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4zNy4xIiwidXBkYXRlZEluVmVyIjoiNDEuMTczLjEiLCJ0YXJnZXRCcmFuY2giOiJtYXN0ZXIiLCJsYWJlbHMiOltdfQ==--> See merge request alpine/infra/build-server-status!21

This PR implements support for
time.Timeflags which can be used to accept timestamps as input directly (see spf13/cobra#742).The implementation allows defining a list of acceptable timestamp formats which are tested in order, i.e., devs using this flag type would be responsible for ordering their formats correctly should there be an overlap.