1
+ namespace GrunCS
2
+ {
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . IO ;
6
+ using System . Linq ;
7
+ using System . Text . RegularExpressions ;
8
+ using System . Windows ;
9
+ using Antlr4 . Runtime ;
10
+ using NDesk . Options ;
11
+
12
+ /// <summary>
13
+ /// Interaction logic for App.xaml
14
+ /// </summary>
15
+ public partial class App
16
+ {
17
+ protected override void OnStartup ( StartupEventArgs e )
18
+ {
19
+ base . OnStartup ( e ) ;
20
+
21
+ bool showHelp = false ;
22
+ bool showGui = false ;
23
+ bool showTokens = false ;
24
+ bool showTree = false ;
25
+ string configFileLocation = null ;
26
+
27
+ // parse command line args
28
+ var options = new OptionSet
29
+ {
30
+ { "to|tokens" , "Print the tokens" , v => showTokens = true } ,
31
+ { "tr|tree" , "Print the tree" , v => showTree = true } ,
32
+ { "g|gui" , "Show the GUI" , v => showGui = true } ,
33
+ { "c|config" , "Specify an explicit config file (gruncs.json) location" , v => configFileLocation = v } ,
34
+ { "h|help" , "show this message and exit" , v => showHelp = true } ,
35
+ } ;
36
+
37
+ List < string > extraArgs = null ;
38
+ try
39
+ {
40
+ extraArgs = options . Parse ( e . Args ) ;
41
+ }
42
+ catch ( OptionException exception )
43
+ {
44
+ this . PrintInputException ( exception ) ;
45
+ this . Shutdown ( ) ;
46
+ return ;
47
+ }
48
+
49
+ // if help flag present, quit out
50
+ if ( showHelp )
51
+ {
52
+ options . WriteOptionDescriptions ( Console . Out ) ;
53
+ this . Shutdown ( ) ;
54
+ return ;
55
+ }
56
+
57
+ if ( extraArgs == null || extraArgs . Count < 3 )
58
+ {
59
+ this . PrintInputException ( new ArgumentException ( "Necessary arguments missing." ) ) ;
60
+ this . Shutdown ( ) ;
61
+ return ;
62
+ }
63
+
64
+ // load and parse
65
+ try
66
+ {
67
+ string testFilePath = Path . GetFullPath ( Path . Combine ( Environment . CurrentDirectory , extraArgs [ 2 ] ) ) ;
68
+
69
+ Main main = new Main
70
+ {
71
+ ShowTokens = showTokens ,
72
+ ShowTree = showTree ,
73
+ ConfigFileLocation = configFileLocation
74
+ } ;
75
+
76
+ main . Process ( Environment . CurrentDirectory , extraArgs [ 0 ] , extraArgs [ 1 ] , testFilePath ) ;
77
+ }
78
+ catch ( Exception exception )
79
+ {
80
+ Console . WriteLine ( "There was an error when trying to parse." ) ;
81
+ Console . WriteLine ( exception . Message ) ;
82
+ this . Shutdown ( ) ;
83
+ throw ;
84
+ }
85
+
86
+ // don't allow the window to show if the gui flag isn't set
87
+ if ( ! showGui )
88
+ {
89
+ this . Shutdown ( ) ;
90
+ }
91
+ }
92
+
93
+
94
+ private void PrintInputException ( Exception exception )
95
+ {
96
+ Console . Write ( "gruncs: " ) ;
97
+ Console . WriteLine ( exception . Message ) ;
98
+ Console . WriteLine ( "Try `gruncs --help for more information." ) ;
99
+ }
100
+
101
+
102
+ private void Shutdown ( )
103
+ {
104
+ System . Windows . Application . Current . Shutdown ( ) ;
105
+ }
106
+ }
107
+ }
0 commit comments