-
Notifications
You must be signed in to change notification settings - Fork 116
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
apparently no way to implement --foo[=val] #243
Comments
Hi, I agree it's not ideal. We've been asked about this one a few times and have been cautious in supporting this due to ambiguity concerns amongst other things #235 #67 #109. But I believe you are correct in that if you enforce that the option must be specified with an Without writing anything, I believe this will require a new entry in the Huw |
Not sure if I'm totally following as I'm new to both Haskell and this library (I guess this is a likely library to catch newbies? not sure ;-)). Anyway, I was trying something similar but for fixed values on the RHS of equals using alternatives as well: sdlDefaultRendererP = flag' (rendererType(defaultRenderer)) (
long "renderer=default"
<> help "Use SDL's default renderer")
So as we can see, parsing the I think the above is probably what is described in #242 as
Anyway, in my particular case, I don't really need |
This is unrelated. So you shouldn't be using sdlRendererP =
option renderReader $
long "renderer" <> help "Select SDL renderer"
where
renderReader :: ReadM Renderemajig
renderReader = auto >>= \case
"default" -> pure defaultRenderer
_ -> fail "Nope" Please open a separate issue if you need additional help. We don't want to bother Joey. |
If we could require |
@HuwCampbell I think this is a very important feature to have. And implementing optional arguments would increase I'm completely okay with the disambiguation by the A few examples of how optional args can be used:
I think optional args provide much better UX and you need to remember/implement much fewer commands if the feature is implemented. |
There's currently an okish way to do this, after the fix for #242. optArg :: String -> String -> ReadM a -> Parser (Maybe a)
optArg x meta rdr =
flag' Nothing (long x <> style (<> string ("[=" <> meta <> "]"))) <|>
option (Just <$> rdr) (long x <> internal)
two :: Parser (Maybe Int, Maybe (Maybe String))
two = (,) <$> optArg "milestone" "INT" (auto :: ReadM Int)
<*> optional (optArg "output" "FILE" (str :: ReadM String))
main :: IO ()
main = execParser (info (two <**> helper) mempty) >>= print
|
It's a fairly common pattern for an option --foo to set some default value, and --foo=val to set some more specific value. (With the equals sign being required before the value; a space is ambiguous in this case.)
I cannot find a way to implement this with optparse-applicative, without renaming one of the options from --foo to --bar.
This code compiles, and usage shows "[--foo ARG] | [--foo]", but parsing "--foo" fails.
I don't really like that as a way of implementing it even if it worked, due to the redundancy.
Could there be a version of
option
that takes a default value, for when no value is provided?(#242 touches on this, but is also about a regression)
The text was updated successfully, but these errors were encountered: