1+ // original source: https://www.codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser
2+
3+ using System ;
4+ using System . Collections . Specialized ;
5+ using System . Text . RegularExpressions ;
6+
7+ namespace CommandLine
8+ {
9+ public static class Arguments
10+ {
11+
12+ public static bool TryGetOptions ( string [ ] args , bool inConsole , out string mode , out ushort port , out bool https )
13+ {
14+ var arguments = Parse ( args ) ;
15+ var validArgs = true ;
16+
17+ mode = arguments [ "m" ] ?? arguments [ "mode" ] ?? "start" ;
18+ https = arguments [ "http" ] == null && arguments [ "https" ] != null ;
19+ var portString = arguments [ "p" ] ?? arguments [ "port" ] ?? "8080" ;
20+
21+ if ( mode != "start" && mode != "attach" && mode != "kill" )
22+ {
23+ if ( inConsole )
24+ {
25+ Console . ForegroundColor = ConsoleColor . Red ;
26+ Console . WriteLine ( "Invalid mode; Allowed values are start | attach | kill" ) ;
27+ Console . ResetColor ( ) ;
28+ }
29+ validArgs = false ;
30+ }
31+
32+ if ( ! ushort . TryParse ( portString , out port ) || port < 80 )
33+ {
34+ if ( inConsole )
35+ {
36+ Console . ForegroundColor = ConsoleColor . Red ;
37+ Console . WriteLine ( "Invalid port number specified." ) ;
38+ Console . ResetColor ( ) ;
39+ }
40+ validArgs = false ;
41+ }
42+
43+ if ( arguments [ "h" ] != null || arguments [ "help" ] != null ) validArgs = false ;
44+
45+ if ( inConsole && ! validArgs )
46+ {
47+ Console . WriteLine ( ) ;
48+ Console . WriteLine ( " Mode Argument Options (Defaults to start)" ) ;
49+ Console . WriteLine ( " -m | --mode start -> Start the SPA server and proxy to that." ) ;
50+ Console . WriteLine ( " -m | --mode attach -> Attach to existing SPA server" ) ;
51+ Console . WriteLine ( " -m | --mode kill -> Shutdown any existing SPA server on the specified port (used after debugging in VS Code)" ) ;
52+ Console . WriteLine ( ) ;
53+ Console . WriteLine ( " Port Argument (Defaults to 8080)" ) ;
54+ Console . WriteLine ( " -p | --port 8080 -> Specify what port to start or attach to, minimum of 80" ) ;
55+ Console . WriteLine ( ) ;
56+ Console . WriteLine ( " HTTPS (Defaults to false)" ) ;
57+ Console . WriteLine ( " -https -> Uses HTTPS" ) ;
58+ Console . WriteLine ( ) ;
59+
60+ }
61+
62+ return validArgs ;
63+
64+ }
65+
66+ public static StringDictionary Parse ( string [ ] args )
67+ {
68+ var parameters = new StringDictionary ( ) ;
69+ Regex splitter = new Regex ( @"^-{1,2}|^/|=|:" ,
70+ RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
71+
72+ Regex remover = new Regex ( @"^['""]?(.*?)['""]?$" ,
73+ RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
74+
75+ string parameter = null ;
76+ string [ ] parts ;
77+
78+ // Valid parameters forms:
79+ // {-,/,--}param{ ,=,:}((",')value(",'))
80+ // Examples:
81+ // -param1 value1 --param2 /param3:"Test-:-work"
82+ // /param4=happy -param5 '--=nice=--'
83+ foreach ( string txt in args )
84+ {
85+ // Look for new parameters (-,/ or --) and a
86+ // possible enclosed value (=,:)
87+ parts = splitter . Split ( txt , 3 ) ;
88+
89+ switch ( parts . Length )
90+ {
91+ // Found a value (for the last parameter
92+ // found (space separator))
93+ case 1 :
94+ if ( parameter != null )
95+ {
96+ if ( ! parameters . ContainsKey ( parameter ) )
97+ {
98+ parts [ 0 ] =
99+ remover . Replace ( parts [ 0 ] , "$1" ) ;
100+
101+ parameters . Add ( parameter , parts [ 0 ] ) ;
102+ }
103+ parameter = null ;
104+ }
105+ // else Error: no parameter waiting for a value (skipped)
106+ break ;
107+
108+ // Found just a parameter
109+ case 2 :
110+ // The last parameter is still waiting.
111+ // With no value, set it to true.
112+ if ( parameter != null && ! parameters . ContainsKey ( parameter ) )
113+ parameters . Add ( parameter , "true" ) ;
114+
115+ parameter = parts [ 1 ] ;
116+ break ;
117+
118+ // Parameter with enclosed value
119+ case 3 :
120+ // The last parameter is still waiting.
121+ // With no value, set it to true.
122+ if ( parameter != null && ! parameters . ContainsKey ( parameter ) )
123+ parameters . Add ( parameter , "true" ) ;
124+
125+ parameter = parts [ 1 ] ;
126+
127+ // Remove possible enclosing characters (",')
128+ if ( ! parameters . ContainsKey ( parameter ) )
129+ {
130+ parts [ 2 ] = remover . Replace ( parts [ 2 ] , "$1" ) ;
131+ parameters . Add ( parameter , parts [ 2 ] ) ;
132+ }
133+
134+ parameter = null ;
135+ break ;
136+ }
137+ }
138+ // In case a parameter is still waiting
139+ if ( parameter != null && ! parameters . ContainsKey ( parameter ) )
140+ parameters . Add ( parameter , "true" ) ;
141+
142+ return parameters ;
143+ }
144+ }
145+ }
0 commit comments