-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Against all wisdom, home-cooked. Where's the fun in using pre-built solutions? :)
- Loading branch information
Showing
3 changed files
with
150 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::{collections::BTreeSet, sync::LazyLock}; | ||
|
||
// XXX: This really needs proc macros to have any semblance of code-reuse. I | ||
// reckon that this isn't worth the effort; when the CLI gets more complicated | ||
// one should just accept the additional transitive dependencies and switch to | ||
// clap. | ||
|
||
pub static HELP: LazyLock<String> = LazyLock::new(|| { | ||
[ | ||
"rq: A tiny functional language to filter and manipulate JSON.", | ||
&USAGE, | ||
&OPTIONS, | ||
"POSITIONAL ARGUMENTS: | ||
«EXPR» A function in rq's expression language.", | ||
] | ||
.into_iter() | ||
.intersperse("\n\n") | ||
.collect() | ||
}); | ||
|
||
static USAGE: LazyLock<String> = LazyLock::new(|| { | ||
let flatten = CLI_OPTIONS.iter().find(|o| o.long == "--flatten").unwrap(); | ||
let help = CLI_OPTIONS.iter().find(|o| o.long == "--help").unwrap(); | ||
format!( | ||
"USAGE: | ||
rq [EXPR] < [JSON] | ||
rq {} < [JSON] | ||
rq {}", | ||
flatten.usage(), | ||
help.usage() | ||
) | ||
}); | ||
|
||
static OPTIONS: LazyLock<String> = LazyLock::new(|| { | ||
let opts: String = CLI_OPTIONS | ||
.iter() | ||
.map(|opt| opt.help()) | ||
.intersperse("\n".to_string()) | ||
.collect(); | ||
format!("OPTIONS:\n{opts}") | ||
}); | ||
|
||
#[derive(PartialEq, Eq, PartialOrd, Ord)] | ||
struct CliOption { | ||
long: String, | ||
short: String, | ||
help: String, | ||
} | ||
|
||
impl CliOption { | ||
fn usage(&self) -> String { format!("[{}|{}]", self.short, self.long) } | ||
|
||
fn help(&self) -> String { format!(" {},{} {}", self.short, self.long, self.help) } | ||
} | ||
|
||
macro_rules! mk_option { | ||
($long:literal, $short:literal, $help:literal $(,)?) => {{ | ||
CliOption { | ||
long: format!("--{}", $long), | ||
short: format!("-{}", $short), | ||
help: $help.to_string(), | ||
} | ||
}}; | ||
} | ||
|
||
macro_rules! mk_options { | ||
($(($long:literal, $short:literal, $help:literal $(,)?)),+ $(,)?) => { | ||
static CLI_OPTIONS: LazyLock<BTreeSet<CliOption>> = LazyLock::new (|| { | ||
BTreeSet::from([ | ||
$(mk_option!($long, $short, $help)),+ | ||
])}); | ||
}; | ||
} | ||
|
||
mk_options!( | ||
("help", "h", "Show this help text."), | ||
("flatten", "f", "Flatten the given JSON into a list.") | ||
); | ||
|
||
macro_rules! Help { | ||
() => { | ||
"--help" | "-h" | ||
}; | ||
} | ||
pub(crate) use Help; | ||
|
||
macro_rules! Flatten { | ||
() => { | ||
"--flatten" | "-f" | ||
}; | ||
} | ||
pub(crate) use Flatten; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters