clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
13.65k stars 1.02k forks source link

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

Open yedayak opened 2 months ago

yedayak commented 2 months ago

Please complete the following tasks

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: https://github.com/clap-rs/clap/blob/0cd10d19f0ac39341f89130d924e87879b58867f/clap_complete/src/macros.rs#L5

Opened after suggestion in #5453.