diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2024-04-24 20:40:47 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2024-04-24 20:40:47 +0200 |
| commit | 5548c9f4a234db1819cf912d42664b5eb83c2de9 (patch) | |
| tree | 6d15bf3d196da18ae10267951121dd27c2c22e2c | |
| parent | 1a0fde95fc2403041f9f477f4220618ee916853b (diff) | |
Format with rustfmt
| -rw-r--r-- | src/args.rs | 87 | ||||
| -rw-r--r-- | src/main.rs | 16 |
2 files changed, 61 insertions, 42 deletions
diff --git a/src/args.rs b/src/args.rs index 3cd9d8a..31bd470 100644 --- a/src/args.rs +++ b/src/args.rs @@ -93,17 +93,19 @@ pub struct Arguments { } pub trait Parser { - fn run(&self, options: &mut Options, args: impl IntoIterator<Item=String>) -> Result<Arguments, String>; + fn run( + &self, + options: &mut Options, + args: impl IntoIterator<Item = String>, + ) -> Result<Arguments, String>; fn print_help(&self, options: &Options); } #[allow(dead_code)] -pub struct LongOnlyParser { -} +pub struct LongOnlyParser {} #[allow(dead_code)] -pub struct ShortAndLongParser { -} +pub struct ShortAndLongParser {} fn print_list(list: Vec<(String, &str)>) { let mut left_len: usize = 0; @@ -118,12 +120,16 @@ fn print_list(list: Vec<(String, &str)>) { #[allow(dead_code)] impl LongOnlyParser { pub fn new() -> LongOnlyParser { - LongOnlyParser { } + LongOnlyParser {} } } impl Parser for LongOnlyParser { - fn run(&self, options: &mut Options, args: impl IntoIterator<Item=String>) -> Result<Arguments, String> { + fn run( + &self, + options: &mut Options, + args: impl IntoIterator<Item = String>, + ) -> Result<Arguments, String> { let mut ret = Vec::new(); let mut args_iter = args.into_iter(); let program = args_iter.next(); @@ -134,7 +140,7 @@ impl Parser for LongOnlyParser { while let Some(arg) = args_iter.next() { ret.push(arg) } - break + break; } let start = 1; let name; @@ -151,22 +157,22 @@ impl Parser for LongOnlyParser { match option.value_req { ValueRequirement::None => { if value.is_some() { - return Err(format!("option '{}' doesn't allow an argument", arg)) + return Err(format!("option '{}' doesn't allow an argument", arg)); } - }, + } ValueRequirement::Required(_) => { if value.is_none() { value = args_iter.next(); if value.is_none() { - return Err(format!("option '{}' requires an argument", arg)) + return Err(format!("option '{}' requires an argument", arg)); } } - }, - ValueRequirement::Optional(_) => {}, + } + ValueRequirement::Optional(_) => {} } option.set(value); } else { - return Err(format!("unrecognized option '{}'", arg)) + return Err(format!("unrecognized option '{}'", arg)); } } else { ret.push(arg) @@ -193,12 +199,16 @@ impl Parser for LongOnlyParser { #[allow(dead_code)] impl ShortAndLongParser { pub fn new() -> ShortAndLongParser { - ShortAndLongParser { } + ShortAndLongParser {} } } impl Parser for ShortAndLongParser { - fn run(&self, options: &mut Options, args: impl IntoIterator<Item=String>) -> Result<Arguments, String> { + fn run( + &self, + options: &mut Options, + args: impl IntoIterator<Item = String>, + ) -> Result<Arguments, String> { let mut ret = Vec::new(); let mut args_iter = args.into_iter(); let program = args_iter.next(); @@ -209,7 +219,7 @@ impl Parser for ShortAndLongParser { while let Some(arg) = args_iter.next() { ret.push(arg) } - break + break; } let start = 2; let name; @@ -226,22 +236,22 @@ impl Parser for ShortAndLongParser { match option.value_req { ValueRequirement::None => { if value.is_some() { - return Err(format!("option '{}' doesn't allow an argument", arg)) + return Err(format!("option '{}' doesn't allow an argument", arg)); } - }, + } ValueRequirement::Required(_) => { if value.is_none() { value = args_iter.next(); if value.is_none() { - return Err(format!("option '{}' requires an argument", arg)) + return Err(format!("option '{}' requires an argument", arg)); } } - }, - ValueRequirement::Optional(_) => {}, + } + ValueRequirement::Optional(_) => {} } option.set(value); } else { - return Err(format!("unrecognized option '{}'", arg)) + return Err(format!("unrecognized option '{}'", arg)); } } else if arg.starts_with("-") && arg.len() > 1 { for c in arg.get(1..).unwrap().chars() { @@ -249,18 +259,18 @@ impl Parser for ShortAndLongParser { let ref mut option = options.options[*index]; let mut value = None; match option.value_req { - ValueRequirement::None => {}, + ValueRequirement::None => {} ValueRequirement::Required(_) => { value = args_iter.next(); if value.is_none() { - return Err(format!("option requires an argument -- '{}", c)) + return Err(format!("option requires an argument -- '{}", c)); } - }, - ValueRequirement::Optional(_) => {}, + } + ValueRequirement::Optional(_) => {} } option.set(value); } else { - return Err(format!("invalid option -- '{}'", c)) + return Err(format!("invalid option -- '{}'", c)); } } } else { @@ -277,25 +287,26 @@ impl Parser for ShortAndLongParser { if option.short() == '\0' { left = match option.value_req { ValueRequirement::None => format!(" --{}", option.long()), - ValueRequirement::Required(name) => - format!(" --{}={}", option.long(), name), - ValueRequirement::Optional(name) => - format!(" --{}[={}]", option.long(), name), + ValueRequirement::Required(name) => format!(" --{}={}", option.long(), name), + ValueRequirement::Optional(name) => { + format!(" --{}[={}]", option.long(), name) + } }; } else if option.long() == "" { left = match option.value_req { ValueRequirement::None => format!("-{}", option.short()), ValueRequirement::Required(name) => format!("-{}={}", option.short(), name), - ValueRequirement::Optional(name) => - format!("-{}[={}]", option.short(), name), + ValueRequirement::Optional(name) => format!("-{}[={}]", option.short(), name), }; } else { left = match option.value_req { ValueRequirement::None => format!("-{}, --{}", option.short(), option.long()), - ValueRequirement::Required(name) => - format!("-{}, --{}={}", option.short(), option.long(), name), - ValueRequirement::Optional(name) => - format!("-{}, --{}[={}]", option.short(), option.long(), name), + ValueRequirement::Required(name) => { + format!("-{}, --{}={}", option.short(), option.long(), name) + } + ValueRequirement::Optional(name) => { + format!("-{}, --{}[={}]", option.short(), option.long(), name) + } }; } lines.push((left, option.description())); diff --git a/src/main.rs b/src/main.rs index 6b7c181..347e144 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,25 +10,33 @@ fn parse_args() -> Result<bool, String> { .short('h') .long("help") .description("display this and exit") - .build().unwrap()); + .build() + .unwrap(), + ); let version_idx = options.push( args::OptionBuilder::default() .short('V') .long("version") .description("display version and exit") - .build().unwrap()); + .build() + .unwrap(), + ); let verbose_idx = options.push( args::OptionBuilder::default() .long("verbose") .description("be verbose") - .build().unwrap()); + .build() + .unwrap(), + ); let test_idx = options.push( args::OptionBuilder::default() .short('t') .long("test") .description("testing") .value(args::ValueRequirement::Required("FILE")) - .build().unwrap()); + .build() + .unwrap(), + ); let parser = args::ShortAndLongParser::new(); let args = parser.run(&mut options, std::env::args())?; |
