<commit_before>extern crate lz4_compress as lz4;

use std::env;
use std::io::{self, Write, Read};

fn main() {
    // Get and lock stdout.
    let stdout = io::stdout();
    let mut stdout = stdout.lock();

    match &*env::args().nth(1).unwrap_or(String::new()) {
        "-c" => {
            // Read stream from stdin.
            let mut vec = Vec::new();
            io::stdin().read_to_end(&mut vec).expect("Failed to read stdin");

            // Compress it and write the result to stdout.
            stdout.write(&lz4::compress(&vec)).expect("Failed to write to stdout");
        },
        "-d" => {
            // Read stream from stdin.
            let mut vec = Vec::new();
            io::stdin().read_to_end(&mut vec).expect("Failed to read stdin");

            // Decompress the input.
            let decompressed = lz4::decompress(&vec).expect("Compressed data contains errors");

            // Write the decompressed buffer to stdout.
            stdout.write(&decompressed).expect("Failed to write to stdout");
        },
        // If no valid arguments are given, we print the help page.
        _ => {
            stdout.write(b"\
            Introduction:\n\
                lz4 - an utility to decompress or compress a raw, headerless LZ4 stream.\n\
            Usage:\n\
                lz4 [option]\n\
            Options:\n\
                -c : Compress stdin and write the result to stdout.\n\
                -d : Decompress stdin and write the result to stdout.\n\
                -h : Write this manpage to stderr.\n\
            ").expect("Failed to write to stdout");
        },
    }
}<commit_msg>Fix bug in which errors in the command line flags weren't detected properly (LZ4 compression CLI)<commit_after>extern crate lz4_compress as lz4;

use std::{env, process};
use std::io::{self, Write, Read};

/// The help page for this command.
const HELP: &'static [u8] = br#"
Introduction:
    lz4 - an utility to decompress or compress a raw, headerless LZ4 stream.
Usage:
    lz4 [option]
Options:
    -c : Compress stdin and write the result to stdout.
    -d : Decompress stdin and write the result to stdout.
    -h : Write this manpage to stderr.
"#;

fn main() {
    let mut iter = env::args().skip(1);
    let mut flag = iter.next().unwrap_or(String::new());
    // If another argument is provided (e.g. the user passes a file name), we need to make sure we
    // issue an error properly, so we set back the flag to `""`.
    if iter.next().is_some() {
        flag = String::new();
    }

    match &*flag {
        "-c" => {
            // Read stream from stdin.
            let mut vec = Vec::new();
            io::stdin().read_to_end(&mut vec).expect("Failed to read stdin");

            // Compress it and write the result to stdout.
            io::stdout().write(&lz4::compress(&vec)).expect("Failed to write to stdout");
        },
        "-d" => {
            // Read stream from stdin.
            let mut vec = Vec::new();
            io::stdin().read_to_end(&mut vec).expect("Failed to read stdin");

            // Decompress the input.
            let decompressed = lz4::decompress(&vec).expect("Compressed data contains errors");

            // Write the decompressed buffer to stdout.
            io::stdout().write(&decompressed).expect("Failed to write to stdout");
        },
        // If no valid arguments are given, we print the help page.
        _ => {
            io::stdout().write(HELP).expect("Failed to write to stdout");

            process::exit(1);
        },
    }
}
{'fraction_non_alphanumeric': 0.1320754716981132, 'fraction_numerical': 0.005350605463249789, 'mean_word_length': 1.9873843566021867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2044942', 'n_tokens_mistral': 1040, 'n_tokens_neox': 1018, 'n_words': 347}