开发者

Configuration file syntax

I'm developing software that will utilize a config file. I don't know many syntaxes or formats of config files. Here are the two I know:

(common with .conf files)

[section]
key=value
#comment

or (common with .ini)

key value
; comment

My interest is something versatile that's almost a language. Let's say

[Default]
Start = 0
End =开发者_开发百科 10
Speed = 1

[Section 3-6]
Speed = 2

This would act as an override. However this isn't any convention that I know of. Is there a common syntax that allows for this?


As of 2015, xml is no longer the de facto standard. Here are options.

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"

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

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.
        '''

JSON5 and Human JSON - flexible json supersets

Properties File - used by java programs


This once-valid answer is being voted down in 2020, so I edited it.

In 2011, I suggested you use an xml format. This was the de-facto standard then.

The configuration document could have looked like this:

<?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>

There are many libraries to parse such files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜