dtvjl64442 2017-03-09 09:00
浏览 655
已采纳

如何在yaml文件中动态处理UNIX / Windows目录分隔符

We facing a problem where we need to configure a path in one of our config-file.yml. It's a path to an CLI-Script. We facing an old known problem "directory separators" for UNIX/Windows - / - \ . Now we want to configure just one path for both OSs.

Currently we need to switch the path manually depending on the currently used OS. This breaks our VCS handling / application stability all the time.

Unix

bin_path: path/to/script

Windows

bin_path: path\to\script

Is there something like a placeholder known from PHP superglobal DIRECTORY_SEPARATOR? We focusing a solution where we not need to replace the separators in our scripts. I stuck in this problem for a couple of times when using yaml files.

  • 写回答

1条回答 默认 最新

  • duangai1368 2017-03-09 10:06
    关注

    YAML is a data description language and as such has no support for data flowing into the YAML document from the outside (i.e. variables you can use in the document). The only thing you can theoretically do is to structure your YAML document in a way that lets you select the value you actually want afterwards. Example:

    bin_path:
      unix: path/to/script
      windows: path\to\script
    

    Then, when loading the file, you can select the appropriate path for the current operating system. However, this seems convoluted compared to the approach where you simply replace / with \ when on Windows.


    An other solution is to just use one path configured for Windows or UNIX like in the following example. In this way you don't need to configure a specific path for each operation system.

    bin_path: path/to/script
    

    In e.g. PHP you could normalize your pass inside your application by using realpath.

    realpath('path/to/script'); //creates an absolute path including OS based directory separators
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?