Skip to content

Commit

Permalink
move --help output from stderr to stdout
Browse files Browse the repository at this point in the history
as per gnu guidelines

fixes #2105

Also convert match with two arms to if let for CSV error from() impl
  • Loading branch information
jqnatividad committed Sep 13, 2024
1 parent 4774cb7 commit 2b7dbdc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/clitypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub type CliResult<T> = Result<T, CliError>;
#[derive(Debug)]
pub enum CliError {
Flag(docopt::Error),
Help(String),
Csv(csv::Error),
Io(io::Error),
NoMatch(),
Expand All @@ -159,6 +160,7 @@ impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CliError::Flag(ref e) => e.fmt(f),
CliError::Help(ref e) => e.fmt(f),
CliError::Csv(ref e) => e.fmt(f),
CliError::Io(ref e) => e.fmt(f),
CliError::NoMatch() => f.write_str("no_match"),
Expand All @@ -173,7 +175,11 @@ impl fmt::Display for CliError {

impl From<docopt::Error> for CliError {
fn from(err: docopt::Error) -> CliError {
CliError::Flag(err)
if let docopt::Error::WithProgramUsage(_, usage_text) = err {
CliError::Help(usage_text)
} else {
CliError::Flag(err)
}
}
}

Expand All @@ -182,9 +188,11 @@ impl From<csv::Error> for CliError {
if !err.is_io_error() {
return CliError::Csv(err);
}
match err.into_kind() {
csv::ErrorKind::Io(v) => From::from(v),
_ => unreachable!(),
if let csv::ErrorKind::Io(v) = err.into_kind() {
From::from(v)
} else {
// safety: we checked for !is_io_error above
unreachable!()
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ sponsored by datHere - Data Infrastructure Engineering (https://qsv.datHere.com)
util::log_end(qsv_args, now);
QsvExitCode::Good
},
Err(CliError::Help(usage_text)) => {
wout!("{usage_text}");
util::log_end(qsv_args, now);
QsvExitCode::Good
},
Err(CliError::Flag(err)) => {
werr!("{err}");
util::log_end(qsv_args, now);
Expand Down
5 changes: 5 additions & 0 deletions src/maindp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ Please choose one of the following commands:",
util::log_end(qsv_args, now);
QsvExitCode::Good
},
Err(CliError::Help(usage_text)) => {
wout!("{usage_text}");
util::log_end(qsv_args, now);
QsvExitCode::Good
},
Err(CliError::Flag(err)) => {
werr!("{err}");
util::log_end(qsv_args, now);
Expand Down
5 changes: 5 additions & 0 deletions src/mainlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ Please choose one of the following commands:",
util::log_end(qsv_args, now);
QsvExitCode::Good
},
Err(CliError::Help(usage_text)) => {
wout!("{usage_text}");
util::log_end(qsv_args, now);
QsvExitCode::Good
},
Err(CliError::Flag(err)) => {
werr!("{err}");
util::log_end(qsv_args, now);
Expand Down

0 comments on commit 2b7dbdc

Please sign in to comment.