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

问题描述 投票:2回答:1

我正在开发一个程序,它带有另一个带有参数的命令:

$ 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”,而不查找其中的标志?

haskell command-line-arguments command-line-parsing haskell-cmdargs
1个回答
3
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.