【问题标题】:Incorrect behavior of getArgs on Windows under cygwincygwin 下 Windows 上 getArgs 的不正确行为
【发布时间】:2012-09-05 15:45:54
【问题描述】:

我试图将引号中的星号符号作为命令行参数传递到我的控制台应用程序中,当我使用System.Environment.getArgs 获取它时,我实际上得到了当前目录中的文件列表。这是错误的,因为我将星号包装在引号中,因此不应替换引号中的文本。如何在 cygwin 下的 Windows 中获取没有这种替换的命令行参数?

【问题讨论】:

  • 这似乎更像是 windows 的问题,而不是 Haskell 的问题。
  • 你是用单引号还是双引号?
  • 这是你的 shell 的问题,而不是 getArgs 的问题。在将任何参数发送到您的程序之前执行通配符。
  • @valderman 在 Windows 上不一定是这样,在 Windows 上程序需要自己执行 glob 扩展(cmd.exe 不会扩展 glob,GetCommandLine 只是一个字符串;C 运行时会解析在输入main之前)。
  • 哦。我想我已经在 *nix-land 中停留了太久,忘记了这一点。 :)

标签: windows haskell command-line cygwin


【解决方案1】:

如果将其括在单引号中,'*' 它不会被扩展,但两个引号会进入getArgs 的结果(见下文),因此您需要稍后将其删除。

"*" 在 Windows 中展开的原因是由于空格,引号作为全局文件名的一部分是合法的。你可能想在某个时候del "temp file *.dat"

module ListArgs where
 import System.Environment
 main = getArgs >>= print

给予:

[1 of 1] Compiling ListArgs             ( ListArgs.hs, ListArgs.o )
Linking ListArgs.exe ...

D:\Files\Andrew\prog\haskell\utils>ListArgs.exe *
["HereDoc.hs","IOutils.lhs","SugaredApplicative.hs","ListArgs.exe","ListArgs.hi","ListArgs.hs","ListArgs.o"]

D:\Files\Andrew\prog\haskell\utils>ListArgs.exe "*"
["HereDoc.hs","IOutils.lhs","SugaredApplicative.hs","ListArgs.exe","ListArgs.hi","ListArgs.hs","ListArgs.o"]

D:\Files\Andrew\prog\haskell\utils>ListArgs.exe '*'
["'*'"]

D:\Files\Andrew\prog\haskell\utils>ListArgs.exe '*
["'*"]

D:\Files\Andrew\prog\haskell\utils>ListArgs.exe -*
["-*"]

【讨论】:

  • AndrewC,感谢您的回答,单引号在 cmd.exe 中确实有效。但是在 cygwin 下它不起作用,我仍然得到当前目录中的文件列表。 -* 无处不在。
  • 使用前缀和单引号是一种解决方法,显然不适合用户。开发人员还应该记住 Windows 中的此类行为,并以某种方式从输入中消除不需要的符号。我希望有一个不会进行此类替换的功能。但似乎 Haskell 平台不提供这样的功能。可惜了。
  • 我已经好几年没用过cygwin了。我将添加一个标签以扩大受众范围。
猜你喜欢
  • 2017-06-25
  • 1970-01-01
  • 2012-11-26
  • 2015-03-01
  • 2013-08-29
  • 2011-06-25
  • 2016-05-29
  • 1970-01-01
  • 2012-11-15
相关资源
最近更新 更多