Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misunderstanding Jump from 1.4->1.5+ #222

Open
Muddledde opened this issue Jul 18, 2023 · 4 comments
Open

Misunderstanding Jump from 1.4->1.5+ #222

Muddledde opened this issue Jul 18, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@Muddledde
Copy link

Muddledde commented Jul 18, 2023

First, thank you for taking the time to create this guide (and reading this). :-)

It was my understanding from the beginning that the guide would guide one towards creating a simple Command Line App, but after 1.4 there doesn't appear to be much cohesion. I assume I am missing something here, but 1.5 (and further) changes main.rs in a way that it doesn't appear to function around the same logic built/defined before 1.4.

1.3

#![allow(unused)]

use clap::Parser;

/// Search for a pattern in a file and display the lines that contain it.
#[derive(Parser)]
struct Cli {
    /// The pattern to look for
    pattern: String,
    /// The path to the file to read
    path: std::path::PathBuf,
}

fn main() {
    let args = Cli::parse();
    let content = std::fs::read_to_string(&args.path).expect("could not read file");

    for line in content.lines() {
        if line.contains(&args.pattern) {
            println!("{}", line);
        }
    }
}

1.4

use anyhow::{Context, Result};

fn main() -> Result<()> {
    let path = "test.txt";
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("could not read file `{}`", path))?;
    println!("file content: {}", content);
    Ok(())
}

1.5 gives an example of a bunch of nice tools to add (such as logging and progress bars), but as mentioned earlier, there is no cohesion with the early development; no addition to the previously constructed chapters.

Printer Performance

#![allow(unused)]
fn main() {
use std::io::{self, Write};

let stdout = io::stdout(); // get the global stdout entity
let mut handle = stdout.lock(); // acquire a lock on it
writeln!(handle, "foo: {}", 42); // add `?` if you care about errors here
}

Progress Bar

fn main() {
    let pb = indicatif::ProgressBar::new(100);
    for i in 0..100 {
        do_hard_work();
        pb.println(format!("[+] finished #{}", i));
        pb.inc(1);
    }
    pb.finish_with_message("done");
}

Logging

use log::{info, warn};

fn main() {
    env_logger::init();
    info!("starting up");
    warn!("oops, nothing implemented!");
}
@epage epage added the bug Something isn't working label Jul 18, 2023
@epage
Copy link
Contributor

epage commented Jul 18, 2023

Good call.

We also should be highlighting that prinln will panic on broken pipes

@axjms1
Copy link

axjms1 commented Oct 3, 2023

Just came here to chime in and agree with @Muddledde. I love the idea of this shorter intro to cli programs, but it does seem to need a bit of updating. How can I help?

@stomar
Copy link
Contributor

stomar commented Nov 5, 2023

A smaller "jump already happens with 1.4, the error handling code is not really integrated into the earlier grss example. Rather, the example used in the error chapter is essentially independent from the code developed earlier (admittedly with some similarities).

@stomar
Copy link
Contributor

stomar commented Nov 10, 2023

An implementation of grss with improved error handling has been added at the end of chapter 1.4 (#233), but still the code is not developed incrementally into the given form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants