1111using System . IO ;
1212using System . Text . Json ;
1313using System . Threading . Tasks ;
14+ using DotNetCampus . Cli ;
15+ using DotNetCampus . MediaConverters . CommandLineHandlers ;
1416using SixLabors . ImageSharp . Formats . Png ;
1517using ErrorCode = DotNetCampus . MediaConverters . Contexts . MediaConverterErrorCode ;
1618
@@ -42,7 +44,7 @@ static async Task<int> Main(string[] args)
4244 await File . WriteAllTextAsync ( configurationFile , jsonText ) ;
4345 var outputFile = Path . Join ( testFolder . FullName , "1.png" ) ;
4446
45- return await RunAsync ( new Options ( )
47+ return await RunAsync ( new ConvertHandler ( )
4648 {
4749 InputFile = inputFile ,
4850 ConvertConfigurationFile = configurationFile ,
@@ -54,16 +56,17 @@ static async Task<int> Main(string[] args)
5456 } ) ;
5557 }
5658
57- var options = DotNetCampus . Cli . CommandLine . Parse ( args ) . As < Options > ( ) ;
58-
59- return await RunAsync ( options ) ;
59+ return await DotNetCampus . Cli . CommandLine . Parse ( args )
60+ . AddHandler < ConvertHandler > ( )
61+ . RunAsync ( )
62+ ;
6063 }
6164
62- internal static async Task < ErrorCode > RunAsync ( Options options )
65+ internal static async Task < ErrorCode > RunAsync ( ConvertHandler convertHandler )
6366 {
6467 var stopwatch = Stopwatch . StartNew ( ) ;
6568
66- var jsonText = await File . ReadAllTextAsync ( options . ConvertConfigurationFile ) ;
69+ var jsonText = await File . ReadAllTextAsync ( convertHandler . ConvertConfigurationFile ) ;
6770
6871 var imageConvertContext = ImageConvertContext . FromJsonText ( jsonText ) ;
6972
@@ -73,18 +76,18 @@ internal static async Task<ErrorCode> RunAsync(Options options)
7376 return ErrorCode . UnknownError ;
7477 }
7578
76- var inputFile = new FileInfo ( options . InputFile ) ;
79+ var inputFile = new FileInfo ( convertHandler . InputFile ) ;
7780
78- var workingFolder = Directory . CreateDirectory ( options . WorkingFolder ) ;
81+ var workingFolder = Directory . CreateDirectory ( convertHandler . WorkingFolder ) ;
7982
8083 var useAreaSizeLimit = imageConvertContext . UseAreaSizeLimit ?? true ;
8184 var copyNewFile = imageConvertContext . ShouldCopyNewFile ?? true ;
8285
8386 var context = new ImageFileOptimizationContext ( inputFile , workingFolder , imageConvertContext . MaxImageWidth ,
8487 imageConvertContext . MaxImageHeight )
8588 {
86- ShouldLogToConsole = options . ShouldLogToConsole ?? false ,
87- ShouldLogToFile = options . ShouldLogToFile ?? false ,
89+ ShouldLogToConsole = convertHandler . ShouldLogToConsole ?? false ,
90+ ShouldLogToFile = convertHandler . ShouldLogToFile ?? false ,
8891 } ;
8992 using var imageFileOptimizationResult = await ImageFileOptimization . OptimizeImageFileAsync ( context , useAreaSizeLimit , copyNewFile ) ;
9093
@@ -134,19 +137,19 @@ internal static async Task<ErrorCode> RunAsync(Options options)
134137 workerProvider . Run ( image , imageConvertTask ) ;
135138 }
136139
137- await image . SaveAsPngAsync ( options . OutputFile , new PngEncoder ( )
140+ await image . SaveAsPngAsync ( convertHandler . OutputFile , new PngEncoder ( )
138141 {
139142 ColorType = PngColorType . RgbWithAlpha ,
140143 BitDepth = PngBitDepth . Bit8 ,
141144 } ) ;
142145 }
143146 else
144147 {
145- optimizedImageFile . CopyTo ( options . OutputFile , overwrite : true ) ;
148+ optimizedImageFile . CopyTo ( convertHandler . OutputFile , overwrite : true ) ;
146149 }
147150
148151 stopwatch . Stop ( ) ;
149- context . LogMessage ( $ "Success converted image. Cost { stopwatch . ElapsedMilliseconds } ms. OutputFile:'{ options . OutputFile } '") ;
152+ context . LogMessage ( $ "Success converted image. Cost { stopwatch . ElapsedMilliseconds } ms. OutputFile:'{ convertHandler . OutputFile } '") ;
150153
151154 return ErrorCode . Success ;
152155 }
0 commit comments