diff --git a/Cargo.lock b/Cargo.lock index f5a0f08..5617754 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" [[package]] name = "jf" -version = "0.2.1" +version = "0.2.2" dependencies = [ "serde_json", "serde_yaml", diff --git a/Cargo.toml b/Cargo.toml index 3ac2592..e269af4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jf" -version = "0.2.1" +version = "0.2.2" edition = "2021" authors = ["Arijit Basu "] description = 'jf: %q "JSON Formatted"' diff --git a/README.md b/README.md index 673dc7e..e48cbd9 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ And [VALUE]... [[NAME=]VALUE]... are the values for the placeholders. - 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. +- To get the `jf` version number, use `jf %v`. Example: diff --git a/src/main.rs b/src/main.rs index f374cb2..dff2927 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use serde_json as json; use serde_yaml as yaml; use std::{collections::HashMap, env, fmt::Display}; +const VERSION: &str = env!("CARGO_PKG_VERSION"); const USAGE: &str = r#"not enough arguments USAGE: jf TEMPLATE [VALUE]... [NAME=VALUE]... @@ -19,6 +20,7 @@ USAGE: jf TEMPLATE [VALUE]... [NAME=VALUE]... 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. + To get the `jf` version number, use `jf %v`. EXAMPLE: @@ -176,6 +178,10 @@ where val.push_str(&arg); last_char = None; } + ('v', Some('%')) => { + val.push_str(&VERSION); + last_char = None; + } ('(', Some('%')) => { if !is_reading_named_placeholders { is_reading_named_placeholders = true; @@ -418,3 +424,18 @@ fn test_invalid_named_placeholder_error() { format!("jf: invalid named placeholder '%(foo)x' at column 6, use '%(foo)q' for quoted strings and '%(foo)s' for other values") ); } + +#[test] +fn test_print_version() { + let arg = ["jf v%v"].into_iter().map(Into::into); + assert_eq!(format(arg).unwrap().to_string(), r#""jf v0.2.2""#); + + let args = ["{foo: %q, bar: %(bar)q, version: %v}", "foo", "bar=bar"] + .into_iter() + .map(Into::into); + + assert_eq!( + format(args).unwrap().to_string(), + r#"{"foo":"foo","bar":"bar","version":"0.2.2"}"# + ); +}