1+ #![ allow( clippy:: needless_return) ]
12//! # Lambda Args
23//! Lambda Args is a simple argument parser for Rust. It is designed to be
34//! simple to use and primarily for use in lambda command line applications.
@@ -7,6 +8,18 @@ use std::{
78 fmt,
89} ;
910
11+ /// Configurable command‑line argument parser for Lambda tools and examples.
12+ ///
13+ /// Features:
14+ /// - Long/short flags with aliases (e.g., "-o", "--output")
15+ /// - Positional arguments in declaration order
16+ /// - Type‑aware parsing (`string`, `integer`, `float`, `boolean`, `count`, lists)
17+ /// - Optional environment variable and config‑file integration
18+ /// - Mutually exclusive groups and simple requires relationships
19+ /// - Subcommands with their own parsers
20+ ///
21+ /// Use the builder to register arguments and then call `parse` with a slice of
22+ /// tokens (usually `std::env::args().collect()`).
1023pub struct ArgumentParser {
1124 name : String ,
1225 description : String ,
@@ -24,20 +37,32 @@ pub struct ArgumentParser {
2437}
2538
2639#[ derive( Debug , Copy , Clone , PartialEq , PartialOrd ) ]
40+ /// Supported value types for an argument definition.
2741pub enum ArgumentType {
42+ /// `true`/`false` (or implied by presence when compiled as a flag).
2843 Boolean ,
44+ /// 64‑bit signed integer.
2945 Integer ,
46+ /// 32‑bit floating point number.
3047 Float ,
48+ /// 64‑bit floating point number.
3149 Double ,
50+ /// UTF‑8 string.
3251 String ,
52+ /// Count of flag occurrences (e.g., `-vvv` => 3).
3353 Count ,
54+ /// One or more strings (space‑separated tokens).
3455 StringList ,
56+ /// One or more integers.
3557 IntegerList ,
58+ /// One or more 32‑bit floats.
3659 FloatList ,
60+ /// One or more 64‑bit floats.
3761 DoubleList ,
3862}
3963
4064#[ derive( Debug , Clone , PartialEq , PartialOrd ) ]
65+ /// Parsed value container used in results and defaults.
4166pub enum ArgumentValue {
4267 None ,
4368 Boolean ( bool ) ,
@@ -88,6 +113,7 @@ impl Into<f64> for ArgumentValue {
88113}
89114
90115#[ derive( Debug ) ]
116+ /// Declarative definition for a single CLI argument or positional parameter.
91117pub struct Argument {
92118 name : String ,
93119 description : String ,
@@ -99,7 +125,11 @@ pub struct Argument {
99125}
100126
101127impl Argument {
102- /// Creates a new argument where the name represents
128+ /// Create a new argument definition.
129+ ///
130+ /// The `name` should be the canonical identifier used in help/usage output
131+ /// (for example, `--output` or `--count`). For positional parameters call
132+ /// `as_positional()` when building.
103133 pub fn new ( name : & str ) -> Self {
104134 return Argument {
105135 name : name. to_string ( ) ,
@@ -118,13 +148,13 @@ impl Argument {
118148 return self ;
119149 }
120150
121- /// Sets the type for the ArgumentParser to parse the arguments value into .
151+ /// Sets the type the parser should use when converting the raw value.
122152 pub fn with_type ( mut self , arg_type : ArgumentType ) -> Self {
123153 self . arg_type = arg_type;
124154 return self ;
125155 }
126156
127- /// Sets the description (Help string) of the argument for
157+ /// Set a human‑readable description used in help/usage output.
128158 pub fn with_description ( mut self , description : & str ) -> Self {
129159 self . description = description. to_string ( ) ;
130160 return self ;
@@ -167,27 +197,33 @@ impl Argument {
167197 return self . arg_type . clone ( ) ;
168198 }
169199
200+ /// Canonical name used for matching and display (e.g., `--output`).
170201 pub fn name ( & self ) -> & str {
171202 return self . name . as_ref ( ) ;
172203 }
173204
205+ /// Default value used when the argument is not present.
174206 pub fn default_value ( & self ) -> ArgumentValue {
175207 return self . default_value . clone ( ) ;
176208 }
177209
210+ /// Description shown in `usage()` output.
178211 pub fn description ( & self ) -> & str {
179212 return self . description . as_ref ( ) ;
180213 }
181214
215+ /// Declared aliases that resolve to this argument (e.g., `-o`).
182216 pub fn aliases ( & self ) -> & Vec < String > {
183217 & self . aliases
184218 }
219+ /// Whether the argument is positional (no leading `-`/`--`).
185220 pub fn is_positional ( & self ) -> bool {
186221 self . positional
187222 }
188223}
189224
190225#[ derive( Debug , Clone ) ]
226+ /// A single parsed argument result as `(name, value)`.
191227pub struct ParsedArgument {
192228 name : String ,
193229 value : ArgumentValue ,
@@ -205,6 +241,7 @@ impl ParsedArgument {
205241 return self . name . clone ( ) ;
206242 }
207243
244+ /// Return a clone of the parsed value.
208245 pub fn value ( & self ) -> ArgumentValue {
209246 return self . value . clone ( ) ;
210247 }
@@ -240,17 +277,20 @@ impl ArgumentParser {
240277 return self . args . len ( ) ;
241278 }
242279
280+ /// Add an author string displayed in `usage()`.
243281 pub fn with_author ( mut self , author : & str ) -> Self {
244282 self . authors . push ( author. to_string ( ) ) ;
245283 self
246284 }
247285
248286 // TODO(vmarcella): Add description to the name
287+ /// Add a human‑readable description shown in `usage()`.
249288 pub fn with_description ( mut self , description : & str ) -> Self {
250289 self . description = description. to_string ( ) ;
251290 self
252291 }
253292
293+ /// Register an argument definition with the parser.
254294 pub fn with_argument ( mut self , argument : Argument ) -> Self {
255295 let idx = self . args . len ( ) ;
256296 let name = argument. name ( ) . to_string ( ) ;
@@ -264,7 +304,7 @@ impl ArgumentParser {
264304 return self ;
265305 }
266306
267- /// Set an environment variable prefix (e.g., "OBJ_LOADER").
307+ /// Set an environment variable prefix to read defaults (e.g., "OBJ_LOADER").
268308 pub fn with_env_prefix ( mut self , prefix : & str ) -> Self {
269309 self . env_prefix = Some ( prefix. to_string ( ) ) ;
270310 self
@@ -290,13 +330,13 @@ impl ArgumentParser {
290330 self
291331 }
292332
293- /// Merge values from a simple key=value config file (optional).
333+ /// Merge values from a simple ` key=value` config file (optional).
294334 pub fn with_config_file ( mut self , path : & str ) -> Self {
295335 self . config_path = Some ( path. to_string ( ) ) ;
296336 self
297337 }
298338
299- /// Add a subcommand parser.
339+ /// Add a subcommand parser which activates on a matching first token .
300340 pub fn with_subcommand ( mut self , mut sub : ArgumentParser ) -> Self {
301341 sub. is_subcommand = true ;
302342 let key = sub. name . clone ( ) ;
@@ -717,7 +757,7 @@ impl ArgumentParser {
717757 } )
718758 }
719759
720- /// Backwards- compatible panicking API. Prefer `parse` for non- panicking behavior .
760+ /// Backwards‑ compatible panicking API. Prefer `parse` for non‑ panicking use .
721761 pub fn compile ( self , args : & [ String ] ) -> Vec < ParsedArgument > {
722762 match self . parse ( args) {
723763 Ok ( parsed) => parsed. into_vec ( ) ,
@@ -791,16 +831,23 @@ fn parse_value(arg: &Argument, raw: &str) -> Result<ArgumentValue, ArgsError> {
791831}
792832
793833#[ derive( Debug ) ]
834+ /// Errors that may occur during argument parsing.
794835pub enum ArgsError {
836+ /// An unknown flag or option was encountered.
795837 UnknownArgument ( String ) ,
838+ /// The same flag/option was specified more than once when not allowed.
796839 DuplicateArgument ( String ) ,
840+ /// A flag/option expecting a value did not receive one.
797841 MissingValue ( String ) ,
842+ /// A provided value could not be parsed into the expected type.
798843 InvalidValue {
799844 name : String ,
800845 expected : String ,
801846 value : String ,
802847 } ,
848+ /// A required option or positional argument was not provided.
803849 MissingRequired ( String ) ,
850+ /// Help was requested; contains a preformatted usage string.
804851 HelpRequested ( String ) ,
805852}
806853
@@ -831,25 +878,28 @@ impl fmt::Display for ArgsError {
831878
832879impl std:: error:: Error for ArgsError { }
833880
834- /// A parsed arguments wrapper with typed getters.
881+ /// Parsed arguments with typed getters and subcommand support .
835882#[ derive( Debug , Clone ) ]
836883pub struct ParsedArgs {
837884 values : Vec < ParsedArgument > ,
838885 subcommand : Option < ( String , Box < ParsedArgs > ) > ,
839886}
840887
841888impl ParsedArgs {
889+ /// Convert into the raw underlying `(name, value)` vector.
842890 pub fn into_vec ( self ) -> Vec < ParsedArgument > {
843891 self . values
844892 }
845893
894+ /// True if the named argument is present (and not `None`).
846895 pub fn has ( & self , name : & str ) -> bool {
847896 self
848897 . values
849898 . iter ( )
850899 . any ( |p| p. name == name && !matches ! ( p. value, ArgumentValue :: None ) )
851900 }
852901
902+ /// Get a `String` value by name, if present and typed as string.
853903 pub fn get_string ( & self , name : & str ) -> Option < String > {
854904 self
855905 . values
@@ -861,6 +911,7 @@ impl ParsedArgs {
861911 } )
862912 }
863913
914+ /// Get an `i64` value by name, if present and typed as integer.
864915 pub fn get_i64 ( & self , name : & str ) -> Option < i64 > {
865916 self
866917 . values
@@ -872,6 +923,7 @@ impl ParsedArgs {
872923 } )
873924 }
874925
926+ /// Get an `f32` value by name, if present and typed as float.
875927 pub fn get_f32 ( & self , name : & str ) -> Option < f32 > {
876928 self
877929 . values
@@ -883,6 +935,7 @@ impl ParsedArgs {
883935 } )
884936 }
885937
938+ /// Get an `f64` value by name, if present and typed as double.
886939 pub fn get_f64 ( & self , name : & str ) -> Option < f64 > {
887940 self
888941 . values
@@ -894,6 +947,7 @@ impl ParsedArgs {
894947 } )
895948 }
896949
950+ /// Get a `bool` value by name, if present and typed as boolean.
897951 pub fn get_bool ( & self , name : & str ) -> Option < bool > {
898952 self
899953 . values
0 commit comments