You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将含..的命令行文件参数转为Haskell Path库的类型安全路径?

如何将含父目录(..)的命令行路径转换为Path库的类型安全路径

我通过optparse-applicative解析命令行参数,得到包含父目录组件(例如../somedir)的路径字符串,需要将其转换为Path库(v0.9.2)中的类型安全路径Path Rel Dir,以便后续与当前目录结合使用。

尝试过以下两种方式均报错:

  • 直接调用parseRelDir解析:
relDir1 <- parseRelDir fp

报错原因是路径中包含..这类父目录组件。

  • 先拼接当前目录再解析相对路径:
relDir2 <- parseRelDir (currDir </> fp) 

同样触发错误。

目前采用FileSystem.Path的collapse函数作为临时解决方案,需要通过decodeString和encodeString在普通FilePath与该库的特殊FilePath类型间转换:

combPath = toFilePath currDir </> (locationDir $ flags) :: FilePath 
collPath = collapse . decodeString $ combPath   
absdir = makeAbsDir . encodeString $ collPath :: Path Abs Dir 

寻求处理该常见场景的更优方法。

内容的提问来源于stack exchange,提问作者user855443

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:22:46