docs(main): add a few explanatory comments to main
cargo devel CI / cargo CI (push) Has been cancelled Details

This commit is contained in:
Christoph J. Scherr 2024-05-23 13:29:03 +02:00
parent 71f1f2ba08
commit 03a2243a3e
1 changed files with 3 additions and 0 deletions

View File

@ -12,6 +12,7 @@ fn main() -> anyhow::Result<()> {
let mut options = FormatOptions::parse(); let mut options = FormatOptions::parse();
let mut stdin_nums = Vec::new(); let mut stdin_nums = Vec::new();
let stdin = std::io::stdin(); let stdin = std::io::stdin();
// only accept numbers from stdin if the stdin is not an interactive terminal
if !stdin.is_terminal() { if !stdin.is_terminal() {
match stdin.lock().read_to_end(&mut stdin_nums) { match stdin.lock().read_to_end(&mut stdin_nums) {
Ok(_) => { Ok(_) => {
@ -44,6 +45,7 @@ fn main() -> anyhow::Result<()> {
}; };
} }
// add random numbers to the number list, according to how many are requested
if options.rand() > 0 { if options.rand() > 0 {
use rand::prelude::*; use rand::prelude::*;
let mut rand = rand::rngs::OsRng; let mut rand = rand::rngs::OsRng;
@ -52,6 +54,7 @@ fn main() -> anyhow::Result<()> {
} }
} }
// exit with error if no numbers are to be formatted
if options.numbers().is_empty() { if options.numbers().is_empty() {
eprintln!("{}", FormatOptions::command().render_usage()); eprintln!("{}", FormatOptions::command().render_usage());
eprintln!("no numbers have been provided"); eprintln!("no numbers have been provided");