aboutsummaryrefslogtreecommitdiff
path: root/examples/example-argument-parse
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-05-23 23:41:04 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-05-23 23:49:34 +0800
commit0a2ef958c0dca21d19e4ffc38ba5a7c4078e182a (patch)
treec82fc4242ed393b132ba514eb434d722e7d9c387 /examples/example-argument-parse
parentccab1940c019dfbfb7dfcbbe4cb927258933755f (diff)
Rework examples and add entry macro for testing
Diffstat (limited to 'examples/example-argument-parse')
-rw-r--r--examples/example-argument-parse/Cargo.lock83
-rw-r--r--examples/example-argument-parse/Cargo.toml10
-rw-r--r--examples/example-argument-parse/src/main.rs95
3 files changed, 188 insertions, 0 deletions
diff --git a/examples/example-argument-parse/Cargo.lock b/examples/example-argument-parse/Cargo.lock
new file mode 100644
index 0000000..b035436
--- /dev/null
+++ b/examples/example-argument-parse/Cargo.lock
@@ -0,0 +1,83 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "example-argument-parse"
+version = "0.1.0"
+dependencies = [
+ "mingling",
+]
+
+[[package]]
+name = "just_fmt"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e"
+
+[[package]]
+name = "mingling"
+version = "0.1.9"
+dependencies = [
+ "mingling_core",
+ "mingling_macros",
+ "size",
+]
+
+[[package]]
+name = "mingling_core"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+]
+
+[[package]]
+name = "mingling_macros"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.106"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "size"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b6709c7b6754dca1311b3c73e79fcce40dd414c782c66d88e8823030093b02b"
+
+[[package]]
+name = "syn"
+version = "2.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
diff --git a/examples/example-argument-parse/Cargo.toml b/examples/example-argument-parse/Cargo.toml
new file mode 100644
index 0000000..3b06523
--- /dev/null
+++ b/examples/example-argument-parse/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "example-argument-parse"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies.mingling]
+path = "../../mingling"
+
+# Enable `parser` features
+features = ["parser"]
diff --git a/examples/example-argument-parse/src/main.rs b/examples/example-argument-parse/src/main.rs
new file mode 100644
index 0000000..f63fdad
--- /dev/null
+++ b/examples/example-argument-parse/src/main.rs
@@ -0,0 +1,95 @@
+//! Example Argument Parse
+//!
+//! > This example demonstrates how to use the `parser` feature to parse user input
+//!
+//! Run:
+//! ```bash
+//! cargo run --manifest-path examples/example-argument-parse/Cargo.toml --quiet -- transfer README.md --size 32kib
+//! cargo run --manifest-path examples/example-argument-parse/Cargo.toml --quiet -- transfer src/ --dir
+//! cargo run --manifest-path examples/example-argument-parse/Cargo.toml --quiet -- strict-transfer README.md
+//! cargo run --manifest-path examples/example-argument-parse/Cargo.toml --quiet -- strict-transfer --dir
+//! ```
+//!
+//! Output:
+//! ```plaintext
+//! file: README.md (32768)
+//! dir: src/ (1048576)
+//! file: README.md (1048576)
+//! Error: name is not provided
+//! ```
+
+use mingling::{macros::route, prelude::*};
+
+dispatcher!("transfer", CMDTransfer => EntryTransfer);
+dispatcher!("strict-transfer", CMDStrictTransfer => EntryStrictTransfer);
+
+pack!(ResultFile = (bool, usize, String)); // (IsDir, Size, Name)
+
+#[chain]
+fn handle_transfer_parse(args: EntryTransfer) -> Next {
+ // --------- IMPORTANT ---------
+ // First parse flag arguments (like --dir/-D), then positional arguments
+ let result: ResultFile = args
+ // Name --dir --size 20mib
+ // ^^^^^^^^^^^^_ first
+ .pick::<bool>(["--dir", "-D"])
+ // Name --dir
+ // ^^^^^_ second (or `-D`)
+ .pick_or::<usize>("--size", 1024 * 1024_usize)
+ // Name
+ // ^^^^_ finally, pick positional arg
+ .pick::<String>(())
+ .after(|str| str.trim().replace(" ", ""))
+ // Unpack to tuple (is_dir, size, name)
+ .unpack()
+ // Convert into ResultFile
+ .into();
+ // --------- IMPORTANT ---------
+ result
+}
+
+pack!(ErrorNoNameProvided = ());
+
+#[chain]
+fn handle_strict_transfer_parse(args: EntryStrictTransfer) -> Next {
+ // --------- IMPORTANT ---------
+ // Strict parsing: error immediately if the name is not provided
+ let result: ResultFile = route! { // Use `route!` to wrap a Picker that contains `or_route`
+ args
+ .pick::<bool>(["--dir", "-D"])
+ .pick_or::<usize>("--size", 1024 * 1024_usize)
+ // Finally parse the positional argument; if not found, route to `ErrorNoNameProvided`
+ .pick_or_route::<String, _>((), ErrorNoNameProvided::default().to_chain())
+ .after(|str| str.trim().replace(" ", ""))
+ .unpack()
+ }
+ // Convert into ResultFile
+ .into();
+ // --------- IMPORTANT ---------
+ result.to_chain()
+}
+
+#[renderer]
+fn render_result_file(result: ResultFile) {
+ let (is_dir, size, name) = result.into();
+ r_println!(
+ "{}: {} ({})",
+ if is_dir { "dir" } else { "file" },
+ name,
+ size
+ )
+}
+
+#[renderer]
+fn render_error_no_name_provided(_: ErrorNoNameProvided) {
+ r_println!("Error: name is not provided")
+}
+
+gen_program!();
+
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_dispatcher(CMDTransfer);
+ program.with_dispatcher(CMDStrictTransfer);
+ program.exec_and_exit();
+}