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

Is it possible to automatically show in help which values are valid for an option? #467

Open
wolverian opened this issue Mar 16, 2023 · 3 comments

Comments

@wolverian
Copy link

Given an option of a sum type (Foo in this example):

module Main where

import Options.Applicative

data Opts = Opts
    { flag :: Foo
    , ok :: Bool
    }
    deriving (Show)

data Foo = Bar | Baz | Quux deriving (Read, Show)

opts :: Parser Opts
opts =
    Opts
        <$> option auto (help "how to do it" <> long "flag" <> value Bar <> showDefault)
        <*> option auto (help "is it ok" <> long "ok" <> value True <> showDefault)

main :: IO ()
main = do
    opts' <- execParser (info (opts <**> helper) fullDesc)
    print opts'

This is the help output currently:

Usage: optest [--flag ARG] [--ok ARG]

Available options:
  --flag ARG               how to do it (default: Bar)
  --ok ARG                 is it ok (default: True)
  -h,--help                Show this help text

Instead of --flag ARG how to do it (default: Bar) I would like something like --flag ARG how to do it (default: Bar, allowed values: Foo, Bar, Baz). Can optparse-applicative do this?

@HuwCampbell
Copy link
Collaborator

Interesting question. We don't support this natively at the moment, but I think it would be a simple addition to write.

With a Bounded instance on your type, one could imagine something like:

option auto (help "how to do it" <> long "flag" <> value Bar <> showDefault <> boundedPossibilities)

Seeing as all showDefault does is organise the parens and default text to tack onto the end of the help text for the option, you can do it yourself with the help text.

@wolverian
Copy link
Author

With a Bounded instance on your type, one could imagine something like:

I think you'd want Enum as well.

Seeing as all showDefault does is organise the parens and default text to tack onto the end of the help text for the option, you can do it yourself with the help text.

Is there a way to add to the help text with a Mod, instead of replacing it altogether?

@HuwCampbell
Copy link
Collaborator

I think you'd want Enum as well.

Yes

Is there a way to add to the help text with a Mod, instead of replacing it altogether?

No you would have to just put it in help yourself.

I would be happy to look at a PR for this. My only worry is that one could use it for Int.

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