ju-tcs-tbop-24-jfk 0.1.2

CLI tool for head and tail commands
use clap::{Parser, Subcommand};
use ju_tcs_tbop_24_bb::{head, tail};
use std::path::Path;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    cmd: Commands,
}

#[derive(Subcommand, Debug, Clone)]
enum Commands {
    head { n: usize, file: String },
    tail { n: usize, file: String },
}

fn print_lines(n: usize, file: String, option: bool) {
    let path = Path::new(&file);
    let lines = if option { head(path, n) } else { tail(path, n) };

    for line in lines {
        println!("{}", line);
    }
}

fn main() {
    let cli = Cli::parse();

    match cli.cmd {
        Commands::head { n, file } => print_lines(n, file, true),
        Commands::tail { n, file } => print_lines(n, file, false),
    }
}