diff options
Diffstat (limited to 'examples')
29 files changed, 79 insertions, 137 deletions
diff --git a/examples/example-argument-parse/src/main.rs b/examples/example-argument-parse/src/main.rs index eb07672..d7c8681 100644 --- a/examples/example-argument-parse/src/main.rs +++ b/examples/example-argument-parse/src/main.rs @@ -97,8 +97,6 @@ fn render_error_no_name_provided(_: ErrorNoNameProvided) -> RenderResult { gen_program!(); fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDTransfer); - program.with_dispatcher(CMDStrictTransfer); + let program = ThisProgram::new(); program.exec_and_exit(); } diff --git a/examples/example-argument-picker/src/main.rs b/examples/example-argument-picker/src/main.rs index 7fcc5db..36b552e 100644 --- a/examples/example-argument-picker/src/main.rs +++ b/examples/example-argument-picker/src/main.rs @@ -125,7 +125,6 @@ fn main() { program.with_resource(ResNumberDisplaySetting { round: *round }); // --------- IMPORTANT --------- - program.with_dispatcher(CMDCalculate); program.exec_and_exit(); } diff --git a/examples/example-async-support/src/main.rs b/examples/example-async-support/src/main.rs index af86408..1b6c194 100644 --- a/examples/example-async-support/src/main.rs +++ b/examples/example-async-support/src/main.rs @@ -30,8 +30,6 @@ use std::io::Write; async fn main() { let mut program = ThisProgram::new(); - program.with_dispatcher(CMDDownload); - // Add a hook to display when the download begins program.with_hook(ProgramHook::empty().on_begin::<_, ()>(|_| println!("Download begin"))); diff --git a/examples/example-basic/src/main.rs b/examples/example-basic/src/main.rs index 17077e2..418ed95 100644 --- a/examples/example-basic/src/main.rs +++ b/examples/example-basic/src/main.rs @@ -26,10 +26,7 @@ dispatcher!("greet", CMDGreet => EntryGreet); fn main() { // Create a new ThisProgram - let mut program = ThisProgram::new(); - - // Add the CMDGreet dispatcher - program.with_dispatcher(CMDGreet); + let program = ThisProgram::new(); // Run the program, then exit the process program.exec_and_exit(); diff --git a/examples/example-clap-binding/src/main.rs b/examples/example-clap-binding/src/main.rs index d25c5f3..449fee3 100644 --- a/examples/example-clap-binding/src/main.rs +++ b/examples/example-clap-binding/src/main.rs @@ -60,7 +60,6 @@ fn main() { // Capture Clap's help information and write to RenderResult // --------- IMPORTANT --------- - program.with_dispatcher(CMDGreet); program.exec_and_exit(); } diff --git a/examples/example-combine-pathf-metadata/src/main.rs b/examples/example-combine-pathf-metadata/src/main.rs index a68eac5..894e235 100644 --- a/examples/example-combine-pathf-metadata/src/main.rs +++ b/examples/example-combine-pathf-metadata/src/main.rs @@ -24,10 +24,7 @@ mod sub; use mingling::prelude::*; fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(sub::CMDHello); - program.with_dispatcher(sub::CMDDescription); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } gen_program!(); diff --git a/examples/example-command-macro/src/main.rs b/examples/example-command-macro/src/main.rs index 2bf932a..d080043 100644 --- a/examples/example-command-macro/src/main.rs +++ b/examples/example-command-macro/src/main.rs @@ -19,14 +19,7 @@ use mingling::{macros::buffer, picker::IntoPicker, prelude::*}; fn main() { - let mut program = ThisProgram::new(); - - // Import the dispatchers generated by the `#[command]` macro - program.with_dispatcher(CMDHelloWorld); - program.with_dispatcher(CMDGreetSomeone); - program.with_dispatcher(CMDGoodBye); - - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } pack!(ResultGreeting = String); diff --git a/examples/example-completion/src/main.rs b/examples/example-completion/src/main.rs index d65be49..29d6026 100644 --- a/examples/example-completion/src/main.rs +++ b/examples/example-completion/src/main.rs @@ -44,19 +44,11 @@ //! Hello, Alice, Alice, Alice! //! ``` -use mingling::{macros::suggest, prelude::*, ShellContext, Suggest}; +use mingling::{ShellContext, Suggest, macros::suggest, prelude::*}; use std::io::Write; fn main() { - let mut program = ThisProgram::new(); - - program.with_dispatcher(CMDGreet); - - // --------- IMPORTANT --------- - // The `comp` feature makes `gen_program!()` generate a CMDCompletion automatically - // It adds a hidden `__comp` subcommand for communication with the completion script - program.with_dispatcher(crate::CMDCompletion); - // --------- IMPORTANT --------- + let program = ThisProgram::new(); // TIP: Note that the completion script reads stdout, // so make sure no output is produced before the CMDCompletion is dispatched. diff --git a/examples/example-custom-pickable/src/main.rs b/examples/example-custom-pickable/src/main.rs index b163278..ece5d13 100644 --- a/examples/example-custom-pickable/src/main.rs +++ b/examples/example-custom-pickable/src/main.rs @@ -70,9 +70,7 @@ pub fn render_error_parse_address_failed(_: ErrorParseAddressFailed) -> RenderRe gen_program!(); fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDConnect); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } // Address conversion diff --git a/examples/example-dispatch-tree/src/main.rs b/examples/example-dispatch-tree/src/main.rs index 9f76f15..a25af2a 100644 --- a/examples/example-dispatch-tree/src/main.rs +++ b/examples/example-dispatch-tree/src/main.rs @@ -3,11 +3,9 @@ //! > This example will introduce how to use `dispatch_tree` //! > to optimize your command line lookup efficiency //! -//! When the number of commands in your project increases, you can use `dispatch_tree` to complete command registration at compile time. -//! It will generate a trie for quickly finding related commands by prefix. -//! -//! Therefore, after enabling this feature, -//! `Program` will no longer store a Dispatcher list internally, and the `with_dispatcher` function will not be compiled. +//! When the number of commands in your project increases, you can enable +//! `dispatch_tree` to switch command matching from a linear scan to a +//! character-level trie. //! //! Run: //! ```bash @@ -43,12 +41,6 @@ dispatcher!("nested.f", CMDD => EntryD); fn main() { let program = ThisProgram::new(); - - // --------- IMPORTANT --------- - // // You no longer need to use `with_dispatcher` anymore; - // // it'll be collected automatically once the `dispatch_tree` feature is enabled - // program.with_dispatcher(...); - program.exec_and_exit(); } diff --git a/examples/example-enum-tag/src/main.rs b/examples/example-enum-tag/src/main.rs index b57511d..884bc1d 100644 --- a/examples/example-enum-tag/src/main.rs +++ b/examples/example-enum-tag/src/main.rs @@ -100,8 +100,5 @@ fn complete_language_selection(_: &ShellContext) -> Suggest { gen_program!(); fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDCompletion); - program.with_dispatcher(CMDLanguageSelection); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } diff --git a/examples/example-error-handling/src/main.rs b/examples/example-error-handling/src/main.rs index 0cd973a..e9c0f57 100644 --- a/examples/example-error-handling/src/main.rs +++ b/examples/example-error-handling/src/main.rs @@ -108,7 +108,5 @@ fn render_entry_fallback(err: EntryFallback) -> RenderResult { gen_program!(); fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDHello); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } diff --git a/examples/example-exitcode/src/main.rs b/examples/example-exitcode/src/main.rs index 6b22eae..ad712cd 100644 --- a/examples/example-exitcode/src/main.rs +++ b/examples/example-exitcode/src/main.rs @@ -31,7 +31,6 @@ fn main() { program.with_setup(ExitCodeSetup::default()); // --------- IMPORTANT --------- - program.with_dispatcher(CMDHello); program.exec_and_exit(); } diff --git a/examples/example-help/src/main.rs b/examples/example-help/src/main.rs index 3282683..90619f5 100644 --- a/examples/example-help/src/main.rs +++ b/examples/example-help/src/main.rs @@ -36,8 +36,6 @@ fn main() { program.with_setup(BasicProgramSetup); // --------- IMPORTANT --------- - program.with_dispatcher(CMDGreet); - program.exec_and_exit(); } diff --git a/examples/example-hook/src/main.rs b/examples/example-hook/src/main.rs index da92045..1304f85 100644 --- a/examples/example-hook/src/main.rs +++ b/examples/example-hook/src/main.rs @@ -50,7 +50,6 @@ fn main() { ); // --------- IMPORTANT --------- - program.with_dispatcher(CMDGreet); program.exec_and_exit(); } diff --git a/examples/example-implicit-dispatcher/src/main.rs b/examples/example-implicit-dispatcher/src/main.rs index 340d585..69adcfe 100644 --- a/examples/example-implicit-dispatcher/src/main.rs +++ b/examples/example-implicit-dispatcher/src/main.rs @@ -9,15 +9,7 @@ dispatcher!("remote.add" /*, CMDRemoteAdd => EntryRemoteAdd */); dispatcher!("remote.remove", CMDRemoteRemove => EntryRemoteRemove); fn main() { - let mut program = ThisProgram::new(); - - // --------- IMPORTANT --------- - program.with_dispatcher(CMDRemoteAdd); - // ^^^^^^^^^^^^\_ CMDRemoteAdd is implicitly created - // --------- IMPORTANT --------- - - program.with_dispatcher(CMDRemoteRemove); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } gen_program!(); diff --git a/examples/example-lazy-resources/src/main.rs b/examples/example-lazy-resources/src/main.rs index 7dda658..199608e 100644 --- a/examples/example-lazy-resources/src/main.rs +++ b/examples/example-lazy-resources/src/main.rs @@ -64,7 +64,6 @@ fn main() { program.with_resource(ResLargeData::lazy_init(init_res_large_data)); // --------- IMPORTANT --------- - program.with_dispatcher(CMDShow).with_dispatcher(CMDNone); program.exec_and_exit(); } diff --git a/examples/example-metadata/src/main.rs b/examples/example-metadata/src/main.rs index 855241e..5395614 100644 --- a/examples/example-metadata/src/main.rs +++ b/examples/example-metadata/src/main.rs @@ -34,11 +34,7 @@ dispatcher!("desc", CMDDescription => EntryDescription); dispatcher!("nodoc", CMDNoDescription => EntryNoDescription); fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDGreet); - program.with_dispatcher(CMDDescription); - program.with_dispatcher(CMDNoDescription); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } /// The metadata type attached to an entry. diff --git a/examples/example-outside-type/src/main.rs b/examples/example-outside-type/src/main.rs index 3159d19..a04727f 100644 --- a/examples/example-outside-type/src/main.rs +++ b/examples/example-outside-type/src/main.rs @@ -90,10 +90,7 @@ fn render_error_io(err: ErrorIo) -> RenderResult { } fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDParse); - program.with_dispatcher(CMDError); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } gen_program!(); diff --git a/examples/example-pack-err/src/main.rs b/examples/example-pack-err/src/main.rs index 5bf5066..901072d 100644 --- a/examples/example-pack-err/src/main.rs +++ b/examples/example-pack-err/src/main.rs @@ -143,9 +143,9 @@ gen_program!(); fn main() { let mut program = ThisProgram::new(); + // Add StructuralRendererSetup to support --json / --yaml flags program.with_setup(StructuralRendererSetup); - program.with_dispatcher(CMDFind); - program.with_dispatcher(CMDFindStructural); + let _ = program.exec(); } diff --git a/examples/example-panic-unwind/src/main.rs b/examples/example-panic-unwind/src/main.rs index 2c432eb..867e684 100644 --- a/examples/example-panic-unwind/src/main.rs +++ b/examples/example-panic-unwind/src/main.rs @@ -24,7 +24,6 @@ pack!(NotPanic = ()); fn main() { let mut program = ThisProgram::new(); - program.with_dispatcher(CMDPanic); // --------- IMPORTANT --------- // Enable silence_panic to suppress automatic Panic output diff --git a/examples/example-pathfinder/src/main.rs b/examples/example-pathfinder/src/main.rs index 0f93a8d..cd7cc7d 100644 --- a/examples/example-pathfinder/src/main.rs +++ b/examples/example-pathfinder/src/main.rs @@ -19,12 +19,9 @@ mod sub; use mingling::macros::gen_program; -use crate::sub::CMDGreet; fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDGreet); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } gen_program!(); diff --git a/examples/example-repl-basic/src/main.rs b/examples/example-repl-basic/src/main.rs index 361488d..dc3d2fe 100644 --- a/examples/example-repl-basic/src/main.rs +++ b/examples/example-repl-basic/src/main.rs @@ -37,12 +37,6 @@ fn main() { // Resource program.with_resource(ResCurrentDir::default()); - // Dispatchers - program.with_dispatcher(CMDCd); - program.with_dispatcher(CMDLs); - program.with_dispatcher(CMDExit); - program.with_dispatcher(CMDClear); - // Setups // Enable basic std::io::stdin().read_line(&mut input) program.with_setup(BasicREPLReadlineSetup); diff --git a/examples/example-resources/src/main.rs b/examples/example-resources/src/main.rs index 38e4561..262680a 100644 --- a/examples/example-resources/src/main.rs +++ b/examples/example-resources/src/main.rs @@ -37,9 +37,6 @@ fn main() { }); // --------- IMPORTANT --------- - program - .with_dispatcher(CMDCurrent) - .with_dispatcher(CMDModifyCurrent); program.exec_and_exit(); } diff --git a/examples/example-setup/src/main.rs b/examples/example-setup/src/main.rs index bf610ca..b497054 100644 --- a/examples/example-setup/src/main.rs +++ b/examples/example-setup/src/main.rs @@ -1,8 +1,28 @@ //! Example Setup //! -//! > This example demonstrates how to build a custom Setup for modular management of project components +//! > This example demonstrates how to build a custom Setup that encapsulates a +//! > group of related resources and registers them with `with_resource`. use mingling::{Program, macros::program_setup, prelude::*}; +use std::io::Write; + +// A group of related resources — here, the demo app's identity. +// Resource types are plain structs: any `Default + Clone + Send + Sync` type +// can be used as a resource, and it is identified by its type. +#[derive(Default, Clone)] +struct ResAppName { + name: String, +} + +#[derive(Default, Clone)] +struct ResAppVersion { + version: String, +} + +#[derive(Default, Clone)] +struct ResGreetingPrefix { + prefix: String, +} fn main() { let mut program = ThisProgram::new(); @@ -17,21 +37,44 @@ fn main() { // --------- IMPORTANT --------- // Define `CustomSetup` (inferred from `custom_setup`) -// Package part of the program construction logic into this type for modular management +// Package part of the program construction logic into this type for modular +// management — e.g. register a group of related resources here. #[program_setup] fn custom_setup(program: &mut Program<ThisProgram>) { - program.with_dispatcher(CMD1); - program.with_dispatcher(CMD2); - program.with_dispatcher(CMD3); - program.with_dispatcher(CMD4); - program.with_dispatcher(CMD5); + program.with_resource(ResAppName { + name: "mingling".to_string(), + }); + program.with_resource(ResAppVersion { + version: "0.5.0".to_string(), + }); + program.with_resource(ResGreetingPrefix { + prefix: "Hello".to_string(), + }); } // --------- IMPORTANT --------- -dispatcher!("1", CMD1 => Entry1); -dispatcher!("2", CMD2 => Entry2); -dispatcher!("3", CMD3 => Entry3); -dispatcher!("4", CMD4 => Entry4); -dispatcher!("5", CMD5 => Entry5); +dispatcher!("greet", CMDGreet => EntryGreet); + +pack!(ResultGreeting = String); + +/// Chain: reads the `ResAppName` and `ResAppVersion` resources. +#[chain] +fn handle_greet(args: EntryGreet, app: &ResAppName, version: &ResAppVersion) -> Next { + let who = args + .inner + .first() + .cloned() + .unwrap_or_else(|| "World".to_string()); + let greeting: ResultGreeting = format!("{} from {} v{}", who, app.name, version.version).into(); + greeting.into() +} + +/// Renderer: injects the `ResGreetingPrefix` resource to decorate the output. +#[renderer] +fn render_greet(greeting: ResultGreeting, prefix: &ResGreetingPrefix) -> RenderResult { + let mut render_result = RenderResult::new(); + writeln!(render_result, "{}, {}!", prefix.prefix, *greeting).ok(); + render_result +} gen_program!(); diff --git a/examples/example-setup/test.toml b/examples/example-setup/test.toml index 811a108..c9b7ed8 100644 --- a/examples/example-setup/test.toml +++ b/examples/example-setup/test.toml @@ -1,29 +1,11 @@ [[runs]] -input = [ "1" ] +input = [ "greet" ] expect.exit-code = 0 -expect.result = "" +expect.result = "Hello, World from mingling v0.5.0!" [[runs]] -input = [ "2" ] +input = [ "greet", "Alice" ] expect.exit-code = 0 -expect.result = "" - -[[runs]] -input = [ "3" ] - -expect.exit-code = 0 -expect.result = "" - -[[runs]] -input = [ "4" ] - -expect.exit-code = 0 -expect.result = "" - -[[runs]] -input = [ "5" ] - -expect.exit-code = 0 -expect.result = "" +expect.result = "Hello, Alice from mingling v0.5.0!" diff --git a/examples/example-structural-renderer/src/main.rs b/examples/example-structural-renderer/src/main.rs index 070e75d..8583d46 100644 --- a/examples/example-structural-renderer/src/main.rs +++ b/examples/example-structural-renderer/src/main.rs @@ -28,7 +28,6 @@ fn main() { let mut program = ThisProgram::new(); // Add `StructuralRendererSetup` to receive user input `--json` `--yaml` parameters program.with_setup(StructuralRendererSetup); - program.with_dispatcher(CMDRender); let _ = program.exec(); } diff --git a/examples/example-unit-test/src/main.rs b/examples/example-unit-test/src/main.rs index 33ddde0..140df90 100644 --- a/examples/example-unit-test/src/main.rs +++ b/examples/example-unit-test/src/main.rs @@ -140,7 +140,5 @@ fn render_entry_fallback(err: EntryFallback) -> RenderResult { gen_program!(); fn main() { - let mut program = ThisProgram::new(); - program.with_dispatcher(CMDHello); - program.exec_and_exit(); + ThisProgram::new().exec_and_exit(); } diff --git a/examples/full-todolist/src/main.rs b/examples/full-todolist/src/main.rs index 0748832..c0df1df 100644 --- a/examples/full-todolist/src/main.rs +++ b/examples/full-todolist/src/main.rs @@ -65,12 +65,7 @@ fn main() { ); program.with_resource(ResProgramFlags { all }); - // Dispatchers - program.with_dispatcher(CMDAdd); - program.with_dispatcher(CMDComplete); - program.with_dispatcher(CMDList); - program.with_dispatcher(CMDClean); - + // Execute program.exec_and_exit(); } |
