如何指示cmdargs不解析超出某些点的args?

时间:2017-12-31 08:59:57

标签: haskell command-line-arguments command-line-parsing haskell-cmdargs

我正在研究一个带有其参数的另一个命令的程序:

$ myprog record -f 1.txt ls
$ myprog record -f 1.txt ls -l

像sudo这样的东西:

$ sudo ls
$ sudo ls -l

以下是我使用cmdargs的代码:

#!/usr/bin/env stack
-- stack --resolver lts-9.20 script --package cmdargs

{-# LANGUAGE DeriveDataTypeable #-}

module Main where

import           System.Console.CmdArgs as Args

data Tape = Record  {file :: Maybe String, command :: String, commandArgs :: [String]}
          | Replay
            deriving (Data, Typeable, Show, Eq)
main :: IO ()
main = do
  m <- cmdArgs $ modes
         [ Record  { file        = def &= typ "FILE"
                   , command     = def &= typ "COMMAND" &= argPos 0
                   , commandArgs = def &= typ "ARGS"    &= args     }
         , Replay ]
  print m

它很好用,但仅适用于没有自己标志的命令:

$ ./cmdargs.hs record -f 1.txt ls
Record {file = Just "1.txt", command = "ls", commandArgs = []}

$ ./cmdargs.hs record -f 1.txt ls 1 2 3
Record {file = Just "1.txt", command = "ls", commandArgs = ["1","2","3"]}

$ ./cmdargs.hs record -f 1.txt ls -l
Unknown flag: -l

问:如何指示cmdargs保持&#34; commandArgs&#34;是的,没有在里面寻找旗帜?

1 个答案:

答案 0 :(得分:3)

传统方法不在解析器中,而是使用$ ./args record -- something -l Record {file = Nothing, command = "something", commandArgs = ["-l"]} 参数,指示标志的结束:

json

不幸的是我不知道如何告诉getArgs在任意地方这样做,但我想它会与模式有关。