Haskell: Turtle: 命令行解析器

4

我一直在尝试使用Turtle构建命令行解析器,没有太高的要求:https://github.com/Tyrn/go-procr

#!/usr/bin/env stack
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Turtle
import Prelude hiding (FilePath)

parserSwitch :: Parser (Bool, Bool)
parserSwitch = (,) <$> switch "verbose" 'v' "Unless verbose, just progress bar is shown"
                   <*> switch "filetitle" 'f' "Use file name for title tag"

parserArg :: Parser (FilePath, FilePath)
parserArg = (,)    <$> argPath "src" "Source directory"
                   <*> argPath "dst" "Destination directory"

main :: IO ()
main = do
  (verbose, filetitle) <- options "Flags" parserSwitch
  echo (format ("verbose: "%w) verbose)
  echo (format ("filetitle: "%w) filetitle)
  (src, dst) <- options "Args" parserArg
  echo (format ("src: "%fp) src)
  echo (format ("dst: "%fp) dst)

需要三种类型的参数:布尔标志; 选项,文本和整数; 位置参数。到目前为止,我在布尔标志和位置参数方面遇到了困难。不幸的是,即使对于这个问题,示例也似乎太基础了。

  1. 我真的必须为不同类型的选项构建单独的解析器吗(我无法使用单个解析器满足语法)?

  2. 无论如何,它都不会按预期工作。

我无法确定我的下一步应该是什么。

1个回答

4
你的第一步是需要有一个可以轻松存储和检索选项的工具:
data Settings = Settings
   { settingsVerbose      :: Bool
   , settingsFileTitle    :: Bool
   , settingsSource       :: FilePath
   , settingsDestination  :: FilePath
   }

之后,您需要编写选项解析器。为了让事情更清晰,让我们先详细一些:

verboseParser :: Parser Bool
verboseParser = switch "verbose" 'v' "Be more verbose"

fileTitleParser :: Parser Bool
fileTitleParser = switch "filetitle" 'f' "..."

sourceParser :: Parser FilePath
sourceParser = argPath "src" "Source directory"

destinationParser :: Parser FilePath
destinationParser = argPath "dst" "Destination directory"

由于ParserApplicative的一个实例,我们可以将所有选项组合成一个单一的解析器:

settingsParser :: Parser Settings
settingsParser = 
    Settings <$> verboseParser
             <*> fileTitleParser
             <*> sourceParser
             <*> destinationParser

我们已经将所有四个解析器合并为一个解析器,类似于通过(,)的组合。现在我们可以通过单个调用options来解析选项。毕竟,要么所有参数都是正确的,要么我们必须向用户呈现正确的使用方式:
main = do
   s <- options "Description of your program" settingsParser

   echo (format ("verbose: "%w)   (settingsVerbose s))
   echo (format ("filetitle: "%w) (settingsFileTitle s))
   echo (format ("src: "%fp)      (settingsSource s))
   echo (format ("dst: "%fp)      (settingsDestination s))

尽管如此,您可能希望使用更短的名称,并且也许要在 settingsParser 中编写解析器:

data Settings = Settings
   { sVerbose     :: Bool
   , sFileTitle   :: Bool
   , sSource      :: FilePath
   , sDestination :: FilePath
   }

settingsP :: Parser Settings
settingsP = 
  Settings <$> switch "verbose"   'v' "Be more verbose"
           <*> switch "filetitle" 'f' "..."
           <*> argPath "src" "Source directory"
           <*> argPath "dest" "Destination directory"

description :: Description
description = "Description of your program"

main = do
  (Settings verbose filetitle source dest) <- options description settingsP
  ...

非常感谢您,Zeta!settingsP方法非常出色。 - Alexey Orlov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接