diff --git a/src/lib.rs b/src/lib.rs index ac971cd..780f7ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,9 +69,6 @@ pub enum Argument { /// Defines how the arguments are parsed. /// -/// If a type `T` implements this trait, we can construct an `ArgumentIter`, -/// meaning that we can parse the individual arguments to `T`.\ -/// /// Usually, this trait will be implemented via the /// [derive macro](derive@Arguments) and does not need to be implemented /// manually. @@ -79,19 +76,7 @@ pub trait Arguments: Sized { /// The exit code to exit the program with on error. const EXIT_CODE: i32; - /// Parse an iterator of arguments into an - /// [`ArgumentIter`](ArgumentIter). - fn parse(args: I) -> ArgumentIter - where - I: IntoIterator, - I::Item: Into, - { - ArgumentIter::::from_args(args) - } - /// Parse the next argument from the lexopt parser. - /// - /// This method is called by [`ArgumentIter::next_arg`]. fn next_arg(parser: &mut lexopt::Parser) -> Result>, ErrorKind>; /// Print the help string for this command. @@ -111,7 +96,7 @@ pub trait Arguments: Sized { I: IntoIterator, I::Item: Into, { - let mut iter = Self::parse(args); + let mut iter = ArgumentIter::::from_args(args); while iter.next_arg()?.is_some() {} Ok(()) } @@ -121,10 +106,7 @@ pub trait Arguments: Sized { } /// An iterator over arguments. -/// -/// Can be constructed by calling [`Arguments::parse`]. Usually, this method -/// won't be used directly, but is used internally in [`Options::parse`]. -pub struct ArgumentIter { +struct ArgumentIter { parser: lexopt::Parser, positional_arguments: Vec, t: PhantomData, @@ -150,11 +132,11 @@ impl ArgumentIter { })? { match arg { Argument::Help => { - self.help().unwrap(); + T::help(self.parser.bin_name().unwrap()).unwrap(); std::process::exit(0); } Argument::Version => { - print!("{}", self.version()); + print!("{}", T::version()); std::process::exit(0); } Argument::Positional(arg) => { @@ -165,18 +147,6 @@ impl ArgumentIter { } Ok(None) } - - fn get_positional_arguments(self) -> Vec { - self.positional_arguments - } - - fn help(&self) -> std::io::Result<()> { - T::help(self.parser.bin_name().unwrap()) - } - - fn version(&self) -> String { - T::version() - } } /// Defines the app settings by consuming [`Arguments`]. @@ -187,9 +157,8 @@ impl ArgumentIter { /// - the [`apply`](Options::apply) method, which defines to how map that /// type onto the options. /// -/// By default, the [`Options::parse`] method will -/// 1. repeatedly call [`ArgumentIter::next_arg`] and call [`Options::apply`] -/// on the result until the arguments are exhausted. +/// By default, the [`Options::parse`] method iterate over the arguments and +/// call [`Options::apply`] on the result until the arguments are exhausted. pub trait Options: Sized { /// Apply a single argument to the options. fn apply(&mut self, arg: Arg); @@ -215,11 +184,11 @@ pub trait Options: Sized { #[cfg(not(feature = "parse-is-complete"))] { - let mut iter = Arg::parse(args); + let mut iter = ArgumentIter::::from_args(args); while let Some(arg) = iter.next_arg()? { self.apply(arg); } - Ok((self, iter.get_positional_arguments())) + Ok((self, iter.positional_arguments)) } }