Gabriella439 / Gabriella439/optparse-generic

Treat a `--version` flag specially

Open
#93 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Haskell
Stars
215
Forks
36
PR merge metrics
No merged PRs in 30d

Description

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:

```haskell
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.

```haskell
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:

```haskell
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

```haskell
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.