3

我一直在尝试用 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. 无论如何,它不会按预期工作。

我无法弄清楚我的下一步应该是什么。

4

1 回答 1

3

您的第一步是拥有可以轻松存储和检索选项的东西:

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"

既然Parser是一个实例,Applicative我们就可以在一个解析器中组合所有选项:

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

我们将所有四个解析器组合成一个解析器,类似于组合 via (,)。现在我们可以通过一次调用来解析选项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
  ...
于 2016-02-17T18:52:59.580 回答