@@ -98,6 +98,55 @@ impl<F: ErrorFormatter> Error<F> {
9898 self . with_cmd ( cmd)
9999 }
100100
101+ /// Create an error with a pre-defined message
102+ ///
103+ /// See also
104+ /// - [`Error::insert`]
105+ /// - [`Error::with_cmd`]
106+ ///
107+ /// # Example
108+ ///
109+ #[ cfg_attr( not( feature = "error-context" ) , doc = " ```ignore" ) ]
110+ #[ cfg_attr( feature = "error-context" , doc = " ```" ) ]
111+ /// # use clap::error::ErrorKind;
112+ /// # use clap::error::ContextKind;
113+ /// # use clap::error::ContextValue;
114+ ///
115+ /// let cmd = clap::Command::new("prog");
116+ ///
117+ /// let mut err = clap::Error::new(ErrorKind::ValueValidation)
118+ /// .with_cmd(&cmd);
119+ /// err.insert(ContextKind::InvalidArg, ContextValue::String("--foo".to_owned()));
120+ /// err.insert(ContextKind::InvalidValue, ContextValue::String("bar".to_owned()));
121+ ///
122+ /// err.print();
123+ /// ```
124+ pub fn new ( kind : ErrorKind ) -> Self {
125+ Self {
126+ inner : Box :: new ( ErrorInner {
127+ kind,
128+ #[ cfg( feature = "error-context" ) ]
129+ context : FlatMap :: new ( ) ,
130+ message : None ,
131+ source : None ,
132+ help_flag : None ,
133+ color_when : ColorChoice :: Never ,
134+ color_help_when : ColorChoice :: Never ,
135+ backtrace : Backtrace :: new ( ) ,
136+ } ) ,
137+ phantom : Default :: default ( ) ,
138+ }
139+ }
140+
141+ /// Apply [`Command`]'s formatting to the error
142+ ///
143+ /// Generally, this is used with [`Error::new`]
144+ pub fn with_cmd ( self , cmd : & Command ) -> Self {
145+ self . set_color ( cmd. get_color ( ) )
146+ . set_colored_help ( cmd. color_help ( ) )
147+ . set_help_flag ( format:: get_help_flag ( cmd) )
148+ }
149+
101150 /// Apply an alternative formatter to the error
102151 ///
103152 /// # Example
@@ -138,6 +187,13 @@ impl<F: ErrorFormatter> Error<F> {
138187 self . inner . context . get ( & kind)
139188 }
140189
190+ /// Insert a piece of context
191+ #[ inline( never) ]
192+ #[ cfg( feature = "error-context" ) ]
193+ pub fn insert ( & mut self , kind : ContextKind , value : ContextValue ) -> Option < ContextValue > {
194+ self . inner . context . insert ( kind, value)
195+ }
196+
141197 /// Should the message be written to `stdout` or not?
142198 #[ inline]
143199 pub fn use_stderr ( & self ) -> bool {
@@ -216,34 +272,11 @@ impl<F: ErrorFormatter> Error<F> {
216272 self . formatted ( ) . into_owned ( )
217273 }
218274
219- fn new ( kind : ErrorKind ) -> Self {
220- Self {
221- inner : Box :: new ( ErrorInner {
222- kind,
223- #[ cfg( feature = "error-context" ) ]
224- context : FlatMap :: new ( ) ,
225- message : None ,
226- source : None ,
227- help_flag : None ,
228- color_when : ColorChoice :: Never ,
229- color_help_when : ColorChoice :: Never ,
230- backtrace : Backtrace :: new ( ) ,
231- } ) ,
232- phantom : Default :: default ( ) ,
233- }
234- }
235-
236275 #[ inline( never) ]
237276 fn for_app ( kind : ErrorKind , cmd : & Command , styled : StyledStr ) -> Self {
238277 Self :: new ( kind) . set_message ( styled) . with_cmd ( cmd)
239278 }
240279
241- pub ( crate ) fn with_cmd ( self , cmd : & Command ) -> Self {
242- self . set_color ( cmd. get_color ( ) )
243- . set_colored_help ( cmd. color_help ( ) )
244- . set_help_flag ( format:: get_help_flag ( cmd) )
245- }
246-
247280 pub ( crate ) fn set_message ( mut self , message : impl Into < Message > ) -> Self {
248281 self . inner . message = Some ( message. into ( ) ) ;
249282 self
0 commit comments