Skip to content

Allow replacing commands with metavar when using parserOptionGroup #523

Description

@nmattia

First of all, thank you for optparse-applicative! I've been using it for many years and it's one of the reasons I come back to Haskell every now and then.

Recently parserOptionGroup was added which allows grouping options. When used alone though, the options are shown twice. In niv, which has a few options (simplified below), it eats up a lot of real estate:

Usage: <interactive> update [PACKAGE] 
                            [
                              (--attribute KEY=VAL | 
                                --string-attribute KEY=VAL | --owner ARG | 
                                --repo ARG | --branch ARG | --rev ARG)]

  Update dependencies

ATTRIBUTES
  --attribute KEY=VAL      ...
  --string-attribute KEY=VAL
                           ...
  --owner ARG              ...
  --repo ARG               ...
  --branch ARG             ...
  --rev ARG                ...

Available options:
  -h,--help                Show this help text

I've come up with a mixture of a dummy option and hidden modifiers which show a metavar instead, which I think makes the output easier to understand:

Usage: <interactive> update [PACKAGE] [ATTRIBUTES]

  Update dependencies

ATTRIBUTES
  --attribute KEY=VAL      ...
  --string-attribute KEY=VAL
                           ...
  --owner ARG              ...
  --repo ARG               ...
  --branch ARG             ...
  --rev ARG                ...

Available options:
  -h,--help                Show this help text

The implementation looks like this:

parsePackageSpec' :: Opts.Parser ParsedPackageSpec
parsePackageSpec' = groupOptions "ATTRIBUTES" $ ParsedPackageSpec <$> Opts.some attribute
    where
        groupOptions :: String -> Opts.Parser a -> Opts.Parser a
        groupOptions mv x = Opts.option empty (Opts.metavar mv) <|> Opts.parserOptionGroup mv x
        
        attribute = Opts.strOption ... Opts.hidden

        ...

Am I missing something obvious with parserOptionGroup? Is there a simpler way to achieve this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions