如何将含..的命令行文件参数转为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
相关产品推荐
相关产品推荐

