Skip to content
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

Sum type parsing #50

Open
MaxGabriel opened this issue Mar 5, 2018 · 1 comment
Open

Sum type parsing #50

MaxGabriel opened this issue Mar 5, 2018 · 1 comment

Comments

@MaxGabriel
Copy link

Hi, I wanted to use a sum type in my command line parsing, and I came up with this:

data FrontendLanguage = Typescript | Swift deriving (Eq, Show, Read, Generic)

instance ParseField FrontendLanguage
instance ParseRecord FrontendLanguage
instance ParseFields FrontendLanguage

data Tasks
    = GenerateRoutes 
      { language :: FrontendLanguage <?> "Options are: Typescript, Swift"
      , frontendRepo :: FilePath <?> "Path to the root of the frontend repo"
      }
    deriving (Generic, Show)

instance ParseRecord Tasks

It works fine in the success case, though there isn't a specific error message if you provide an invalid sum type, just the generic help text.

Is this the best way to do this? I couldn't find anything in the documentation about this (could have missed it, though). If it is the best way, I'll submit a PR to document this use-case in the Haddocks if you'd like.

@Gabriella439
Copy link
Owner

Yeah, that's about the best you can do within this library. You can do better if you use the underlying optparse-applicative library to define the ParseFields instance for FrontendLanguage because then you can define it to be something like ( --typescript | --swift )

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

No branches or pull requests

2 participants