1
1
using System ;
2
2
using System . Collections ;
3
+ using System . ComponentModel ;
3
4
using System . Drawing ;
4
5
using System . Globalization ;
5
6
using System . IO ;
@@ -19,117 +20,94 @@ class Program
19
20
static int Main ( string [ ] args )
20
21
{
21
22
// 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
31
24
{
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
+ } ) ;
33
35
}
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 ;
35
45
}
36
46
37
47
// Notifies user about error details
38
- private static void NotifyError ( ErrorCode errorCode )
48
+ private static void NotifyError ( Exception e )
39
49
{
40
- // todo: add more details
41
- switch ( errorCode )
50
+ switch ( e )
42
51
{
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 ;
58
61
}
59
62
}
60
63
61
64
/// <summary>
62
65
/// parses image and write arrays to output file
63
66
/// </summary>
64
67
/// <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 )
67
70
{
68
- try
71
+ if ( options . ExcessValue != null )
69
72
{
70
- if ( options . ExcessValue != null )
71
- return ErrorCode . ArgumentsMismatch ;
73
+ throw new ArgumentException ( "Argument mismatch!" ) ;
74
+ }
72
75
73
- Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
74
- _outputFileName = options . OutputFileName ;
76
+ Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
77
+ _outputFileName = options . OutputFileName ;
75
78
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 ;
94
85
}
95
86
96
87
/// <summary>
97
88
/// Grid pattern generation and writing to file
98
89
/// </summary>
99
90
/// <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 )
102
93
{
103
- try
94
+ if ( options . ExcessValue != null )
104
95
{
105
- if ( options . ExcessValue != null )
106
- return ErrorCode . ArgumentsMismatch ;
96
+ throw new ArgumentException ( "Argument mismatch!" ) ;
97
+ }
107
98
108
- Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
109
- _outputFileName = options . OutputFileName ;
99
+ Settings = PixelSettings . FromFile ( options . PixelSettingsPath ) ;
100
+ _outputFileName = options . OutputFileName ;
110
101
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 ;
133
111
}
134
112
135
113
// Parses file with font hex'es (csv file with hex values e.g. 0xDE, 0xAD, 0xBE, 0xEF ...)
0 commit comments