clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust

Home Page:docs.rs/clap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

clap_complete: `generate()` shouldn't panic if writing fails

yedayak opened this issue · comments

Please complete the following tasks

  • I have searched the discussions

  • I have searched the existing issues

Rust Version

rustc 1.77.2 (25ef9e3d8 2024-04-09)

Clap Version

master

Actual Behaviour

Running generate() can panic, for example if writing to a pipe ans the next program stops, or anything really stops the generation from completing.
For example

cmd --generate | head

with a long enough completion will panic.

Expected Behaviour

generate() should return a Result indicating if it fails. This will be a breaking change.

Additional Context

Maybe we ignore some errors? I'm not sure what consumers will want to do with the error, and some of them might be not important enough to propagate, for example a broken pipe.
The panic happens here:

Err(..) => panic!("Failed to write to generated file"),

Opened after suggestion in #5453.