haskellcommand-line-argumentsapplicativeoptparse-applicative

optparse-applicative subcommand help text


I'm using optparse-applicative that comes with stackage lts 5.1 I have a parser with subcommands and I have described a help text for their options, but they don't show.

This is the output when I run the executable with --help :

[david@devcentos65 manipro]$ /home/david/.local/bin/manipro --help
manipro - text1

Usage: manipro COMMAND [-v|--verbose]   text2

Available options:  
  -h,--help                Show this help text  
  -v,--verbose             text3

Available commands:   
  export                   text4
  dico                     text9

The code :

parserArgs :: ParserInfo ArgApp
parserArgs = info (helper <*> args) desc
    where
    desc =  
        fullDesc <> 
        progDesc "text1"  <> 
        header "text2"


args = ArgApp <$> argCmd <*> optverbose
    where
    optverbose = switch ( 
        short 'v' <> long "verbose" <> 
        help "text3" )

argCmd = subparser (argCmdExport <> argCmdDico)

argCmdExport = command "export" infos
    where
    infos = info options desc
    desc  = progDesc "text4" 
    options = ArgCmdExport <$> 
        argModeExport <*> 
        argTableExport <*> 
        argOptExport

argModeExport  = argument auto (metavar "FORMAT")
argTableExport = argument text (metavar "TABLE")

argOptExport = ArgOptExport <$> optional noesc <*> optional cols <*>
    ens <*> tst
    where
    noesc = option textList (long "noesc" <> metavar "CHAMPS" <> help "text5" )
    cols = option textList (long "cols" <> metavar "CHAMPS" <> help "text6" )
    ens = flag EnsEtoile  EnsDollar (short 'd' <> long "dollar" <> 
        help "text7") 
    tst = flag False True (short 't' <> long "test" <> 
        help "text8")

argCmdDico    = command "dico" infos
    where
    infos = info options desc
    desc  = progDesc "text9" 
    options = ArgCmdDico <$> 
        argOptDico

argOptDico = ArgOptDico <$> optional tables
    where
    tables = option textList (long "tables" <> metavar "TABLES" <>
        help "text10" )


text = str >>= return . pack
textList = str >>= return . splitOn "," . pack

Solution

  • optparse-applicative hides the detailed description of a command deliberately if you only use --help. After all, you might have a dozen commands. For example, stack has 34. Listing probably fills your terminal vertically. If it displayed all possible arguments, you would end up with a lot of text.

    Instead, --help will only show the common arguments and a list of commands. In order to show the description of a single command, you have to use <executable> <command> --help:

    $ stack --help | head
    stack - The Haskell Tool Stack
    
    Usage: stack [--help] [--version] [--numeric-version] [--docker*] [--nix*]
                 ([--verbosity VERBOSITY] | [-v|--verbose]) [--work-dir WORK-DIR]
                 [--[no-]system-ghc] [--[no-]install-ghc] [--arch ARCH] [--os OS]
                 [--ghc-variant VARIANT] [-j|--jobs JOBS] [--extra-include-dirs DIR]
                 [--extra-lib-dirs DIR] [--[no-]skip-ghc-check] [--[no-]skip-msys]
                 [--local-bin-path DIR] [--[no-]modify-code-page]
                 [--resolver RESOLVER] [--compiler COMPILER] [--[no-]terminal]
                 [--stack-yaml STACK-YAML] COMMAND|FILE
    
    $ stack --help | grep clean
      clean                    Clean the local packages
    
    $ stack clean --help
    Usage: stack clean [PACKAGE] [--help]
      Clean the local packages
    
    Available options:
      PACKAGE                  If none specified, clean all local packages
      --help                   Show this help text
    
    Run 'stack --help' for global options that apply to all subcommands.
    

    This reduces the clutter somewhat. It also follows the same rules as other popular multi-command applications, such as git and hg (with the small exception that <executable> --help <command> will still show only the general help).