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

Treat a --version flag specially #93

Open
parsonsmatt opened this issue Apr 15, 2022 · 2 comments
Open

Treat a --version flag specially #93

parsonsmatt opened this issue Apr 15, 2022 · 2 comments

Comments

@parsonsmatt
Copy link

Currently, optparse-generic supports --help as a special case.

It'd be nice to also support --version as a special case. It's currently possible to support in a record argument like:

data Opts = Opts { version :: Bool } deriving (Generic, ParseRecord)

main = do
    Opts {..} <- getRecord "main"
    when version $ do
        print version
        exitSuccess
    do the rest of the stuff

This does not work with a subcommand parser. Or, it kinda does, but you need --version after selecting a sub-command. Or you make version it's own subcommand.

data Cmd = Version | OtherCommand
    deriving (Generic, ParseRecord)
    
main = do
    cmd <- getRecord "Cmd"
    case cmd of
        Version -> print version
        OtherCOmmand -> etc

This can be worked-around with a hack:

main = do
    args <- getArgs
    when ("--version" `elem` args) $ do
        print version >> exitSuccess
    Opts {..} <- getRecord "main"
    etc

But then we don't get pretty documentation on the --version flag.

okay, that's a lot of prelude,

Request

getRecordWithVersion 
    :: ParseRecord a
    => IO ()
    -- ^ Action to call if a `--version` flag is passed
    -> String
    -> IO a 

which augments the a parser with the --version flag and calls the special thing.

This would probably be pretty easy to support as an external library, or as app-local code.

@Gabriella439
Copy link
Owner

Yeah, I would accept a pull request to add this

@silky
Copy link

silky commented Jan 26, 2024

Just adding another vote for this :) Came to the issues looking to report just this!

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

3 participants