Skip to content
This repository has been archived by the owner on Oct 30, 2021. It is now read-only.

Add --stdout option / redirect error messages to stderr #79

Merged
merged 1 commit into from Mar 31, 2017

Conversation

sairion
Copy link
Contributor

@sairion sairion commented Mar 27, 2017

Hi,

I added an option to take output to stdout. This is my first attempt to write Rust code other than hello world, so I believe it could be not-so-much-idiomatic ;) Please take your time to tell me if there's anything to fix!

Also, I redirected error messages to stderr to prevent error messages to be written to stdout. I needed this option because I wanted to replace svgo with svgcleaner, using stream interface in Node.

src/main.rs Outdated
let in_file = args.value_of("in-file").unwrap();
let out_file = args.value_of("out-file").unwrap();
let in_file = args.value_of("in-file").unwrap();
let out_file = args.value_of("out-file").unwrap_or("");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use Option<String> in this case. So .unwrap_or("") is not needed.

src/main.rs Outdated

if cli::get_flag(&args, Key::Quiet) {
return;
if !out_file.is_empty() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we switched to Option<String> it will became:
if let Some(out_file) = out_file {

@RazrFalcon
Copy link
Owner

Looks good, but there are few problems:

  1. Warnings from svgcleaner, svgdom and svgparser will still be printed to stdout. So we have to update all of them. It's not a problem, just a task. I planned to do this one day. So I should update all the dependencies and only then merge this patch.
  2. stdout usually marked as -, --, -c or --stdout. I don't know if clap supports -/--, but -c should be added.
  3. I use custom help text, so you should update data/help.txt as well.

As for code itself - I've added few comments.

src/main.rs Outdated
@@ -72,7 +73,7 @@ fn main() {

let on_err = || {
// copy original file to destination
if cli::get_flag(&args, Key::CopyOnError) {
if out_file.is_empty() && cli::get_flag(&args, Key::CopyOnError) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an error? It should be !out_file.is_empty()?

-out_file.is_empty()
+out_file.is_some()

@sairion
Copy link
Contributor Author

sairion commented Mar 31, 2017

@RazrFalcon I believe -- option is a bit different with standard output flag? It seems clap (kinda) supports it but I don't think it is applicable here.

I've add -c as positional argument and set --stdout as alias of it. Please take a look! 😄

src/cleaner.rs Outdated
@@ -207,6 +207,11 @@ pub fn write_buffer(doc: &Document, opt: &WriteOptions, buf: &mut Vec<u8>) {
doc.write_buf_opt(opt, buf);
}

pub fn write_stdout(data: &[u8]) -> Result<(), io::Error> {
io::stdout().write(&data).unwrap();
Ok(())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should remove either Ok(()) or unwrap(), because this code has no meaning.

src/main.rs Outdated
if in_file != out_file {
try_msg!(fs::copy(in_file, out_file));
if in_file != out_file.unwrap() {
try_msg!(fs::copy(in_file, out_file.unwrap()));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much unwrap. You can use if let syntax here too.

if let Some(out_file) = out_file {
    if in_file != out_file {
        try_msg!(fs::copy(in_file, out_file));
    }
}

@RazrFalcon
Copy link
Owner

I've added two comments. You can fix this issues and squash commits into one. Then I will merge it.

@RazrFalcon
Copy link
Owner

svgdom and svgparser will print warnings to stderr now.

- Add -c flag and --stdout option as alias of it
- print error messages to stderr
@sairion sairion force-pushed the master branch 2 times, most recently from eef1fcd to 4b7502f Compare March 31, 2017 15:13
@sairion
Copy link
Contributor Author

sairion commented Mar 31, 2017

@RazrFalcon fixed, and squashed commits into one (I didn't see your commit mixed in, sorry)

@RazrFalcon
Copy link
Owner

Good. Thanks.

@RazrFalcon RazrFalcon merged commit 1d180c6 into RazrFalcon:master Mar 31, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants