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

Strict (no-intersperse) arguments #22

Closed
timbertson opened this issue Sep 30, 2012 · 1 comment
Closed

Strict (no-intersperse) arguments #22

timbertson opened this issue Sep 30, 2012 · 1 comment

Comments

@timbertson
Copy link

I'd like the ability to have option parsing stop on the first non-flag argument encountered, to prevent having to always use "--" for a command that is a runner for other programs with their own arguments.

(like python's optparse, when `disable_interspersed_args() is called):
http://docs.python.org/library/optparse.html#optparse.OptionParser.disable_interspersed_args

I had a go at adding a strict version of arguments and arguments1, but had trouble with the tests and wasn't sure how to best integrate it. Here's what I came up with which only supports non-interspersed mode, if that's useful:

arguments_ allow_empty p m = set_default <$> fromM args1
    where
        Mod f (DefaultProp def sdef) g = m
        show_def = sdef <*> def

        props = mkProps mempty g
        props' = (mkProps mempty g) { propShowDefault = show_def }

        args1 | allow_empty = args
                    | otherwise = do
            mx <- oneM arg_or_ddash
            case mx of
                Nothing -> someM arg
                Just x  -> (x:) <$> manyM arg

        args = do
            mx <- oneM $ optional arg_or_ddash
            case mx of
                Nothing       -> return []
                Just Nothing  -> manyM arg
                Just (Just x) -> (x:) <$> manyM arg

        arg_or_ddash = (ddash *> pure Nothing) <|> (p <$> nonOpt)
        set_default [] = fromMaybe [] def
        set_default xs = xs

        arg = liftOpt (Option (ArgReader (CReader compl p)) props)

        ddash :: Parser ()
        ddash = argument' (guard . (== "--")) internal

        nonOpt = argument' nonOpt' internal
        nonOpt' ('-':_) = Nothing
        nonOpt' a = Just a

        ArgumentFields compl = f (ArgumentFields mempty)

caveat: this could be wrong, I don't fully understand the code yet ;). Seems to work in the cases I threw at it though.

It could be a better idea to have this flag be over the whole of argument parsing, rather than built into the arguments parser. But I'm not even sure if that's possible with the current structure of parsers.

@pcapriotti
Copy link
Owner

It could be a better idea to have this flag be over the whole of argument parsing, rather than built into the arguments parser. But I'm not even sure if that's possible with the current structure of parsers.

I agree, this should be handled at the lower level.

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