Skip to content

date: use custom value parser#4977

Merged
sylvestre merged 2 commits intouutils:mainfrom
cakebaker:date_shortcut_value_parser
Jul 4, 2023
Merged

date: use custom value parser#4977
sylvestre merged 2 commits intouutils:mainfrom
cakebaker:date_shortcut_value_parser

Conversation

@cakebaker
Copy link
Contributor

This PR introduces ShortcutValueParser, a custom value parser for clap that not only recognizes the exact values from a list of allowed values but also shortcuts of them (for example, h, ho, hou, are recognized as hour).

Closes #4744

@tertsdiepraam
Copy link
Collaborator

Super nice! We can use this in almost every util

@tertsdiepraam
Copy link
Collaborator

tertsdiepraam commented Jun 14, 2023

I've previously implemented something very similar as a derive macro in the argument parser I was working on (which is currently paused until I find the time to work on it again). Spinning that out of that parser and into uucore could be a nice follow-up to this PR. The derive macro allows you to write this:

#[derive(Default, Debug, PartialEq, Eq, Value)]
enum Format {
    #[value("long")]
    Long,

    #[value("single-column")]
    SingleColumn,

    #[default]
    #[value("columns", "vertical")]
    Columns,

    #[value("across", "horizontal")]
    Across,

    #[value("commas")]
    Commas,
}

The code for the macro is here: https://github.com/tertsdiepraam/uutils-args/blob/2f1a9b0ed0a328d600992e08a0950a07761a8a3b/derive/src/lib.rs#L115

@cakebaker cakebaker force-pushed the date_shortcut_value_parser branch from a86e6f1 to 643afbd Compare June 14, 2023 11:35
@uutils uutils deleted a comment from github-actions bot Jun 20, 2023
@sylvestre sylvestre merged commit 9d44d8b into uutils:main Jul 4, 2023
@cakebaker cakebaker deleted the date_shortcut_value_parser branch July 5, 2023 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

date -Is is failing

3 participants