配置文件语法

Mik*_*ail 3 syntax configuration-files

我正在开发将使用配置文件的软件.我不知道配置文件的许多语法或格式.以下是我所知道的两个:

(与.conf文件一样)

[section]
key=value
#comment
Run Code Online (Sandbox Code Playgroud)

或(与.ini共同)

key value
; comment
Run Code Online (Sandbox Code Playgroud)

我的兴趣是多才多艺,几乎是一种语言.让我们说吧

[Default]
Start = 0
End = 10
Speed = 1

[Section 3-6]
Speed = 2
Run Code Online (Sandbox Code Playgroud)

这将作为覆盖.然而,这不是我所知道的任何惯例.是否有允许这种情况的通用语法?

AJc*_*dez 12

截至2015年,xml已不再是事实上的标准.这是选项.

TOML

# This is a TOML document.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[servers]

  # tabs / spaces ok but not required
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"
Run Code Online (Sandbox Code Playgroud)

YAML(不安全)

%YAML 1.2
---
YAML: YAML Ain't Markup Language

Projects:
  C/C++ Libraries:
  - libyaml       # "C" Fast YAML 1.1
  - Syck          # (dated) "C" YAML 1.0
  - yaml-cpp      # C++ YAML 1.2 implementation
Run Code Online (Sandbox Code Playgroud)

CSON

# Comments!!!

greatDocumentaries: [
    'earthlings.com'
    'forksoverknives.com'
]

importantFacts:
    # Multi-Line Strings! Without Quote Escaping!
    emissions: '''
        Livestock and their byproducts account for at least 32,000 million tons of carbon dioxide (CO2) per year, or 51% of all worldwide greenhouse gas emissions.
        '''
Run Code Online (Sandbox Code Playgroud)

JSON5Human JSON - 灵活的json超集

属性文件 - 由java程序使用


Mor*_*oth 0

这个曾经有效的答案将在 2020 年被否决,所以我对其进行了编辑。

2011年,我建议你使用xml格式。这是当时事实上的标准。

配置文件可能如下所示:

<?xml version="1.0" encoding="utf-8">
<configuraton>
    <default>
        <start>0</start>
        <end>10</end>
        <speed>1</speed>
    </default>
    <section from="3" to="6">
        <speed>2</speed>
    </section>
</configuration>
Run Code Online (Sandbox Code Playgroud)

有许多库可以解析此类文件。

  • 根据[维基百科](http://en.wikipedia.org/wiki/Configuration_file#See_also),“INI 文件格式是配置文件的事实上的标准。” (3认同)