yq/pkg/yqlib/doc/Style.md

186 lines
2.1 KiB
Markdown
Raw Normal View History

2020-11-18 09:42:37 +00:00
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)
2020-11-22 02:16:54 +00:00
## Set tagged style
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
2020-11-18 09:42:37 +00:00
b: 5
c: 3.2
e: true
2020-11-17 22:44:16 +00:00
```
then
```bash
2020-11-18 09:42:37 +00:00
yq eval '.. style="tagged"' sample.yml
2020-11-17 22:44:16 +00:00
```
will output
```yaml
2020-11-18 09:42:37 +00:00
!!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
```
2020-11-22 02:16:54 +00:00
## Set double quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="double"' sample.yml
```
will output
```yaml
a: "cat"
b: "5"
c: "3.2"
e: "true"
```
2020-12-28 00:24:42 +00:00
## Set double quote style on map keys too
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '... style="double"' sample.yml
```
will output
```yaml
"a": "cat"
"b": "5"
"c": "3.2"
"e": "true"
```
2020-11-22 02:16:54 +00:00
## Set single quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="single"' sample.yml
```
will output
```yaml
a: 'cat'
b: '5'
c: '3.2'
e: 'true'
```
2020-11-22 02:16:54 +00:00
## Set literal quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="literal"' sample.yml
```
will output
```yaml
a: |-
cat
b: |-
5
c: |-
3.2
e: |-
true
```
2020-11-22 02:16:54 +00:00
## Set folded quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="folded"' sample.yml
```
will output
```yaml
a: >-
cat
b: >-
5
c: >-
3.2
e: >-
true
```
2020-11-22 02:16:54 +00:00
## Set flow quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="flow"' sample.yml
```
will output
```yaml
{a: cat, b: 5, c: 3.2, e: true}
```
2020-11-22 02:16:54 +00:00
## Pretty print
2020-12-29 11:32:06 +00:00
Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.
2020-11-22 02:16:54 +00:00
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
2020-12-28 00:24:42 +00:00
"b": 5
'c': 3.2
"e": true
2020-11-18 09:42:37 +00:00
```
then
```bash
2020-12-28 00:24:42 +00:00
yq eval '... style=""' sample.yml
2020-11-18 09:42:37 +00:00
```
will output
```yaml
a: cat
b: 5
c: 3.2
e: true
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Read style
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
2020-11-22 02:16:54 +00:00
{a: "cat", b: 'thing'}
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '.. | style' sample.yml
```
will output
```yaml
2020-11-22 02:16:54 +00:00
flow
double
single
2020-11-17 22:44:16 +00:00
```