Introduce dedicated ParseResult to simplify error handling in main

This commit is contained in:
Thomas Eizinger 2021-05-21 12:35:22 +10:00
parent d43ba33e15
commit 229d840990
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
2 changed files with 124 additions and 47 deletions

View File

@ -20,10 +20,8 @@ use std::future::Future;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use structopt::clap;
use structopt::clap::ErrorKind;
use swap::bitcoin::TxLock; use swap::bitcoin::TxLock;
use swap::cli::command::{parse_args_and_apply_defaults, Arguments, Command}; use swap::cli::command::{parse_args_and_apply_defaults, Arguments, Command, ParseResult};
use swap::database::Database; use swap::database::Database;
use swap::env::Config; use swap::env::Config;
use swap::network::quote::BidQuote; use swap::network::quote::BidQuote;
@ -47,22 +45,12 @@ async fn main() -> Result<()> {
debug, debug,
json, json,
cmd, cmd,
} = match parse_args_and_apply_defaults(env::args_os()) { } = match parse_args_and_apply_defaults(env::args_os())? {
Ok(args) => args, ParseResult::Arguments(args) => args,
Err(e) => { ParseResult::PrintAndExitZero { message } => {
if let Some(clap_err) = e.downcast_ref::<clap::Error>() { println!("{}", message);
match clap_err.kind {
ErrorKind::HelpDisplayed | ErrorKind::VersionDisplayed => {
println!("{}", clap_err.message);
std::process::exit(0); std::process::exit(0);
} }
_ => {
bail!(e);
}
}
}
bail!(e);
}
}; };
match cmd { match cmd {

View File

@ -7,7 +7,7 @@ use libp2p::PeerId;
use std::ffi::OsString; use std::ffi::OsString;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use structopt::StructOpt; use structopt::{clap, StructOpt};
use url::Url; use url::Url;
use uuid::Uuid; use uuid::Uuid;
@ -34,13 +34,37 @@ pub struct Arguments {
pub cmd: Command, pub cmd: Command,
} }
pub fn parse_args_and_apply_defaults<I, T>(raw_args: I) -> Result<Arguments> /// Represents the result of parsing the command-line parameters.
#[derive(Debug, PartialEq)]
pub enum ParseResult {
/// The arguments we were invoked in.
Arguments(Arguments),
/// A flag or command was given that does not need further processing other
/// than printing the provided message.
///
/// The caller should exit the program with exit code 0.
PrintAndExitZero { message: String },
}
pub fn parse_args_and_apply_defaults<I, T>(raw_args: I) -> Result<ParseResult>
where where
I: IntoIterator<Item = T>, I: IntoIterator<Item = T>,
T: Into<OsString> + Clone, T: Into<OsString> + Clone,
{ {
let matches = RawArguments::clap().get_matches_from_safe(raw_args)?; let args = match RawArguments::clap().get_matches_from_safe(raw_args) {
let args = RawArguments::from_clap(&matches); Ok(matches) => RawArguments::from_clap(&matches),
Err(clap::Error {
message,
kind: clap::ErrorKind::HelpDisplayed,
..
})
| Err(clap::Error {
message,
kind: clap::ErrorKind::VersionDisplayed,
..
}) => return Ok(ParseResult::PrintAndExitZero { message }),
Err(e) => anyhow::bail!(e),
};
let debug = args.debug; let debug = args.debug;
let json = args.json; let json = args.json;
@ -176,7 +200,7 @@ where
}, },
}; };
Ok(arguments) Ok(ParseResult::Arguments(arguments))
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -490,8 +514,9 @@ mod tests {
PEER_ID, PEER_ID,
]; ];
let expected_args = Arguments::buy_xmr_mainnet_defaults(); let expected_args = ParseResult::Arguments(Arguments::buy_xmr_mainnet_defaults());
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(expected_args, args); assert_eq!(expected_args, args);
} }
@ -510,7 +535,11 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::buy_xmr_testnet_defaults());
assert_eq!(
args,
ParseResult::Arguments(Arguments::buy_xmr_testnet_defaults())
);
} }
#[test] #[test]
@ -576,7 +605,11 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::resume_mainnet_defaults());
assert_eq!(
args,
ParseResult::Arguments(Arguments::resume_mainnet_defaults())
);
} }
#[test] #[test]
@ -594,7 +627,11 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::resume_testnet_defaults());
assert_eq!(
args,
ParseResult::Arguments(Arguments::resume_testnet_defaults())
);
} }
#[test] #[test]
@ -602,7 +639,11 @@ mod tests {
let raw_ars = vec![BINARY_NAME, "cancel", "--swap-id", SWAP_ID]; let raw_ars = vec![BINARY_NAME, "cancel", "--swap-id", SWAP_ID];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::cancel_mainnet_defaults());
assert_eq!(
args,
ParseResult::Arguments(Arguments::cancel_mainnet_defaults())
);
} }
#[test] #[test]
@ -610,7 +651,11 @@ mod tests {
let raw_ars = vec![BINARY_NAME, "--testnet", "cancel", "--swap-id", SWAP_ID]; let raw_ars = vec![BINARY_NAME, "--testnet", "cancel", "--swap-id", SWAP_ID];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::cancel_testnet_defaults());
assert_eq!(
args,
ParseResult::Arguments(Arguments::cancel_testnet_defaults())
);
} }
#[test] #[test]
@ -618,7 +663,11 @@ mod tests {
let raw_ars = vec![BINARY_NAME, "refund", "--swap-id", SWAP_ID]; let raw_ars = vec![BINARY_NAME, "refund", "--swap-id", SWAP_ID];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::refund_mainnet_defaults());
assert_eq!(
args,
ParseResult::Arguments(Arguments::refund_mainnet_defaults())
);
} }
#[test] #[test]
@ -626,7 +675,11 @@ mod tests {
let raw_ars = vec![BINARY_NAME, "--testnet", "refund", "--swap-id", SWAP_ID]; let raw_ars = vec![BINARY_NAME, "--testnet", "refund", "--swap-id", SWAP_ID];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::refund_testnet_defaults());
assert_eq!(
args,
ParseResult::Arguments(Arguments::refund_testnet_defaults())
);
} }
#[test] #[test]
@ -647,10 +700,13 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!( assert_eq!(
args, args,
ParseResult::Arguments(
Arguments::buy_xmr_mainnet_defaults() Arguments::buy_xmr_mainnet_defaults()
.with_data_dir(PathBuf::from_str(data_dir).unwrap()) .with_data_dir(PathBuf::from_str(data_dir).unwrap())
)
); );
let raw_ars = vec![ let raw_ars = vec![
@ -668,10 +724,13 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!( assert_eq!(
args, args,
ParseResult::Arguments(
Arguments::buy_xmr_testnet_defaults() Arguments::buy_xmr_testnet_defaults()
.with_data_dir(PathBuf::from_str(data_dir).unwrap()) .with_data_dir(PathBuf::from_str(data_dir).unwrap())
)
); );
let raw_ars = vec![ let raw_ars = vec![
@ -688,10 +747,13 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!( assert_eq!(
args, args,
ParseResult::Arguments(
Arguments::resume_mainnet_defaults() Arguments::resume_mainnet_defaults()
.with_data_dir(PathBuf::from_str(data_dir).unwrap()) .with_data_dir(PathBuf::from_str(data_dir).unwrap())
)
); );
let raw_ars = vec![ let raw_ars = vec![
@ -709,10 +771,13 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!( assert_eq!(
args, args,
ParseResult::Arguments(
Arguments::resume_testnet_defaults() Arguments::resume_testnet_defaults()
.with_data_dir(PathBuf::from_str(data_dir).unwrap()) .with_data_dir(PathBuf::from_str(data_dir).unwrap())
)
); );
} }
@ -731,7 +796,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::buy_xmr_mainnet_defaults().with_debug()); assert_eq!(
args,
ParseResult::Arguments(Arguments::buy_xmr_mainnet_defaults().with_debug())
);
let raw_ars = vec![ let raw_ars = vec![
BINARY_NAME, BINARY_NAME,
@ -747,7 +815,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::buy_xmr_testnet_defaults().with_debug()); assert_eq!(
args,
ParseResult::Arguments(Arguments::buy_xmr_testnet_defaults().with_debug())
);
let raw_ars = vec![ let raw_ars = vec![
BINARY_NAME, BINARY_NAME,
@ -762,7 +833,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::resume_mainnet_defaults().with_debug()); assert_eq!(
args,
ParseResult::Arguments(Arguments::resume_mainnet_defaults().with_debug())
);
let raw_ars = vec![ let raw_ars = vec![
BINARY_NAME, BINARY_NAME,
@ -778,7 +852,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::resume_testnet_defaults().with_debug()); assert_eq!(
args,
ParseResult::Arguments(Arguments::resume_testnet_defaults().with_debug())
);
} }
#[test] #[test]
@ -796,7 +873,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::buy_xmr_mainnet_defaults().with_json()); assert_eq!(
args,
ParseResult::Arguments(Arguments::buy_xmr_mainnet_defaults().with_json())
);
let raw_ars = vec![ let raw_ars = vec![
BINARY_NAME, BINARY_NAME,
@ -812,7 +892,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::buy_xmr_testnet_defaults().with_json()); assert_eq!(
args,
ParseResult::Arguments(Arguments::buy_xmr_testnet_defaults().with_json())
);
let raw_ars = vec![ let raw_ars = vec![
BINARY_NAME, BINARY_NAME,
@ -827,7 +910,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::resume_mainnet_defaults().with_json()); assert_eq!(
args,
ParseResult::Arguments(Arguments::resume_mainnet_defaults().with_json())
);
let raw_ars = vec![ let raw_ars = vec![
BINARY_NAME, BINARY_NAME,
@ -843,7 +929,10 @@ mod tests {
]; ];
let args = parse_args_and_apply_defaults(raw_ars).unwrap(); let args = parse_args_and_apply_defaults(raw_ars).unwrap();
assert_eq!(args, Arguments::resume_testnet_defaults().with_json()); assert_eq!(
args,
ParseResult::Arguments(Arguments::resume_testnet_defaults().with_json())
);
} }
impl Arguments { impl Arguments {