11using System ;
22using System . Collections ;
3+ using System . ComponentModel ;
34using System . Drawing ;
45using System . Globalization ;
56using System . IO ;
@@ -19,117 +20,94 @@ class Program
1920 static int Main ( string [ ] args )
2021 {
2122 // Parse command-line arguments
22- var errorCode = Parser . Default . ParseArguments < GenerateOptions , ParseOptions > ( args )
23- . MapResult (
24- ( GenerateOptions options ) => GeneratePattern ( options ) ,
25- ( ParseOptions options ) => ParseFontImage ( options ) ,
26- errs => ErrorCode . ArgumentsNotParsed ) ;
27-
28- if ( errorCode == ErrorCode . NoError )
29- ConsoleLogger . WriteMessage ( $ "SUCCESS! \n File written: \" { _outputFileName } \" ", MessageType . Info ) ;
30- else
23+ try
3124 {
32- NotifyError ( errorCode ) ;
25+ Parser . Default . ParseArguments < GenerateOptions , ParseOptions > ( args )
26+ . MapResult (
27+ ( GenerateOptions options ) => GeneratePattern ( options ) ,
28+ ( ParseOptions options ) => ParseFontImage ( options ) ,
29+ // just exit, this usually means that no arguments were given
30+ ( errs ) =>
31+ {
32+ System . Environment . Exit ( 1 ) ;
33+ return null ;
34+ } ) ;
3335 }
34- return ( int ) errorCode ;
36+ catch ( Exception e )
37+ {
38+ NotifyError ( e ) ;
39+ return 1 ;
40+ }
41+
42+ ConsoleLogger . WriteMessage ( $ "SUCCESS! \n File written: \" { _outputFileName } \" ", MessageType . Info ) ;
43+
44+ return 0 ;
3545 }
3646
3747 // Notifies user about error details
38- private static void NotifyError ( ErrorCode errorCode )
48+ private static void NotifyError ( Exception e )
3949 {
40- // todo: add more details
41- switch ( errorCode )
50+ switch ( e )
4251 {
43- case ErrorCode . UknownError :
44- ConsoleLogger . WriteMessage ( "There was error, but we have no idea why." , MessageType . Error ) ;
45- break ;
46- case ErrorCode . ArgumentsMismatch :
47- ConsoleLogger . WriteMessage ( "Error parsing arguments. Check command line." , MessageType . Error ) ;
48- break ;
49- case ErrorCode . FileNotFound :
50- ConsoleLogger . WriteMessage ( "File was not found" , MessageType . Error ) ;
51- break ;
52- case ErrorCode . FileParsingError :
53- ConsoleLogger . WriteMessage ( "Error parsing file" , MessageType . Error ) ;
54- break ;
55- case ErrorCode . ArgumentsNotParsed :
56- //do nothings as this usually means that no arguments were passed to command line
57- break ;
52+ case FileNotFoundException _:
53+ ConsoleLogger . WriteMessage ( $ "File not found: { e . Message } ", MessageType . Error ) ;
54+ return ;
55+ case ArgumentException _:
56+ ConsoleLogger . WriteMessage ( $ "Error parsing file: { e . Message } ", MessageType . Error ) ;
57+ return ;
58+ default :
59+ ConsoleLogger . WriteMessage ( $ "Unexpected error: { e . Message } ", MessageType . Error ) ;
60+ return ;
5861 }
5962 }
6063
6164 /// <summary>
6265 /// parses image and write arrays to output file
6366 /// </summary>
6467 /// <param name="options">parsed command line args</param>
65- /// <returns>error code </returns>
66- static ErrorCode ParseFontImage ( ParseOptions options )
68+ /// <returns>always returns null </returns>
69+ static object ParseFontImage ( ParseOptions options )
6770 {
68- try
71+ if ( options . ExcessValue != null )
6972 {
70- if ( options . ExcessValue != null )
71- return ErrorCode . ArgumentsMismatch ;
73+ throw new ArgumentException ( "Argument mismatch!" ) ;
74+ }
7275
73- Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
74- _outputFileName = options . OutputFileName ;
76+ Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
77+ _outputFileName = options . OutputFileName ;
7578
76- var bitmap = new Bitmap ( Image . FromFile ( options . InputFileName ) ) ;
77- var mapper = new PixelMapper ( bitmap , Settings ) ;
78- var map = mapper . MapPixels ( options . SkipHeaders ) ;
79- OutputFileFormatter . WriteOutput ( map , options . OutputFileName , options . SingleArray , options . ArrayContentOnly ) ;
80- }
81- catch ( Exception e )
82- {
83- switch ( e )
84- {
85- case FileNotFoundException _:
86- return ErrorCode . FileNotFound ;
87- case ArgumentException _:
88- return ErrorCode . FileParsingError ;
89- default :
90- return ErrorCode . UknownError ;
91- }
92- }
93- return ErrorCode . NoError ;
79+ var bitmap = new Bitmap ( Image . FromFile ( options . InputFileName ) ) ;
80+ var mapper = new PixelMapper ( bitmap , Settings ) ;
81+ var map = mapper . MapPixels ( options . SkipHeaders ) ;
82+ OutputFileFormatter . WriteOutput ( map , options . OutputFileName , options . SingleArray , options . ArrayContentOnly ) ;
83+
84+ return null ;
9485 }
9586
9687 /// <summary>
9788 /// Grid pattern generation and writing to file
9889 /// </summary>
9990 /// <param name="options">parsed command line args</param>
100- /// <returns>error code </returns>
101- static ErrorCode GeneratePattern ( GenerateOptions options )
91+ /// <returns>always returns null </returns>
92+ static object GeneratePattern ( GenerateOptions options )
10293 {
103- try
94+ if ( options . ExcessValue != null )
10495 {
105- if ( options . ExcessValue != null )
106- return ErrorCode . ArgumentsMismatch ;
96+ throw new ArgumentException ( "Argument mismatch!" ) ;
97+ }
10798
108- Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
109- _outputFileName = options . OutputFileName ;
99+ Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
100+ _outputFileName = options . OutputFileName ;
110101
111- var generator = new PatternGenerator ( Settings ) ;
112- byte [ ] sampleData = null ;
113- if ( options . InputFileName != null )
114- sampleData = ParseDataFile ( options . InputFileName ) ;
115- var pattern = generator . GeneratePattern ( options . PatternWidth ,
116- options . PatternHeight , options . EnumerationStyle , sampleData ) ;
117- pattern . Save ( options . OutputFileName ) ;
118- }
119- catch ( Exception e )
120- {
121- switch ( e )
122- {
123- case PixelProcessingException _:
124- ConsoleLogger . WriteMessage ( e . Message , MessageType . Error ) ;
125- return ErrorCode . FileParsingError ;
126- case FileNotFoundException _:
127- return ErrorCode . FileNotFound ;
128- default :
129- return ErrorCode . UknownError ;
130- }
131- }
132- return ErrorCode . NoError ;
102+ var generator = new PatternGenerator ( Settings ) ;
103+ byte [ ] sampleData = null ;
104+ if ( options . InputFileName != null )
105+ sampleData = ParseDataFile ( options . InputFileName ) ;
106+ var pattern = generator . GeneratePattern ( options . PatternWidth ,
107+ options . PatternHeight , options . EnumerationStyle , sampleData ) ;
108+ pattern . Save ( options . OutputFileName ) ;
109+
110+ return null ;
133111 }
134112
135113 // Parses file with font hex'es (csv file with hex values e.g. 0xDE, 0xAD, 0xBE, 0xEF ...)
0 commit comments