Merge pull request #1 from gay-pizza/include-to-mod

Swap include macros to `using mod` with `pub use` & clippy fixes
This commit is contained in:
a dinosaur
2025-11-16 12:55:00 +11:00
committed by GitHub
6 changed files with 42 additions and 25 deletions

View File

@@ -17,7 +17,7 @@ impl Opts<&'static str> {
help: impl Fn(&str), error: impl FnOnce(&str, ParseError) help: impl Fn(&str), error: impl FnOnce(&str, ParseError)
) -> ParseMapResult { ) -> ParseMapResult {
let mut out: BTreeMap<&'static str, String> = BTreeMap::new(); let mut out: BTreeMap<&'static str, String> = BTreeMap::new();
match self.parse(&program_name, args, |_program_name, id, opt, _name, arg| { match self.parse(program_name, args, |_program_name, id, opt, _name, arg| {
if opt.is_help() { if opt.is_help() {
help(program_name); help(program_name);
Ok(ParseControl::Quit) Ok(ParseControl::Quit)

View File

@@ -3,6 +3,10 @@
* SPDX-License-Identifier: MIT OR Apache-2.0 * SPDX-License-Identifier: MIT OR Apache-2.0
*/ */
use crate::{Opt, Opts};
use crate::option::OptType;
use crate::options::RequiredParamsBitSet;
/// 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 {
@@ -25,7 +29,7 @@ pub enum ParseControl {
} }
/// 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>>; pub(crate) type HandlerResult<'a, T> = core::result::Result<T, ParseError<'a>>;
#[derive(Debug)] #[derive(Debug)]
pub enum ParseError<'a> { pub enum ParseError<'a> {

View File

@@ -3,6 +3,9 @@
* SPDX-License-Identifier: MIT OR Apache-2.0 * SPDX-License-Identifier: MIT OR Apache-2.0
*/ */
use crate::{Opt, Opts, ParseError};
use crate::option::{OptIdentifier, OptType};
/// Enough context to show full help text. /// Enough context to show full help text.
pub struct HelpWriterContext<'a, ID: 'static> { pub struct HelpWriterContext<'a, ID: 'static> {
pub options: &'a Opts<ID>, pub options: &'a Opts<ID>,
@@ -166,7 +169,7 @@ impl<ID> core::fmt::Display for StandardFullHelpWriter<'_, ID> {
} }
// Write line for option, with aligned help text if needed // Write line for option, with aligned help text if needed
let line = OptionUsageLine(&option); let line = OptionUsageLine(option);
if let Some(help_text) = option.help_string { if let Some(help_text) = option.help_string {
write!(f, " {line:.<align_width$} {help_text}")?; write!(f, " {line:.<align_width$} {help_text}")?;
} else { } else {

View File

@@ -8,10 +8,15 @@
mod const_utf8; mod const_utf8;
mod ordered_bitset; mod ordered_bitset;
include!("option.rs"); mod option;
include!("options.rs"); mod options;
include!("argparse.rs"); mod argparse;
include!("help.rs"); mod help;
pub use option::*;
pub use options::*;
pub use argparse::*;
pub use help::*;
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
pub mod alloc; pub mod alloc;

View File

@@ -3,15 +3,17 @@
* SPDX-License-Identifier: MIT OR Apache-2.0 * SPDX-License-Identifier: MIT OR Apache-2.0
*/ */
use crate::const_utf8;
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
enum OptType { pub(crate) enum OptType {
Positional, Positional,
Flag, Flag,
Value, Value,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
enum OptIdentifier { pub(crate) enum OptIdentifier {
Single(&'static str), Single(&'static str),
Multi(&'static[&'static str]), Multi(&'static[&'static str]),
} }
@@ -19,11 +21,11 @@ enum OptIdentifier {
/// Represents an option argument or positional argument to be parsed. /// Represents an option argument or positional argument to be parsed.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Opt<ID> { pub struct Opt<ID> {
id: ID, pub(crate) id: ID,
names: OptIdentifier, pub(crate) names: OptIdentifier,
value_name: Option<&'static str>, pub(crate) value_name: Option<&'static str>,
help_string: Option<&'static str>, pub(crate) help_string: Option<&'static str>,
r#type: OptType, pub(crate) r#type: OptType,
flags: OptFlag, flags: OptFlag,
} }
@@ -122,12 +124,12 @@ impl<ID> Opt<ID> {
} }
#[inline(always)] #[inline(always)]
const fn is_short_visible(&self) -> bool { pub(crate) const fn is_short_visible(&self) -> bool {
(self.flags.0 & OptFlag::VISIBLE_SHORT.0) != 0 (self.flags.0 & OptFlag::VISIBLE_SHORT.0) != 0
} }
#[inline(always)] #[inline(always)]
const fn is_full_visible(&self) -> bool { pub(crate) const fn is_full_visible(&self) -> bool {
(self.flags.0 & OptFlag::VISIBLE_FULL.0) != 0 (self.flags.0 & OptFlag::VISIBLE_FULL.0) != 0
} }
} }
@@ -161,7 +163,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> { pub(crate) 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);
if let Some(first) = chars.next() { if let Some(first) = chars.next() {
@@ -174,7 +176,7 @@ impl<ID: 'static> Opt<ID> {
false false
} }
match self.names { match self.names {
OptIdentifier::Single(name) => if predicate(&name) { Some(name) } else { None }, OptIdentifier::Single(name) => if predicate(name) { Some(name) } else { None },
// 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;
@@ -190,7 +192,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> { pub(crate) 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);
if let Some(first) = chars.next() { if let Some(first) = chars.next() {
@@ -203,7 +205,7 @@ impl<ID: 'static> Opt<ID> {
None None
} }
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;
@@ -219,7 +221,7 @@ impl<ID: 'static> Opt<ID> {
} }
/// Search for a matching name in the option, offset allows to skip the first `n = offset` 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> { pub(crate) fn match_name(&self, string: &str, offset: usize) -> Option<&'static str> {
let rhs = &string[offset..]; let rhs = &string[offset..];
if rhs.is_empty() { if rhs.is_empty() {
return None; return None;

View File

@@ -3,18 +3,21 @@
* SPDX-License-Identifier: MIT OR Apache-2.0 * SPDX-License-Identifier: MIT OR Apache-2.0
*/ */
use crate::{ordered_bitset, Opt};
use crate::option::OptType;
/// Static structure that contains instructions for parsing command-line arguments. /// Static structure that contains instructions for parsing command-line arguments.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Opts<ID: 'static> { pub struct Opts<ID: 'static> {
/// List of options /// List of options
options: &'static[Opt<ID>], pub(crate) options: &'static[Opt<ID>],
/// String containing single characters that match option prefixes /// String containing single characters that match option prefixes
flag_chars: &'static str, pub(crate) flag_chars: &'static str,
/// A description of what the program does /// A description of what the program does
description: Option<&'static str>, pub(crate) description: Option<&'static str>,
} }
type RequiredParamsBitSet = ordered_bitset::OrderedBitSet<u32, 4>; pub(crate) type RequiredParamsBitSet = ordered_bitset::OrderedBitSet<u32, 4>;
/// The maximum amount of allowed required non-positional options. /// The maximum amount of allowed required non-positional options.
pub const MAX_REQUIRED_OPTIONS: usize = RequiredParamsBitSet::CAPACITY; pub const MAX_REQUIRED_OPTIONS: usize = RequiredParamsBitSet::CAPACITY;