如何修复 YAML 语法错误:解析块时未找到预期的“-"指示符? [英] How to fix the YAML syntax error: did not find expected '-' indicator while parsing a block?

查看:43
本文介绍了如何修复 YAML 语法错误:解析块时未找到预期的“-"指示符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 .travis.yml 中有一些代码是为 Python 库编写的.使用 lint.travis-ci.org,我知道我的 YAML 中存在一些缩进问题文件.这是错误指向的部分

I have some code written in my .travis.yml written for a Python library. Using lint.travis-ci.org, I came to know that there is some indentation problem in my YAML file. Here is the part which the error points to

install:

  - if [[ "${TEST_PY3}" == "false" ]]; then
      pip install Cython;
      python setup.py build; # To build networkx-metis
      mkdir core; # For the installation of networkx core
      cd core;
      git clone https://github.com/orkohunter/networkx.git;
      cd networkx/;
      git checkout addons;
      python setup.py install;
      cd ..;
    fi

我哪里错了?错误说

syntax error: (<unknown>): did not find expected '-' indicator while parsing a block collection at line 32 column 3

如果有像 autopep8 这样的工具来修复 YAML 文件的缩进,那就太好了.

It would be great if there were a tool like autopep8 to fix the indentation of YAML files.

推荐答案

您的文件中没有 32 行(可能是因为您从示例中剥离了非必要数据),但缩进级别指向该行与 fi.

You don't have 32 lines in your file (probably because you stripped non-essential data out of the example), but the indentation level points to the line with fi.

实际上问题开始得更早,您要做的是将要执行的操作指定为多行字符串.您可以通过多种方式在 YAML 中指定它们,但最简洁的是使用 literal scalar 指示符|",保留换行符:

Actually the problem starts earlier and what you want to do is specify the action to take as a multi-line string. You can specify those in YAML in multiple ways but the cleanest is to use the literal scalar indicator "|", which preserves newlines:

install:

  - |
    if [[ "${TEST_PY3}" == "false" ]]; then
      pip install Cython;
      python setup.py build; # To build networkx-metis
      mkdir core; # For the installation of networkx core
      cd core;
      git clone https://github.com/orkohunter/networkx.git;
      cd networkx/;
      git checkout addons;
      python setup.py install;
      cd ..;
    fi

没有针对此类错误的自动 YAML 重新缩进工具.

There is no automatic YAML re-indentation tool for these kind of errors.

Python 的 Reindenters 采用工作代码并使缩进保持一致(替换 TAB,每个级别总是相同的缩进).对有语法错误的代码进行 Python 代码重新缩进,要么不起作用,要么可能产生不正确的结果.

Reindenters for Python take working code and make the indentation consistent (replacing TABs, always same indent per level). Python code re-indentation on code with syntax errors, either doesn't work or might produce non-correct results.

YAML 的 Reindenters 面临同样的问题:如果输入没有意义,该怎么办(你和我都清楚,程序并不总是清楚).仅仅将所有不能很好解析的东西都变成多行标量并不是一个通用的解决方案.

Reindenters for YAML face the same problem: what to do if the input doesn't make sense (and what is clear to you and me, is not always clear to a program). Just making everything that doesn't parse well into a multi-line scalar is not a generic solution.

除此之外,大多数 YAML 解析器在读取文件时会丢弃一些信息,您不希望因重新缩进而丢失,包括 EOL 注释、手工制作的锚名称、映射键顺序等.违反规范中的要求.

Apart from that, most YAML parsers throw away some information on reading in the files, that you would not want to get lost by re-indenting, including EOL comments, hand crafted anchor names, mapping key ordering, etc. All without violating the requirements in the specification.

如果您想统一缩进(正确的)YAML,您可以使用 [ruamel.yaml][2] 包中的 yaml 实用程序(免责声明: 我是那个包的作者).您与 yaml round-trip .travis.yml 一起使用的原始输入将给出:

If you want to uniformly indent your (correct) YAML you can use the yaml utility that is part of the [ruamel.yaml][2] package (disclaimer: I am the author of that package). Your original input used with yaml round-trip .travis.yml would give:

 ...
  in "<byte string>", line 3, column 3:
      - if [[ "${TEST_PY3}" == "false" ... 
      ^
expected <block end>, but found '<scalar>'
  in "<byte string>", line 6, column 7:
          mkdir core; # For the installati ...

不幸的是,在查找错误方面没有多大帮助,通过 yaml 往返 .travis.yml 运行的正确 .travis.yml 版本会告诉您它稳定了在第二次往返时(即在第一次时,额外的空白丢失了).而 yaml 往返 .travis.yml --save 给你:

Unfortunately not much more helpful in finding the error, the correct .travis.yml version run through yaml round-trip .travis.yml will tell you that it stabilizes on the second round-trip (ie. on the first the extra whitespace is lost). And yaml round-trip .travis.yml --save gives you:

install:
- |
  if [[ "${TEST_PY3}" == "false" ]]; then
    pip install Cython;
    python setup.py build; # To build networkx-metis
    mkdir core; # For the installation of networkx core
    cd core;
    git clone https://github.com/orkohunter/networkx.git;
    cd networkx/;
    git checkout addons;
    python setup.py install;
    cd ..;
  fi

请注意,在此 # TO build networkx-metis 中不是 YAML 注释.它只是多行字符串的一部分.然而,在第一行之前或最后一行之后的注释将被保留.

Please note that in this # TO build networkx-metis is not a YAML comment. It is just part of the multi-line string. A comment on a line before the first or after the last would however be preserved.

这篇关于如何修复 YAML 语法错误:解析块时未找到预期的“-"指示符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆