mirror of
https://github.com/gay-pizza/jaarg.git
synced 2025-12-19 07:20:18 +00:00
Fix glaring errors in docstrings
This commit is contained in:
@@ -6,15 +6,15 @@
|
|||||||
/// Enum describing the result of parsing arguments, and how the program should behave.
|
/// Enum describing the result of parsing arguments, and how the program should behave.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ParseResult {
|
pub enum ParseResult {
|
||||||
/// Parsing succeeded and program execution should continue
|
/// Parsing succeeded and program execution should continue.
|
||||||
ContinueSuccess,
|
ContinueSuccess,
|
||||||
/// Parsing succeeded and program should exit with success (eg; std::process::ExitCode::SUCCESS)
|
/// Parsing succeeded and program should exit with success (eg; [std::process::ExitCode::SUCCESS]).
|
||||||
ExitSuccess,
|
ExitSuccess,
|
||||||
/// There was an error while parsing and program should exit with failure (eg; std::process::ExitCode::FAILURE)
|
/// There was an error while parsing and program should exit with failure (eg; [std::process::ExitCode::FAILURE]).
|
||||||
ExitError,
|
ExitError,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execution control for the parser handler
|
/// Execution control for parser handlers.
|
||||||
pub enum ParseControl {
|
pub enum ParseControl {
|
||||||
/// Continue parsing arguments
|
/// Continue parsing arguments
|
||||||
Continue,
|
Continue,
|
||||||
@@ -24,7 +24,7 @@ pub enum ParseControl {
|
|||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Result type used by the handler passed to the parser
|
/// Result type used by the handler passed to the parser.
|
||||||
type HandlerResult<'a, T> = core::result::Result<T, ParseError<'a>>;
|
type HandlerResult<'a, T> = core::result::Result<T, ParseError<'a>>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -69,7 +69,7 @@ impl core::fmt::Display for ParseError<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience coercion for dealing with integer parsing errors
|
/// Convenience coercion for dealing with integer parsing errors.
|
||||||
impl From<core::num::ParseIntError> for ParseError<'_> {
|
impl From<core::num::ParseIntError> for ParseError<'_> {
|
||||||
fn from(err: core::num::ParseIntError) -> Self {
|
fn from(err: core::num::ParseIntError) -> Self {
|
||||||
use core::num::IntErrorKind;
|
use core::num::IntErrorKind;
|
||||||
@@ -83,7 +83,7 @@ impl From<core::num::ParseIntError> for ParseError<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience coercion for dealing with floating-point parsing errors
|
/// Convenience coercion for dealing with floating-point parsing errors.
|
||||||
impl From<core::num::ParseFloatError> for ParseError<'_> {
|
impl From<core::num::ParseFloatError> for ParseError<'_> {
|
||||||
fn from(_err: core::num::ParseFloatError) -> Self {
|
fn from(_err: core::num::ParseFloatError) -> Self {
|
||||||
// HACK: The empty option & argument fields will be fixed up by the parser
|
// HACK: The empty option & argument fields will be fixed up by the parser
|
||||||
@@ -94,7 +94,7 @@ impl From<core::num::ParseFloatError> for ParseError<'_> {
|
|||||||
|
|
||||||
impl core::error::Error for ParseError<'_> {}
|
impl core::error::Error for ParseError<'_> {}
|
||||||
|
|
||||||
/// Internal state tracked by the parser
|
/// Internal state tracked by the parser.
|
||||||
struct ParserState<ID: 'static> {
|
struct ParserState<ID: 'static> {
|
||||||
positional_index: usize,
|
positional_index: usize,
|
||||||
expects_arg: Option<(&'static str, &'static Opt<ID>)>,
|
expects_arg: Option<(&'static str, &'static Opt<ID>)>,
|
||||||
@@ -112,7 +112,7 @@ impl<ID> Default for ParserState<ID> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<ID: 'static> Opts<ID> {
|
impl<ID: 'static> Opts<ID> {
|
||||||
/// Parse an iterator of strings as arguments
|
/// Parses an iterator of strings as argument tokens.
|
||||||
pub fn parse<'a, S: AsRef<str> + 'a, I: Iterator<Item = S>>(&self, program_name: &str, args: I,
|
pub fn parse<'a, S: AsRef<str> + 'a, I: Iterator<Item = S>>(&self, program_name: &str, args: I,
|
||||||
mut handler: impl FnMut(&str, &ID, &Opt<ID>, &str, &str) -> HandlerResult<'a, ParseControl>,
|
mut handler: impl FnMut(&str, &ID, &Opt<ID>, &str, &str) -> HandlerResult<'a, ParseControl>,
|
||||||
error: impl FnOnce(&str, ParseError),
|
error: impl FnOnce(&str, ParseError),
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ enum OptIdentifier {
|
|||||||
Multi(&'static[&'static str]),
|
Multi(&'static[&'static str]),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents an option argument or positional argument to be parsed
|
/// Represents an option argument or positional argument to be parsed.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Opt<ID> {
|
pub struct Opt<ID> {
|
||||||
id: ID,
|
id: ID,
|
||||||
@@ -48,20 +48,20 @@ impl<ID> Opt<ID> {
|
|||||||
Self { id, names, value_name, help_string: None, r#type, flags: OptFlag::NONE }
|
Self { id, names, value_name, help_string: None, r#type, flags: OptFlag::NONE }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A positional argument that is parsed sequentially without being invoked by an option flag
|
/// A positional argument that is parsed sequentially without being invoked by an option flag.
|
||||||
pub const fn positional(id: ID, name: &'static str) -> Self {
|
pub const fn positional(id: ID, name: &'static str) -> Self {
|
||||||
Self::new(id, OptIdentifier::Single(name), None, OptType::Positional)
|
Self::new(id, OptIdentifier::Single(name), None, OptType::Positional)
|
||||||
}
|
}
|
||||||
/// A flag-type option that serves as the interface's help flag
|
/// A flag-type option that serves as the interface's help flag.
|
||||||
pub const fn help_flag(id: ID, names: &'static[&'static str]) -> Self {
|
pub const fn help_flag(id: ID, names: &'static[&'static str]) -> Self {
|
||||||
Self::new(id, OptIdentifier::Multi(names), None, OptType::Flag)
|
Self::new(id, OptIdentifier::Multi(names), None, OptType::Flag)
|
||||||
.with_help_flag()
|
.with_help_flag()
|
||||||
}
|
}
|
||||||
/// A flag-type option, takes no value
|
/// A flag-type option, takes no value.
|
||||||
pub const fn flag(id: ID, names: &'static[&'static str]) -> Self {
|
pub const fn flag(id: ID, names: &'static[&'static str]) -> Self {
|
||||||
Self::new(id, OptIdentifier::Multi(names), None, OptType::Flag)
|
Self::new(id, OptIdentifier::Multi(names), None, OptType::Flag)
|
||||||
}
|
}
|
||||||
/// An option argument that takes a value
|
/// An option argument that takes a value.
|
||||||
pub const fn value(id: ID, names: &'static[&'static str], value_name: &'static str) -> Self {
|
pub const fn value(id: ID, names: &'static[&'static str], value_name: &'static str) -> Self {
|
||||||
Self::new(id, OptIdentifier::Multi(names), Some(value_name), OptType::Value)
|
Self::new(id, OptIdentifier::Multi(names), Some(value_name), OptType::Value)
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,7 @@ impl<ID> Opt<ID> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<ID: 'static> Opt<ID> {
|
impl<ID: 'static> Opt<ID> {
|
||||||
/// Get the first name of the option
|
/// Get the first name of the option.
|
||||||
const fn first_name(&self) -> &str {
|
const fn first_name(&self) -> &str {
|
||||||
match self.names {
|
match self.names {
|
||||||
OptIdentifier::Single(name) => name,
|
OptIdentifier::Single(name) => name,
|
||||||
@@ -101,7 +101,7 @@ impl<ID: 'static> Opt<ID> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the first long option name, if one exists
|
/// Get the first long option name, if one exists.
|
||||||
const fn first_long_name(&self) -> Option<&'static str> {
|
const fn first_long_name(&self) -> Option<&'static str> {
|
||||||
match self.names {
|
match self.names {
|
||||||
OptIdentifier::Single(name) => if name.len() >= 3 { Some(name) } else { None },
|
OptIdentifier::Single(name) => if name.len() >= 3 { Some(name) } else { None },
|
||||||
@@ -119,7 +119,7 @@ impl<ID: 'static> Opt<ID> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the first short option name, if one exists
|
/// Get the first short option name, if one exists.
|
||||||
const fn first_short_name(&self) -> Option<&'static str> {
|
const fn first_short_name(&self) -> Option<&'static str> {
|
||||||
const fn predicate(name: &str) -> bool {
|
const fn predicate(name: &str) -> bool {
|
||||||
let mut chars = const_utf8::CharIterator::from(name);
|
let mut chars = const_utf8::CharIterator::from(name);
|
||||||
@@ -148,7 +148,7 @@ impl<ID: 'static> Opt<ID> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the first applicable short option's flag character, if one exists
|
/// Get the first applicable short option's flag character, if one exists.
|
||||||
const fn first_short_name_char(&self) -> Option<char> {
|
const fn first_short_name_char(&self) -> Option<char> {
|
||||||
const fn predicate(name: &str) -> Option<char> {
|
const fn predicate(name: &str) -> Option<char> {
|
||||||
let mut chars = const_utf8::CharIterator::from(name);
|
let mut chars = const_utf8::CharIterator::from(name);
|
||||||
@@ -163,7 +163,7 @@ impl<ID: 'static> Opt<ID> {
|
|||||||
}
|
}
|
||||||
match self.names {
|
match self.names {
|
||||||
OptIdentifier::Single(name) => predicate(&name),
|
OptIdentifier::Single(name) => predicate(&name),
|
||||||
// Can be replaced with `find_map` once iterators are const fn
|
// Can be replaced with `find_map` once iterators are const fn.
|
||||||
OptIdentifier::Multi(names) => {
|
OptIdentifier::Multi(names) => {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < names.len() {
|
while i < names.len() {
|
||||||
@@ -177,7 +177,7 @@ impl<ID: 'static> Opt<ID> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Search for a matching name in the option, offset allows to skip the first characters in the comparison
|
/// Search for a matching name in the option, offset allows to skip the first `n = offset` characters in the comparison.
|
||||||
fn match_name(&self, string: &str, offset: usize) -> Option<&'static str> {
|
fn match_name(&self, string: &str, offset: usize) -> Option<&'static str> {
|
||||||
match self.names {
|
match self.names {
|
||||||
OptIdentifier::Single(name) =>
|
OptIdentifier::Single(name) =>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// Static structure that contains instructions for parsing command-line arguments
|
/// Static structure that contains instructions for parsing command-line arguments.
|
||||||
pub struct Opts<ID: 'static> {
|
pub struct Opts<ID: 'static> {
|
||||||
/// List of options
|
/// List of options
|
||||||
options: &'static[Opt<ID>],
|
options: &'static[Opt<ID>],
|
||||||
@@ -19,7 +19,7 @@ type RequiredParamsBitSet = ordered_bitset::OrderedBitSet<u32, 4>;
|
|||||||
pub const MAX_REQUIRED_OPTIONS: usize = RequiredParamsBitSet::CAPACITY;
|
pub const MAX_REQUIRED_OPTIONS: usize = RequiredParamsBitSet::CAPACITY;
|
||||||
|
|
||||||
impl<ID: 'static> Opts<ID> {
|
impl<ID: 'static> Opts<ID> {
|
||||||
/// Build argument parser options with the default flag character of '-'
|
/// Build argument parser options with the default flag character of '-'.
|
||||||
pub const fn new(options: &'static[Opt<ID>]) -> Self {
|
pub const fn new(options: &'static[Opt<ID>]) -> Self {
|
||||||
// Validate passed options
|
// Validate passed options
|
||||||
let mut opt_idx = 0;
|
let mut opt_idx = 0;
|
||||||
|
|||||||
26
src/std.rs
26
src/std.rs
@@ -13,34 +13,34 @@ use std::string::String;
|
|||||||
use std::{env, eprintln, println};
|
use std::{env, eprintln, println};
|
||||||
|
|
||||||
impl<ID: 'static> Opts<ID> {
|
impl<ID: 'static> Opts<ID> {
|
||||||
/// Wrapper around `jaarg::parse` that gathers arguments from the command line and prints errors to stderr.
|
/// Wrapper around [Opts::parse] that gathers arguments from the command line and prints errors to stderr.
|
||||||
/// The errors are formatted in a standard user-friendly format.
|
/// The errors are formatted in a standard user-friendly format.
|
||||||
///
|
///
|
||||||
/// Requires features = [std]
|
/// Requires `features = [std]`.
|
||||||
pub fn parse_easy<'a>(&self, handler: impl FnMut(&str, &ID, &Opt<ID>, &str, &str) -> HandlerResult<'a, ParseControl>
|
pub fn parse_easy<'a>(&self, handler: impl FnMut(&str, &ID, &Opt<ID>, &str, &str) -> HandlerResult<'a, ParseControl>
|
||||||
) -> ParseResult {
|
) -> ParseResult {
|
||||||
let (program_name, argv) = Self::easy_args();
|
let (program_name, argv) = Self::easy_args();
|
||||||
self.parse(&program_name, argv, handler, |name, e| self.easy_error(name, e))
|
self.parse(&program_name, argv, handler, |name, e| self.easy_error(name, e))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints full help text for the options using the standard full
|
/// Prints full help text for the options using the standard full.
|
||||||
///
|
///
|
||||||
/// Requires features = [std]
|
/// Requires `features = [std]`.
|
||||||
pub fn print_full_help(&self, program_name: &str) {
|
pub fn print_full_help(&self, program_name: &str) {
|
||||||
self.print_help::<StandardFullHelpWriter<'_, ID>>(program_name);
|
self.print_help::<StandardFullHelpWriter<'_, ID>>(program_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Print help text to stdout using the provided help writer
|
/// Print help text to stdout using the provided help writer.
|
||||||
///
|
///
|
||||||
/// Requires features = [std]
|
/// Requires `features = [std]`.
|
||||||
pub fn print_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
|
pub fn print_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
|
||||||
let ctx = HelpWriterContext { options: self, program_name };
|
let ctx = HelpWriterContext { options: self, program_name };
|
||||||
println!("{}", W::new(ctx));
|
println!("{}", W::new(ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Print help text to stderr using the provided help writer
|
/// Print help text to stderr using the provided help writer.
|
||||||
///
|
///
|
||||||
/// Requires features = [std]
|
/// Requires `features = [std]`.
|
||||||
pub fn eprint_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
|
pub fn eprint_help<'a, W: HelpWriter<'a, ID>>(&'a self, program_name: &'a str) {
|
||||||
let ctx = HelpWriterContext { options: self, program_name };
|
let ctx = HelpWriterContext { options: self, program_name };
|
||||||
eprintln!("{}", W::new(ctx));
|
eprintln!("{}", W::new(ctx));
|
||||||
@@ -63,16 +63,16 @@ impl<ID: 'static> Opts<ID> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The result of parsing commands using `jaarg::Opts::parse_map`.
|
/// The result of parsing commands with [Opts::parse_map].
|
||||||
pub enum ParseMapResult {
|
pub enum ParseMapResult {
|
||||||
Map(BTreeMap<&'static str, String>),
|
Map(BTreeMap<&'static str, String>),
|
||||||
Exit(std::process::ExitCode),
|
Exit(std::process::ExitCode),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Opts<&'static str> {
|
impl Opts<&'static str> {
|
||||||
/// Parse an iterator of strings as arguments and return the results in a BTreeMap.
|
/// Parse an iterator of strings as arguments and return the results in a [BTreeMap].
|
||||||
///
|
///
|
||||||
/// Requires features = [std]
|
/// Requires `features = [std]`.
|
||||||
pub fn parse_map<'a, S: AsRef<str> + 'a, I: Iterator<Item = S>>(&self, program_name: &str, args: I,
|
pub fn parse_map<'a, S: AsRef<str> + 'a, I: Iterator<Item = S>>(&self, program_name: &str, args: I,
|
||||||
help: impl Fn(&str), error: impl FnOnce(&str, ParseError)
|
help: impl Fn(&str), error: impl FnOnce(&str, ParseError)
|
||||||
) -> ParseMapResult {
|
) -> ParseMapResult {
|
||||||
@@ -92,10 +92,10 @@ impl Opts<&'static str> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse arguments from the command line and return the results in a BTreeMap.
|
/// Parse arguments from the command line and return the results in a [BTreeMap].
|
||||||
/// Help and errors are formatted in a standard user-friendly format.
|
/// Help and errors are formatted in a standard user-friendly format.
|
||||||
///
|
///
|
||||||
/// Requires features = [std]
|
/// Requires `features = [std]`.
|
||||||
pub fn parse_map_easy(&self) -> ParseMapResult {
|
pub fn parse_map_easy(&self) -> ParseMapResult {
|
||||||
let (program_name, argv) = Self::easy_args();
|
let (program_name, argv) = Self::easy_args();
|
||||||
self.parse_map(&program_name, argv,
|
self.parse_map(&program_name, argv,
|
||||||
|
|||||||
Reference in New Issue
Block a user