@@ -21,7 +21,32 @@ format(<formatString>, <arg1>, <arg2>, ...)
21
21
22
22
The ` format() ` function returns a string that includes formatted values using a template and
23
23
placeholders. Each placeholder in the template string is replaced with the corresponding argument
24
- value. Placeholders are specified with curly braces around the zero-based index of the argument.
24
+ value.
25
+
26
+ ### Placeholder syntax
27
+
28
+ Placeholders must be defined with the following syntax:
29
+
30
+ ``` Syntax
31
+ {<index>[:<formatSpecifier>]}
32
+ ```
33
+
34
+ Every placeholder must specify the zero-based index of the argument.
35
+
36
+ This function supports a subset of format specifiers for controlling how data is formatted in the
37
+ string. To use a format specifier, define a placeholder followed by a colon and then a specifier.
38
+
39
+ The following table defines the supported format specifiers and the data types they're valid for.
40
+ If you use a format specifier with an invalid data type, DSC raises an error.
41
+
42
+ | Specifier | Format | Valid Types |
43
+ | :---------:| :----------------------| :------------|
44
+ | ` b ` | Binary | ` integer ` |
45
+ | ` e ` | Lowercase exponential | ` integer ` |
46
+ | ` E ` | Uppercase exponential | ` integer ` |
47
+ | ` o ` | Octal | ` integer ` |
48
+ | ` e ` | Lowercase hexadecimal | ` integer ` |
49
+ | ` E ` | Uppercase hexadecimal | ` integer ` |
25
50
26
51
## Examples
27
52
@@ -54,13 +79,60 @@ messages: []
54
79
hadErrors : false
55
80
` ` `
56
81
57
- ### Example 2 - Format a string with parameters
82
+ ### Example 2 - Format specifiers
58
83
59
- The configuration uses other functions within the ` format()` function to build a dynamic message .
84
+ This example demonstrates formatting every supported data type and format specifier .
60
85
61
86
` ` ` yaml
62
87
# format.example.2.dsc.config.yaml
63
88
$schema : https://aka.ms/dsc/schemas/v3/bundled/config/document.json
89
+ resources :
90
+ - name : descriptive resource name
91
+ type : Microsoft.DSC.Debug/Echo
92
+ properties :
93
+ output :
94
+ string : " [format('{0} {1}', 'hello', 'world')]"
95
+ boolean : " [format('{0} or {1}', true, false)]"
96
+ integer :
97
+ binary : " [format('{0} => {0:b}', 123)]"
98
+ octal : " [format('{0} => {0:o}', 123)]"
99
+ lowercaseHex : " [format('{0} => {0:x}', 123)]"
100
+ uppercaseHex : " [format('{0} => {0:X}', 123)]"
101
+ lowercaseExponent : " [format('{0} => {0:e}', 123)]"
102
+ uppercaseExponent : " [format('{0} => {0:E}', 123)]"
103
+ ` ` `
104
+
105
+ ` ` ` bash
106
+ dsc config get --file format.example.2.dsc.config.yaml
107
+ ```
108
+
109
+ ``` yaml
110
+ results :
111
+ - name : descriptive resource name
112
+ type : Microsoft.DSC.Debug/Echo
113
+ result :
114
+ actualState :
115
+ output :
116
+ string : hello world
117
+ boolean : true or false
118
+ integer :
119
+ binary : 1111011
120
+ octal : 173
121
+ lowercaseHex : 7b
122
+ uppercaseHex : 7B
123
+ lowercaseExponent : 1.23e2
124
+ uppercaseExponent : 1.23E2
125
+ messages : []
126
+ hadErrors : false
127
+ ` ` `
128
+
129
+ ### Example 3 - Format a string with parameters
130
+
131
+ The configuration uses other functions within the ` format()` function to build a dynamic message.
132
+
133
+ ` ` ` yaml
134
+ # format.example.3.dsc.config.yaml
135
+ $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
64
136
parameters:
65
137
username:
66
138
type: string
@@ -75,19 +147,22 @@ resources:
75
147
- name: Echo dynamic formatted string
76
148
type: Microsoft.DSC.Debug/Echo
77
149
properties:
78
- output: "[format('Hello, {0}! The time is {1}:{2}.', parameters('username'), parameters('hour'), parameters('minute'))]"
150
+ output: >-
151
+ [format(
152
+ 'Hello, {0}! The time is {1}:{2}.',
153
+ parameters('username'),
154
+ parameters('hour'),
155
+ parameters('minute')
156
+ )]
79
157
` ` `
80
158
81
159
` ` ` bash
82
- dsc --file format.example.2 .dsc.config.yaml config get
160
+ dsc --file format.example.3 .dsc.config.yaml config get
83
161
` ` `
84
162
85
163
` ` ` yaml
86
164
results:
87
- - metadata:
88
- Microsoft.DSC:
89
- duration: PT0.0185508S
90
- name: Echo dynamic formatted string
165
+ - name: Echo dynamic formatted string
91
166
type: Microsoft.DSC.Debug/Echo
92
167
result:
93
168
actualState:
@@ -101,7 +176,13 @@ hadErrors: false
101
176
# ## formatString
102
177
103
178
The `format()` function requires a template string that includes placeholders for argument values.
104
- Placeholders use the format `{n}`, where `n` is the zero-based index of the argument to insert.
179
+
180
+ Placeholders use the zero-based index of the function arguments. You can reference the same
181
+ argument any number of times. If DSC can't resolve the placeholder index to an argument, DSC raises
182
+ an error.
183
+
184
+ For more information about the syntax for defining placeholders, see
185
+ [Placeholder syntax](#placeholder-syntax).
105
186
106
187
` ` ` yaml
107
188
Type: string
@@ -112,18 +193,18 @@ MaximumCount: 1
112
193
113
194
# ## arguments
114
195
115
- The function accepts one or more arguments of any type to insert into the formatted string.
196
+ The function accepts one or more arguments to insert into the formatted string.
116
197
117
198
` ` ` yaml
118
- Type: any
199
+ Type: [boolean, integer, string]
119
200
Required: true
120
201
MinimumCount: 1
121
- MaximumCount: unlimited
202
+ MaximumCount: 18446744073709551615
122
203
` ` `
123
204
124
205
# # Output
125
206
126
- The `format()` function returns a string where each placeholder in the ** formatString** is replaced
207
+ The `format()` function returns a string where each placeholder in the ` formatString` is replaced
127
208
with the corresponding argument value.
128
209
129
210
` ` ` yaml
0 commit comments