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

标签 haskell command-line-arguments 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 保持“commandArgs”原样,而不在其中查找标志?

最佳答案

传统方法不在解析器内,而是使用 -- 参数,指示标志的结束:

$ ./args record -- something -l
Record {file = Nothing, command = "something", commandArgs = ["-l"]}

不幸的是,我不知道如何告诉 getArgs 在任意位置执行此操作,但我想这可能与模式有关。

关于haskell - 如何指示 cmdargs 不要解析超出某个点的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041242/

相关文章:

haskell - 为什么在 haskell 中 show 不被视为转换?

haskell - 为 newtype 创建 MonadBaseControl 实例

python-2.7 - 使用 Class() 时如何显示 argparse 中所有子解析器的帮助

haskell - 使用 cmdargs 排序参数

parsing - 双递归函数中的 Haskell 类型错误

scala - 纯函数式编程中的 "value"是什么?

python - 通过命令行传递 Python 变量?

c - 从 unix 命令行运行时 getopt 无法正常工作

haskell - 没有等号的 cmdargs 值参数