Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
apparently no way to implement --foo[=val] #243
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
(#242 touches on this, but is also about a regression)
I agree it's not ideal.
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
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.
@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