AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
Arguments.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using System;
5 using System.Collections.Specialized;
6 using System.Text.RegularExpressions;
7 
12 namespace Utility{
13  public class Arguments : MonoBehaviour
14  {
15  private StringDictionary Parameters;
16 
22  public Arguments(string[] Args)
23  {
24  Parameters = new StringDictionary();
25  Regex Spliter = new Regex(@"^-{1,2}|^/|=|:",
26  RegexOptions.IgnoreCase|RegexOptions.Compiled);
27 
28  Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
29  RegexOptions.IgnoreCase|RegexOptions.Compiled);
30 
31  string Parameter = null;
32  string[] Parts;
33 
34  // Valid parameters forms:
35  // {-,/,--}param{ ,=,:}((",')value(",'))
36  // Examples:
37  // -param1 value1 --param2 /param3:"Test-:-work"
38  // /param4=happy -param5 '--=nice=--'
39  foreach(string Txt in Args)
40  {
41  // Look for new parameters (-,/ or --) and a
42  // possible enclosed value (=,:)
43  Parts = Spliter.Split(Txt,3);
44 
45  switch(Parts.Length){
46  // Found a value (for the last parameter
47  // found (space separator))
48  case 1:
49  if(Parameter != null)
50  {
51  if(!Parameters.ContainsKey(Parameter))
52  {
53  Parts[0] =
54  Remover.Replace(Parts[0], "$1");
55 
56  Parameters.Add(Parameter, Parts[0]);
57  }
58  Parameter=null;
59  }
60  // else Error: no parameter waiting for a value (skipped)
61  break;
62 
63  // Found just a parameter
64  case 2:
65  // The last parameter is still waiting.
66  // With no value, set it to true.
67  if(Parameter!=null)
68  {
69  if(!Parameters.ContainsKey(Parameter))
70  Parameters.Add(Parameter, "true");
71  }
72  Parameter=Parts[1];
73  break;
74 
75  // Parameter with enclosed value
76  case 3:
77  // The last parameter is still waiting.
78  // With no value, set it to true.
79  if(Parameter != null)
80  {
81  if(!Parameters.ContainsKey(Parameter))
82  Parameters.Add(Parameter, "true");
83  }
84 
85  Parameter = Parts[1];
86 
87  // Remove possible enclosing characters (",')
88  if(!Parameters.ContainsKey(Parameter))
89  {
90  Parts[2] = Remover.Replace(Parts[2], "$1");
91  Parameters.Add(Parameter, Parts[2]);
92  }
93 
94  Parameter=null;
95  break;
96  }
97  }
98  // In case a parameter is still waiting
99  if(Parameter != null)
100  {
101  if(!Parameters.ContainsKey(Parameter))
102  Parameters.Add(Parameter, "true");
103  }
104  }
105 
110  public string this [string Param]
111  {
112  get
113  {
114  return(Parameters[Param]);
115  }
116  }
117  }
118 
119 }
Utility
Simple Commandline Argument Parser https://www.codeproject.com/Articles/3111/C-NET-Command-Line-Argum...
Definition: Arguments.cs:12
Utility.Arguments
Definition: Arguments.cs:13
Utility.Arguments.Arguments
Arguments(string[] Args)
Constructor Parsing command line arguments
Definition: Arguments.cs:22