Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support variable length args #4

Merged
merged 4 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.2.6"
version = "0.2.7"
edition = "2021"
authors = ["Arijit Basu <hi@arijitbasu.in>"]
description = 'A small utility to safely format and print JSON objects in the commandline'
Expand Down
33 changes: 11 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ nix-env -f https://github.com/NixOS/nixpkgs/tarball/nixos-unstable -iA jf

### USAGE

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

Expand All @@ -54,13 +54,16 @@ And [VALUE]... [NAME=VALUE]... are the values for the placeholders.
- `%(NAME)s`, `%(NAME)q` for named placeholders.
- `%(NAME=DEFAULT)s`, `%(NAME=DEFAULT)q` for placeholders with default values.
- `%?(NAME)s`, `%?(NAME)q` for optional placeholders.
- `%*s`, `%*q` for variable number of array items.
- `%**s`, `%**q` for variable number of key value pairs.

### RULES

- Pass values for positional placeholders in the same order as in the template.
- Pass values for named placeholders using `NAME=VALUE` syntax.
- Do not declare or pass positional placeholders or values after named ones.
- Nesting placeholders is prohibited.
- Variable length placeholder should be the last placeholder in a template.

### EXAMPLES

Expand All @@ -71,36 +74,22 @@ jf %s 1
jf %q 1
# "1"

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

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

jf "%q: %(value=default)q" foo value=bar
# {"foo":"bar"}

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

jf '{1: %s, two: %q, 3: %(3)s, four: %(four=4)q, "%%": %(pct)q}' 1 2 3=3 pct=100%
# {"1":1,"two":"2","3":3,"four":"4","%":"100%"}
```

#### vs jo

```bash
jf "hello: %q" world
# jo hello=world
# {"hello":"world"}

jf "hello: {beautiful: %(what)q}" what=world
# jo hello=$(jo beautiful=world)
# {"hello":{"beautiful":"world"}}

jf "d: {m: %s, n: %s}" 10 20
# jo d[m]=10 d[n]=20
# {"d":{"m":10,"n":20}}

jf "{a: {b: %s, c: {d: %s, f: %s}, d: {e: [%s, %q]}}, b: {e: [%q]}}" 0 1 true 2 sam hi
# jo -d\|first_char_only a\|b=0 a\|c\|d=1 a\|d\|e[]=2 a\|d\|e[]=sam a\|c[f]@1 b\|e[]=hi
# {"a":{"b":0,"c":{"d":1,"f":true},"d":{"e":[2,"sam"]}},"b":{"e":["hi"]}}
```

#### Rust Library

```rust
Expand Down
83 changes: 83 additions & 0 deletions assets/jf.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
.\" Text automatically generated by txt2man
.TH jf "1" "" ""

.SH USAGE

jf TEMPLATE [VALUE]\.\.\. [NAME=VALUE]\.\.\.
.PP
Where TEMPLATE may contain the following placeholders:
.TP
.B
`%q`
for quoted and safely escaped JSON string.
.TP
.B
`%s`
for JSON values other than string.
.TP
.B
`%v`
for the `jf` version number.
.TP
.B
`%%`
for a literal `%` character.
.PP
And [VALUE]\.\.\. [NAME=VALUE]\.\.\. are the values for the placeholders.
.SH SYNTAX

.TP
.B
`%s`, `%q`
for posiitonal placeholders.
.TP
.B
`%(NAME)s`, `%(NAME)q`
for named placeholders.
`%(NAME=DEFAULT)s`, `%(NAME=DEFAULT)q` for placeholders with default values.
.TP
.B
`%?(NAME)s`, `%?(NAME)q`
for optional placeholders.
.TP
.B
`%*s`, `%*q`
for variable number of array items.
.TP
.B
`%**s`, `%**q`
for variable number of 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 for named placeholders using `NAME=VALUE` syntax.
.IP \(bu 3
Do not declare or pass positional placeholders or values after named ones.
.IP \(bu 3
Nesting placeholders is prohibited.
.IP \(bu 3
Variable length placeholder should be the last placeholder in a template.
.SH EXAMPLES

jf %s 1
.SS # 1

jf %q 1
.SS # "1"

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

jf {%**q} one 1 two 2 three 3
# {"one":"1","two":"2","three":"3"}
.PP
jf "%q: %(value=default)q" foo value=bar
# {"foo":"bar"}
.PP
jf "{str_or_bool: %?(str)q %?(bool)s, optional: %?(optional)q}" str=true
# {"str_or_bool":"true","optional":null}
.PP
jf '{1: %s, two: %q, 3: %(3)s, four: %(four=4)q, "%%": %(pct)q}' 1 2 3=3 pct=100%
# {"1":1,"two":"2","3":3,"four":"4","%":"100%"}
53 changes: 53 additions & 0 deletions assets/jf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

USAGE

jf TEMPLATE [VALUE]... [NAME=VALUE]...

Where TEMPLATE may contain the following placeholders:

`%q` for quoted and safely escaped JSON string.
`%s` for JSON values other than string.
`%v` for the `jf` version number.
`%%` for a literal `%` character.

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

SYNTAX

`%s`, `%q` for posiitonal placeholders.
`%(NAME)s`, `%(NAME)q` for named placeholders.
`%(NAME=DEFAULT)s`, `%(NAME=DEFAULT)q` for placeholders with default values.
`%?(NAME)s`, `%?(NAME)q` for optional placeholders.
`%*s`, `%*q` for variable number of array items.
`%**s`, `%**q` for variable number of key value pairs.

RULES

* Pass values for positional placeholders in the same order as in the template.
* Pass values for named placeholders using `NAME=VALUE` syntax.
* Do not declare or pass positional placeholders or values after named ones.
* Nesting placeholders is prohibited.
* Variable length placeholder should be the last placeholder in a template.

EXAMPLES

jf %s 1
# 1

jf %q 1
# "1"

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

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

jf "%q: %(value=default)q" foo value=bar
# {"foo":"bar"}

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

jf '{1: %s, two: %q, 3: %(3)s, four: %(four=4)q, "%%": %(pct)q}' 1 2 3=3 pct=100%
# {"1":1,"two":"2","3":3,"four":"4","%":"100%"}
Loading