2323#![ feature( test) ]
2424#![ feature( vec_remove_item) ]
2525#![ feature( entry_and_modify) ]
26+ #![ feature( dyn_trait) ]
2627
2728extern crate arena;
2829extern crate getopts;
@@ -48,6 +49,8 @@ extern crate tempdir;
4849
4950extern crate serialize as rustc_serialize; // used by deriving
5051
52+ use errors:: ColorConfig ;
53+
5154use std:: collections:: { BTreeMap , BTreeSet } ;
5255use std:: default:: Default ;
5356use std:: env;
@@ -279,6 +282,21 @@ pub fn opts() -> Vec<RustcOptGroup> {
279282 "edition to use when compiling rust code (default: 2015)" ,
280283 "EDITION" )
281284 } ) ,
285+ unstable( "color" , |o| {
286+ o. optopt( "" ,
287+ "color" ,
288+ "Configure coloring of output:
289+ auto = colorize, if output goes to a tty (default);
290+ always = always colorize output;
291+ never = never colorize output" ,
292+ "auto|always|never" )
293+ } ) ,
294+ unstable( "error-format" , |o| {
295+ o. optopt( "" ,
296+ "error-format" ,
297+ "How errors and other messages are produced" ,
298+ "human|json|short" )
299+ } ) ,
282300 ]
283301}
284302
@@ -363,9 +381,33 @@ pub fn main_args(args: &[String]) -> isize {
363381 }
364382 let input = & matches. free [ 0 ] ;
365383
384+ let color = match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
385+ Some ( "auto" ) => ColorConfig :: Auto ,
386+ Some ( "always" ) => ColorConfig :: Always ,
387+ Some ( "never" ) => ColorConfig :: Never ,
388+ None => ColorConfig :: Auto ,
389+ Some ( arg) => {
390+ print_error ( & format ! ( "argument for --color must be `auto`, `always` or `never` \
391+ (instead was `{}`)", arg) ) ;
392+ return 1 ;
393+ }
394+ } ;
395+ let error_format = match matches. opt_str ( "error-format" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
396+ Some ( "human" ) => ErrorOutputType :: HumanReadable ( color) ,
397+ Some ( "json" ) => ErrorOutputType :: Json ( false ) ,
398+ Some ( "pretty-json" ) => ErrorOutputType :: Json ( true ) ,
399+ Some ( "short" ) => ErrorOutputType :: Short ( color) ,
400+ None => ErrorOutputType :: HumanReadable ( color) ,
401+ Some ( arg) => {
402+ print_error ( & format ! ( "argument for --error-format must be `human`, `json` or \
403+ `short` (instead was `{}`)", arg) ) ;
404+ return 1 ;
405+ }
406+ } ;
407+
366408 let mut libs = SearchPaths :: new ( ) ;
367409 for s in & matches. opt_strs ( "L" ) {
368- libs. add_path ( s, ErrorOutputType :: default ( ) ) ;
410+ libs. add_path ( s, error_format ) ;
369411 }
370412 let externs = match parse_externs ( & matches) {
371413 Ok ( ex) => ex,
@@ -465,7 +507,9 @@ pub fn main_args(args: &[String]) -> isize {
465507 }
466508
467509 let output_format = matches. opt_str ( "w" ) ;
468- let res = acquire_input ( PathBuf :: from ( input) , externs, edition, cg, & matches, move |out| {
510+
511+ let res = acquire_input ( PathBuf :: from ( input) , externs, edition, cg, & matches, error_format,
512+ move |out| {
469513 let Output { krate, passes, renderinfo } = out;
470514 info ! ( "going to format" ) ;
471515 match output_format. as_ref ( ) . map ( |s| & * * s) {
@@ -509,13 +553,14 @@ fn acquire_input<R, F>(input: PathBuf,
509553 edition : Edition ,
510554 cg : CodegenOptions ,
511555 matches : & getopts:: Matches ,
556+ error_format : ErrorOutputType ,
512557 f : F )
513558 -> Result < R , String >
514559where R : ' static + Send , F : ' static + Send + FnOnce ( Output ) -> R {
515560 match matches. opt_str ( "r" ) . as_ref ( ) . map ( |s| & * * s) {
516- Some ( "rust" ) => Ok ( rust_input ( input, externs, edition, cg, matches, f) ) ,
561+ Some ( "rust" ) => Ok ( rust_input ( input, externs, edition, cg, matches, error_format , f) ) ,
517562 Some ( s) => Err ( format ! ( "unknown input format: {}" , s) ) ,
518- None => Ok ( rust_input ( input, externs, edition, cg, matches, f) )
563+ None => Ok ( rust_input ( input, externs, edition, cg, matches, error_format , f) )
519564 }
520565}
521566
@@ -546,6 +591,7 @@ fn rust_input<R, F>(cratefile: PathBuf,
546591 edition : Edition ,
547592 cg : CodegenOptions ,
548593 matches : & getopts:: Matches ,
594+ error_format : ErrorOutputType ,
549595 f : F ) -> R
550596where R : ' static + Send ,
551597 F : ' static + Send + FnOnce ( Output ) -> R
@@ -598,7 +644,7 @@ where R: 'static + Send,
598644 let ( mut krate, renderinfo) =
599645 core:: run_core ( paths, cfgs, externs, Input :: File ( cratefile) , triple, maybe_sysroot,
600646 display_warnings, crate_name. clone ( ) ,
601- force_unstable_if_unmarked, edition, cg) ;
647+ force_unstable_if_unmarked, edition, cg, error_format ) ;
602648
603649 info ! ( "finished with rustc" ) ;
604650
0 commit comments