optparse-applicative子命令帮助文本

时间:2016-02-15 16:18:30

标签: haskell command-line-arguments applicative optparse-applicative

我正在使用带有stackage lts 5.1的optparse-applicative 我有一个带子命令的解析器,我已经为他们的选项描述了一个帮助文本,但它们没有显示。

这是我使用--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

代码:

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

2 个答案:

答案 0 :(得分:2)

如果您只使用MemoryError

x_list = table_pivot['X'].columns.values y_list = table_pivot['Y'].columns.values table_diff = pandas.DataFrame() for each_x in x_list: for each_y in y_list: colName = each_y + ' - ' + each_x table_diff[colName] = table_pivot['Y'][each_y] - table_pivot['X'][each_x] 会故意隐藏命令的详细说明。毕竟,你可能有十几个命令。例如,optparse-applicative有34.列表可能会垂直填充您的终端。如果它显示了所有可能的参数,那么你最终会得到很多文本。

相反,--help只会显示 common 参数和命令列表。为了显示单个命令的描述,您必须使用stack

--help

这有点减少了混乱。它还遵循与其他流行的多命令应用程序相同的规则,例如<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. git仍然只显示常规帮助的小例外)。

答案 1 :(得分:2)

正如Zeta所指出的,<executable> <command> --help上显示了单个命令的描述。但为了实现这一点,在optparse-applicative中,每个命令的子分析器都需要一个选项--help的解析器。您只为<executable> --help定义了一个(请参阅haskell代码块的第二行)。如果没有特定命令的帮助解析器,<executable> <command> --help的输出将不是帮助消息,而是关于invalid option --help和使用说明的通知。

一般模式是将--help选项的解析器(即helper)依次应用于解析器以获取命令的参数,如下所示:command "command-name" (info (yourCommandArgParser <**> helper) (fullDesc <> progDesc "your commands description" <> ...))

所以对于export命令,你可以写:

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

PS。这个问题已经过时了,但当我遇到同样的问题时,我偶然发现了它。网上还没有任何关于它的内容。