haskell - 使用 cmdargs 排序参数

标签 haskell haskell-cmdargs

我想创建一个程序,它可以通过 cmdargs 获取一些参数。 我想检索文件路径列表和要执行的操作列表。我需要获取这些文件并按顺序执行这些操作。

我的论点是这样声明的:

data Options = 
    Mode1   { 
              input :: [FilePath]
            , act1 :: Bool
            , act2 :: Bool
            , act3 :: Bool
            } deriving (Data, Typeable, Show, Eq)

mode1 = 
  Mode1 { 
            input   = []      &= name "i" &= help "input file"   &= typ "FILE"
          , act1    = False   &= name "a" &= help "action 1"     &= typ "ACTION"
          , act2    = False   &= name "b" &= help "action 2"     &= typ "ACTION"
          , act3    = False   &= name "c" &= help "action 3"     &= typ "ACTION"
        }

我设法使用String(FilePath)列表按顺序获取文件路径列表。通过这种方式,我可以使用以下命令对输入文件进行排序:

./myprog --input="file1.txt" --input="file2.txt"
./myprog --input="file2.txt" --input="file1.txt"

但是只要我的操作声明为 Bool,我就无法对它们进行排序。 我想这样传递我的论点:

./myprog --act1 --act2 --act3 --input="file1.txt" --input="file2.txt"
./myprog --act3 --act1        --input="file1.txt" --input="file2.txt"
./myprog --act2 --act3 --act1 --input="file1.txt" --input="file2.txt"

获得不同的输出结果。

是否可以使用 cmdargs 按顺序检索不同的参数?

最佳答案

Is it possible with cmdargs to retrieve differents arguments in order ?

是的,与 enum .

$ runhaskell ~/testargs.hs -a -b -1 "~" -c -a
Mode1 {input = ["~"], acts = [ActA,ActB,ActC,ActA]}

使用:

{-# LANGUAGE DeriveDataTypeable #-}
import System.Console.CmdArgs

data Act = 
    ActA
  | ActB
  | ActC
  deriving (Show, Typeable,  Data, Eq)

data Options = Mode1
  { input :: [FilePath]
  , acts :: [Act]
  } deriving (Show, Data, Typeable, Eq)

actsDef :: [Act]
actsDef = (enum
  [ [] &= ignore
  , [ActA] &= name "a" &= help "action a"
  , [ActB] &= name "b" &= help "action b" 
  , [ActC] &= name "c" &= help "action c" 
  ]) &= typ "ACTION"

mode :: Options
mode = Mode1
  { input = [] &= name "1" &= help "input file" &= typ "FILE"
  , acts = actsDef 
  }

main = print =<< cmdArgs mode

关于haskell - 使用 cmdargs 排序参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288777/

相关文章:

haskell - 从函数返回 GADT

haskell - 是否可以像NPM(NodeJS)一样设计Stack(Haskell)?

haskell - 高阶函数的计算复杂性?

haskell - 如何指示 cmdargs 不要解析超出某个点的参数?

haskell - Haskell 中的素数

haskell - 如何使用 Pandoc 从 Markdown 文件中获取 YAML 元数据? [ haskell ]