Skip to content

Commit

Permalink
Add support for @file (#7)
Browse files Browse the repository at this point in the history
- Use `%-s`, `%-q`, `%*-s`, `%*-q`, `%**-s`, `%**-q` syntax to read from stdin.
- Use `%(NAME@FILE)` syntax to read default value from file.
- Use `%(NAME@-)` syntax to read default value from stdin.
- Use `NAME@FILE` to pass default value for named placeholder from file.
- Use `NAME@-` to pass default value for named placeholder from stdin.
- Stdin values are separated by null (`\0`).

Also, display better error for invalid expandable placeholder syntax.
  • Loading branch information
sayanarijit committed May 27, 2023
1 parent 96a8fcc commit b7fb344
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 139 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jf"
version = "0.3.3"
version = "0.4.0"
edition = "2021"
authors = ["Arijit Basu <hi@arijitbasu.in>"]
description = 'A small utility to safely format and print JSON objects in the commandline'
Expand Down
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ nix-env -f https://github.com/NixOS/nixpkgs/tarball/nixos-unstable -iA jf
### USAGE

```bash
jf TEMPLATE [VALUE]... [NAME=VALUE]...
jf TEMPLATE [VALUE]... [NAME=VALUE]... [NAME@FILE]...
```

Where TEMPLATE may contain the following placeholders:
Expand All @@ -46,26 +46,33 @@ Where TEMPLATE may contain the following placeholders:
- `%v` the `jf` version number
- `%%` a literal `%` character

And [VALUE]... [NAME=VALUE]... are the values for the placeholders.
And [VALUE]... [NAME=VALUE]... [NAME@FILE]... are the values for the placeholders.

### SYNTAX

- `%s` `%q` posiitonal placeholder
- `%(NAME)s` `%(NAME)q` named placeholder
- `%s` `%q` read positional argument
- `%-s` `%-q` read stdin
- `%(NAME)s` `%(NAME)q` read named value from argument
- `%(NAME=DEFAULT)s` `%(NAME=DEFAULT)q` placeholder with default value
- `%(NAME?)s` `%(NAME?)q` nullable placeholder that defaults to `null`
- `%(NAME@FILE)s` `%(NAME@FILE)q` read default value from file path
- `%(NAME@-)s` `%(NAME@-)q` read default value from stdin
- `%(NAME?)s` `%(NAME?)q` nullable placeholder that defaults to null
- `%(NAME)?s` `%(NAME)?q` optional placeholder that defaults to blank
- `%*s` `%*q` expand positional values as array items
- `%**s` `%**q` expand positional values as key value pairs
- `%(NAME)*s` `%(NAME)*q` expand named values as array items
- `%(NAME)**s` `%(NAME)**q` expand named values as key value pairs
- `%*s` `%*q` expand positional args as array items
- `%*-s` `%*-q` expand stdin as array items
- `%**s` `%**q` expand positional args as key value pairs
- `%**-s` `%**-q` expand stdin as key value pairs
- `%(NAME)*s` `%(NAME)*q` expand named args as array items
- `%(NAME)**s` `%(NAME)**q` expand named args as key value pairs

### RULES

- Pass values for positional placeholders in the same order as in the template.
- Pass values to stdin following the order and separate them with null byte (`\0`).
- Pass values for named placeholders using `NAME=VALUE` syntax.
- Pass values for named array items using `NAME=ITEM_N` syntax.
- Pass values for named key value pairs using `NAME=KEY_N NAME=VALUE_N` syntax.
- Use `NAME@FILE` syntax to read from file where FILE can be `-` for stdin.
- Do not declare or pass positional placeholders or values after named ones.
- Expandable positional placeholder should be the last placeholder in a template.

Expand All @@ -78,16 +85,15 @@ jf %s 1
jf %q 1
# "1"

jf [%*s] 1 2 3
# [1,2,3]

jf {%**q} one 1 two 2 three 3
jf '{%**q}' one 1 two 2 three 3
# {"one":"1","two":"2","three":"3"}

seq 1 3 | xargs printf '%s\0' | jf '[%*-s]'
# [1,2,3]

jf "{%q: %(value=default)q, %(bar)**q}" foo value=bar bar=biz bar=baz
# {"foo":"bar","biz":"baz"}


jf "{str or bool: %(str)?q %(bool)?s, nullable: %(nullable?)q}" str=true
# {"str or bool":"true","nullable":null}

Expand Down
50 changes: 37 additions & 13 deletions assets/jf.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.TH jf "1" "" ""
.SH USAGE

jf TEMPLATE [VALUE]\.\.\. [NAME=VALUE]\.\.\.
jf TEMPLATE [VALUE]\.\.\. [NAME=VALUE]\.\.\. [NAME@FILE]\.\.\.
.PP
Where TEMPLATE may contain the following placeholders:
.TP
Expand All @@ -22,56 +22,80 @@ the `jf` version number
`%%`
a literal `%` character
.PP
And [VALUE]\.\.\. [NAME=VALUE]\.\.\. are the values for the placeholders.
And [VALUE]\.\.\. [NAME=VALUE]\.\.\. [NAME@FILE]\.\.\. are the values for the placeholders.
.SH SYNTAX

.TP
.B
`%s`
`%q` posiitonal placeholder
`%q` read positional argument
.TP
.B
`%\fB-s\fP`
`%\fB-q\fP` read stdin
.TP
.B
`%(NAME)s`
`%(NAME)q` named placeholder
`%(NAME)q` read named value from argument
.TP
.B
`%(NAME=DEFAULT)s`
`%(NAME=DEFAULT)q` placeholder with default value
.TP
.B
`%(NAME@FILE)s`
`%(NAME@FILE)q` read default value from file path
.TP
.B
`%(NAME@-)s`
`%(NAME@-)q` read default value from stdin
.TP
.B
`%(NAME?)s`
`%(NAME?)q` nullable placeholder that defaults to `null`
`%(NAME?)q` nullable placeholder that defaults to null
.TP
.B
`%(NAME)?s`
`%(NAME)?q` optional placeholder that defaults to blank
.TP
.B
`%*s`
`%*q` expand positional values as array items
`%*q` expand positional args as array items
.TP
.B
`%*\fB-s\fP`
`%*\fB-q\fP` expand stdin as array items
.TP
.B
`%**s`
`%**q` expand positional values as key value pairs
`%**q` expand positional args as key value pairs
.TP
.B
`%**\fB-s\fP`
`%**\fB-q\fP` expand stdin as key value pairs
.TP
.B
`%(NAME)*s`
`%(NAME)*q` expand named values as array items
`%(NAME)*q` expand named args as array items
.TP
.B
`%(NAME)**s`
`%(NAME)**q` expand named values as key value pairs
`%(NAME)**q` expand named args as key value pairs
.SH RULES

.IP \(bu 3
Pass values for positional placeholders in the same order as in the template.
.IP \(bu 3
Pass values to stdin following the order and separate them with null byte (`\0`).
.IP \(bu 3
Pass values for named placeholders using `NAME=VALUE` syntax.
.IP \(bu 3
Pass values for named array items using `NAME=ITEM_N` syntax.
.IP \(bu 3
Pass values for named key value pairs using `NAME=KEY_N NAME=VALUE_N` syntax.
.IP \(bu 3
Use `NAME@FILE` syntax to read from file where FILE can be `-` for stdin.
.IP \(bu 3
Do not declare or pass positional placeholders or values after named ones.
.IP \(bu 3
Expandable positional placeholder should be the last placeholder in a template.
Expand All @@ -86,13 +110,13 @@ Run: jf %q 1
.IP \(bu 3
Out: "1"
.IP \(bu 3
Run: jf [%*s] 1 2 3
Run: jf '{%**q}' one 1 two 2 three 3
.IP \(bu 3
Out: [1,2,3]
Out: {"one":"1","two":"2","three":"3"}
.IP \(bu 3
Run: jf {%**q} one 1 two 2 three 3
Run: seq 1 3 | xargs printf '%s\0' | jf '[%*\fB-s\fP]'
.IP \(bu 3
Out: {"one":"1","two":"2","three":"3"}
Out: [1,2,3]
.IP \(bu 3
Run: jf "{%q: %(value=default)q, %(bar)**q}" foo value=bar bar=biz bar=baz
.IP \(bu 3
Expand Down
Loading

0 comments on commit b7fb344

Please sign in to comment.