diff options
| -rw-r--r-- | arg_picker/src/picker/parse.rs | 8 | ||||
| -rw-r--r-- | arg_picker/src/picker/result.rs | 8 | ||||
| -rw-r--r-- | mingling/src/setups/picker/basic.rs | 50 |
3 files changed, 40 insertions, 26 deletions
diff --git a/arg_picker/src/picker/parse.rs b/arg_picker/src/picker/parse.rs index 366028b..9db5bd9 100644 --- a/arg_picker/src/picker/parse.rs +++ b/arg_picker/src/picker/parse.rs @@ -21,14 +21,16 @@ internal_repeat!(1..=32 => { internal_repeat!(1..=32 => { impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route> where (T$: Pickable<'a>,+) { - /// Unwraps the result, panicking if a route was selected. + /// Unwraps the result, panicking if a route was selected or a required + /// value is missing. /// /// # Panics /// - /// Panics if a route was selected. + /// Panics if a route was selected, or if a required argument was not + /// provided by the user. pub fn unwrap(self) -> ((T$,+)) { let p = self.parse(); - ((p.v$.unwrap(),+)) + ((p.v$.expect(concat!("missing required argument at position ", $)),+)) } /// Returns the individual option values without checking the route. diff --git a/arg_picker/src/picker/result.rs b/arg_picker/src/picker/result.rs index 83ca2cd..2099825 100644 --- a/arg_picker/src/picker/result.rs +++ b/arg_picker/src/picker/result.rs @@ -21,13 +21,15 @@ internal_repeat!(1..=32 => { internal_repeat!(1..=32 => { impl<(T$,+), Route> PickerResult$<(T$,+), Route> { - /// Unwraps the result, panicking if a route was selected. + /// Unwraps the result, panicking if a route was selected or a required + /// value is missing. /// /// # Panics /// - /// Panics if `self.route` is `Some(...)`. + /// Panics if `self.route` is `Some(...)`, or if a required argument was + /// not provided by the user. pub fn unwrap(self) -> ((T$,+)) { - ((self.v$.unwrap(),+)) + ((self.v$.expect(concat!("missing required argument at position ", $)),+)) } /// Returns the individual option values without checking the route. diff --git a/mingling/src/setups/picker/basic.rs b/mingling/src/setups/picker/basic.rs index 6a301c7..0539a09 100644 --- a/mingling/src/setups/picker/basic.rs +++ b/mingling/src/setups/picker/basic.rs @@ -3,6 +3,22 @@ use mingling_core::{Program, ProgramCollect, setup::ProgramSetup}; use crate::setups::picker::{CONFIRM_FLAG, HELP_FLAG, QUIET_FLAG}; +/// Helper: picks a boolean flag from the program arguments, calls `f` with the +/// flag value, then replaces the program arguments with the remaining args. +fn pick_flag<'a, C>( + program: &mut Program<C>, + flag: &PickerArg<'a, Flag>, + f: impl FnOnce(bool, &mut Program<C>), +) where + C: ProgramCollect<Enum = C>, +{ + let args = program.take_args(); + let remains_arg = PickerArg::<PickerArgs<'a>>::new(&[], None, true); + let (active, remains) = args.pick(flag).pick(&remains_arg).unwrap(); + f(*active, program); + program.replace_args(remains.into()); +} + /// Performs basic program initialization: /// /// - Collects `--quiet` flag to control message rendering @@ -40,11 +56,9 @@ where C: ProgramCollect<Enum = C>, { fn setup(self, program: &mut Program<C>) { - let args = program.take_args(); - let remains_arg = PickerArg::<PickerArgs<'a>>::new(&[], None, true); - let (active, remains) = args.pick(self.flag).pick(&remains_arg).unwrap(); - program.user_context.help = *active; - program.replace_args(remains.into()); + pick_flag(program, self.flag, |active, ctx| { + ctx.user_context.help = active; + }); } } @@ -73,14 +87,12 @@ where C: ProgramCollect<Enum = C>, { fn setup(self, program: &mut Program<C>) { - let args = program.take_args(); - let remains_arg = PickerArg::<PickerArgs<'a>>::new(&[], None, true); - let (active, remains) = args.pick(self.flag).pick(&remains_arg).unwrap(); - if *active { - program.stdout_setting.render_output = false; - program.stdout_setting.error_output = false; - } - program.replace_args(remains.into()); + pick_flag(program, self.flag, |active, ctx| { + if active { + ctx.stdout_setting.render_output = false; + ctx.stdout_setting.error_output = false; + } + }); } } @@ -109,13 +121,11 @@ where C: ProgramCollect<Enum = C>, { fn setup(self, program: &mut Program<C>) { - let args = program.take_args(); - let remains_arg = PickerArg::<PickerArgs<'a>>::new(&[], None, true); - let (active, remains) = args.pick(self.flag).pick(&remains_arg).unwrap(); - if *active { - program.user_context.confirm = true; - } - program.replace_args(remains.into()); + pick_flag(program, self.flag, |active, ctx| { + if active { + ctx.user_context.confirm = true; + } + }); } } |
