33import java .io .IOException ;
44import java .nio .file .Path ;
55import java .nio .file .Paths ;
6+ import org .apache .commons .cli .ParseException ;
67
78import apolang .interpreter .Controller ;
89
910public final class Interpreter
1011{
12+ private static final CliParser cliParser = new CliParser ();
13+
1114 public static void main (String [] args )
1215 throws IOException
1316 {
14- if (args .length == 0 )
17+ CliParser .Arguments arguments ;
18+
19+ try
1520 {
16- System .err .println ("No file specified. Execution stopped." );
17- return ;
21+ arguments = cliParser .parse (args );
1822 }
19-
20- int memorySize = 1 ;
21- String filename = args [0 ];
22-
23- if (args .length > 1 )
23+ catch (ParseException e )
2424 {
25- try
26- {
27- memorySize = Integer .parseInt (args [1 ]);
28- }
29- catch (Exception e )
30- {
31- System .err .println ("Invalid memory size. Execution stopped." );
32- return ;
33- }
25+ System .err .printf ("Error while parsing arguments:%n\t %s%n\t Execution stopped.%n" ,
26+ e .getMessage ());
27+ return ;
3428 }
3529
36- if (! filename . endsWith ( ".apo" ))
30+ if (arguments . isHelp ( ))
3731 {
38- System . err . println ( "Wrong filename extension. Execution stopped." );
32+ cliParser . printHelp ( );
3933 return ;
4034 }
4135
42- Path path = Paths .get (filename );
43- Controller controller = new Controller (memorySize , path );
36+ Path path = Paths .get (arguments . getFilename () );
37+ Controller controller = new Controller (arguments . getMemorySize () , path );
4438
4539 try
4640 {
4741 controller .parse ();
4842 }
4943 catch (Exception e )
5044 {
51- System .err .printf ("parser error>> %s\ n\t Execution stopped.\ n " , e );
45+ System .err .printf ("parser error>> %s% n\t Execution stopped.% n" , e );
5246 e .printStackTrace (System .err );
5347 return ;
5448 }
@@ -59,7 +53,7 @@ public static void main(String[] args)
5953 }
6054 catch (Exception e )
6155 {
62- System .err .printf ("interpreter error>> %s\ n\t Execution stopped.\ n " , e );
56+ System .err .printf ("interpreter error>> %s% n\t Execution stopped.% n" , e );
6357 e .printStackTrace (System .err );
6458 }
6559 }
0 commit comments