TOML 配置文件语法

精选 TOML 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。 本备忘单为 TOML (Tom's Obvious, Minimal Language) 格式配置文件语法提供快速速查手册。

#🚀 入门指引

#入门介绍

TOML 是一种旨在易于读取和编写的最小化配置文件格式,具有极其清晰直观的语义定义。

#💡 实用示例

bool = true
date = 2006-05-27T07:32:00Z
string = "hello"
number = 42
float = 3.14
scientificNotation = 1e+12

#代码注释

# 单行注释示例

# 块级多行注释示例
# 注释行 1
# 注释行 2
# 注释行 3

#整数 (Integer)

int1 = +42
int2 = 0
int3 = -21
integerRange = 64

#浮点数 (Float)

float2 = 3.1415
float4 = 5e+22
float7 = 6.626e-34

#布尔值 (Boolean)

bool1 = true
bool2 = false
boolMustBeLowercase = true

#日期与时间 (Datetime)

date1 = 1989-05-27T07:32:00Z
date2 = 1989-05-26T15:32:00-07:00
date3 = 1989-05-27T07:32:00
date4 = 1989-05-27
time1 = 07:32:00
time2 = 00:32:00.999999

#字符串 (String)

str1 = "I'm a string."
str2 = "You can \"quote\" me."
str3 = "Name\tJos\u00E9\nLoc\tSF."

参见:TOML 字符串

#表结构 (Table)

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

参见:TOML 表结构

#基础数组 (Array)

array1 = [1, 2, 3]
array2 = ["Commas", "are", "delimiter"]
array3 = [8001, 8001, 8002]

#多类型与多行数组 (Friendly Array)

array1 = [ "Don't mix", "different", "types" ]
array2 = [ [ 1.2, 2.4 ], ["all", 'strings', """are the same""", '''type'''] ]
array3 = [
  "Whitespace", "is",
  "ignored"
]

#TOML 字符串 (Strings)

#多行字符串 (Multiline String)

multiLineString = """
Multi-line basic strings are surrounded
by three quotation marks on each side
and allow newlines.
"""

#原生字面量字符串 (Literal String)

path = 'C:\Users\nodejs\templates'
path2 = '\\User\admin$\system32'
quoted = 'Tom "Dubs" Preston-Werner'
regex = '<\i\c*\s*>'

包裹在单引号 ' 内部,不支持转义字符。

#多行原生字面量字符串 (MultiLine Literal String)

re = '''\d{2} apps is t[wo]o many'''
lines = '''
The first newline is
trimmed in raw strings.
All other whitespace
is preserved.
'''

#TOML 表结构 (Tables)

#基础表 (Basic Table)

[name]
foo = 1
bar = 2

foobar 是表 name 下的属性 Key

#嵌套表 (Nested Tables)

[table1]
	foo = "bar"

[table1.nested_table]
	baz = "bat"

#表对象数组 (Array-like Tables)

[[comments]]
author = "Nate"
text = "Great Article!"

[[comments]]
author = "Anonymous"
text = "Love it!"

↓ 等价的 JSON 结构

{
  "comments": [
    {
      "author": "Nate",
      "text": "Great Article!"
    },
    {
      "author": "Anonymous",
      "text": "Love It!"
    }
  ]
}

#带点的带引号表名 (Dot separated)

[dog."tater.man"]
type = "pug"

↓ 等价的 JSON 结构

{
  "dog": {
    "tater.man": {
      "type": "pug"
    }
  }
}

#多层深层嵌套表 (Multi-nested)

[foo.bar.baz]
bat = "hi"

↓ 等价的 JSON 结构

{
  "foo": {
    "bar": {
      "baz": {
        "bat": "hi"
      }
    }
  }
}

#空格无关写法 (Ignore whitespace)

[a.b.c]          # 推荐的最佳写法规范
[ d.e.f ]        # 等价于 [d.e.f]
[ g .  h  .i ]   # 等价于 [g.h.i]
[ j . "ʞ" .'l' ] # 等价于 [j."ʞ".'l']

#行内表 (Inline Table)

name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
animal = { type.name = "pug" }