Skip to content

Commit

Permalink
Merge pull request #2138 from jqnatividad/2105-move-help-to-stdout
Browse files Browse the repository at this point in the history
move --help output from stderr to stdout
  • Loading branch information
jqnatividad authored Sep 13, 2024
2 parents 4774cb7 + c8ef543 commit b997c76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
20 changes: 16 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,15 @@ 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(ref errtype, ref usage_text) = err {
if let docopt::Error::Help = **errtype {
CliError::Help(usage_text.to_string())
} else {
CliError::Flag(err)
}
} else {
CliError::Flag(err)
}
}
}

Expand All @@ -182,9 +192,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 b997c76

Please sign in to comment.