@@ -21,7 +21,7 @@ pub trait Generator {
2121 /// # Examples
2222 ///
2323 /// ```
24- /// # use std::io::Write;
24+ /// # use std::io::{Error, Write} ;
2525 /// # use clap::Command;
2626 /// use clap_complete::Generator;
2727 ///
@@ -32,6 +32,7 @@ pub trait Generator {
3232 /// format!("{name}.fish")
3333 /// }
3434 /// # fn generate(&self, cmd: &Command, buf: &mut dyn Write) {}
35+ /// # fn try_generate(&self, cmd: &Command, buf: &mut dyn Write) -> Result<(), Error> {Ok(())}
3536 /// }
3637 /// ```
3738 fn file_name ( & self , name : & str ) -> String ;
@@ -48,7 +49,7 @@ pub trait Generator {
4849 /// as if it is printed using [`std::println`].
4950 ///
5051 /// ```
51- /// use std::{io::Write, fmt::write};
52+ /// use std::{io::{Error, Write} , fmt::write};
5253 /// use clap::Command;
5354 /// use clap_complete::Generator;
5455 ///
@@ -61,9 +62,44 @@ pub trait Generator {
6162 /// fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
6263 /// write!(buf, "{cmd}").unwrap();
6364 /// }
65+ /// # fn try_generate(&self, cmd: &Command, buf: &mut dyn Write) -> Result<(), Error> {
66+ /// # write!(buf, "{cmd}")
67+ /// # }
6468 /// }
6569 /// ```
6670 fn generate ( & self , cmd : & Command , buf : & mut dyn Write ) ;
71+
72+ ///
73+ /// Fallible version to generate output out of [`clap::Command`].
74+ ///
75+ /// # Examples
76+ ///
77+ /// The following example generator displays the [`clap::Command`]
78+ /// as if it is printed using [`std::println`].
79+ ///
80+ /// ```
81+ /// use std::{io::{Error, Write}, fmt::write};
82+ /// use clap::Command;
83+ /// use clap_complete::Generator;
84+ ///
85+ /// pub struct ClapDebug;
86+ ///
87+ /// impl Generator for ClapDebug {
88+ /// # fn file_name(&self, name: &str) -> String {
89+ /// # name.into()
90+ /// # }
91+ /// # fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
92+ /// # self.try_generate(cmd, buf).expect("failed to write completion file");
93+ /// # }
94+ /// fn try_generate(&self, cmd: &Command, buf: &mut dyn Write) -> Result<(), Error> {
95+ /// write!(buf, "{cmd}")
96+ /// }
97+ /// }
98+ /// ```
99+ fn try_generate ( & self , cmd : & Command , buf : & mut dyn Write ) -> Result < ( ) , Error > {
100+ self . generate ( cmd, buf) ;
101+ Ok ( ( ) )
102+ }
67103}
68104
69105/// Generate a completions file for a specified shell at compile-time.
0 commit comments