11using System ;
2+ using System . Collections . Generic ;
23using System . Globalization ;
34using System . Linq ;
5+ using System . Text . RegularExpressions ;
46
57public static class CommandParser
6- {
8+ {
9+ private static readonly Regex _tokeniser = new Regex (
10+ @"[\""].+?[\""]|['].+?[']|[^ ]+" ,
11+ RegexOptions . Compiled ) ;
12+
13+ /// <summary>
14+ /// Takes the raw input of the user and returns the command name and it's converted parameters
15+ /// </summary>
16+ /// <param name="rawInput">Input sent by user</param>
17+ /// <param name="command">The commands name</param>
18+ /// <param name="args">The command parameters</param>
719 public static void StringToCommand ( string rawInput , out string command , out string [ ] args )
820 {
9- // TODO: Keep in single string when surrounded in quotes
10- var parts = rawInput . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
11- if ( parts . Length == 0 )
21+ // Skip empty input
22+ if ( string . IsNullOrWhiteSpace ( rawInput ) )
23+ {
24+ command = "" ;
25+ args = Array . Empty < string > ( ) ;
26+ return ;
27+ }
28+
29+ // Keep strings which are surrounded by " or '
30+ var matches = _tokeniser . Matches ( rawInput . Trim ( ) ) ;
31+ var tokens = new List < string > ( matches . Count ) ;
32+ foreach ( Match m in matches )
33+ {
34+ var t = m . Value ;
35+
36+ // Strip the quotes
37+ if ( ( t . StartsWith ( "\" " ) && t . EndsWith ( "\" " ) ) || ( t . StartsWith ( "'" ) && t . EndsWith ( "'" ) ) )
38+ {
39+ t = t . Substring ( 1 , t . Length - 2 ) ;
40+ }
41+ tokens . Add ( t ) ;
42+ }
43+
44+ // Skip if we found no tokens from past stripping
45+ if ( tokens . Count == 0 )
1246 {
1347 command = "" ;
1448 args = Array . Empty < string > ( ) ;
1549 return ;
1650 }
1751
18- // Return variables
19- command = parts [ 0 ] ;
20- args = parts . Length > 1 ? parts . Skip ( 1 ) . ToArray ( ) : Array . Empty < string > ( ) ;
52+ command = tokens [ 0 ] ;
53+ args = tokens . Skip ( 1 ) . ToArray ( ) ;
2154 }
2255
23- // Convert string into it's given type. E.g "1" = int, "STATE_IDLE" = enum, etc...
56+ /// <summary>
57+ /// Convert string into it's given type. E.g "1" = int, "STATE_IDLE" = enum, etc...
58+ /// </summary>
59+ /// <param name="raw">The raw string</param>
60+ /// <param name="targetType">Type of data we will convert to</param>
61+ /// <returns></returns>
62+ /// <exception cref="InvalidOperationException"></exception>
2463 public static object ConvertStringToType ( string raw , Type targetType )
2564 {
2665 // String Conversion
@@ -41,6 +80,7 @@ public static object ConvertStringToType(string raw, Type targetType)
4180 return Convert . ChangeType ( raw , targetType , CultureInfo . InvariantCulture ) ;
4281 }
4382
44- throw new InvalidOperationException ( $ "No converter for type { targetType . Name } ") ;
83+ // Data needs
84+ throw new InvalidOperationException ( $ "No converter for type { targetType . Name } ! Create conversion in .../Commander/Runtime/Core/CommandParse.cs") ;
4585 }
4686}
0 commit comments