From 938af23be9fa5274235778d2ad37db4b5fc0ffd4 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 28 Jun 2024 17:08:36 +0200 Subject: [PATCH] yeah, clap can be used without the env --- Cargo.lock | 7 +++++++ Cargo.toml | 2 +- members/clap-repl-demo/Cargo.toml | 7 +++++++ members/clap-repl-demo/src/main.rs | 12 ++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 members/clap-repl-demo/Cargo.toml create mode 100644 members/clap-repl-demo/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 9e36606..f31e457 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -304,6 +304,13 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-repl-demo" +version = "0.1.0" +dependencies = [ + "clap", +] + [[package]] name = "clap_builder" version = "4.5.7" diff --git a/Cargo.toml b/Cargo.toml index 5c58efe..0f995d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ members = [ "members/diesel-demo", "members/ptlog", "members/hello-world", - "members/statusline", + "members/statusline", "members/clap-repl-demo", ] default-members = [ ".", diff --git a/members/clap-repl-demo/Cargo.toml b/members/clap-repl-demo/Cargo.toml new file mode 100644 index 0000000..6f63e43 --- /dev/null +++ b/members/clap-repl-demo/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "clap-repl-demo" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5.7", features = ["derive"] } diff --git a/members/clap-repl-demo/src/main.rs b/members/clap-repl-demo/src/main.rs new file mode 100644 index 0000000..0beacf9 --- /dev/null +++ b/members/clap-repl-demo/src/main.rs @@ -0,0 +1,12 @@ +use clap::{arg, command, value_parser, ArgAction, Command}; + +fn main() { + // define a command with clap builder + let cmdtest = Command::new("test").arg(arg!(--foo ).required(true)); + + // instead of using the args that were actually given to the executable, we just insert our own + // vec + let simulated_input = vec!["test", "--foo", "5"]; + let parsed = cmdtest.try_get_matches_from(simulated_input); + dbg!(&parsed); +}