Skip to content

Commit 683b178

Browse files
Randall FlaggRandall Flagg
authored andcommitted
Moved CmdLine back to LogExpert as it is not part of the core logic of the application.
1 parent 8e706ab commit 683b178

8 files changed

Lines changed: 544 additions & 517 deletions

File tree

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Taken from https://cmdline.codeplex.com/
3+
*
4+
*/
5+
6+
//TODO: Replace with https://github.com/commandlineparser/commandline
7+
//TODO: or with this https://github.com/natemcmaster/CommandLineUtils
8+
namespace LogExpert.Classes.CommandLine
9+
{
10+
/// <summary>
11+
/// Represents an string command line parameter.
12+
/// </summary>
13+
public class CmdLineString(string name, bool required, string helpMessage) : CmdLineParameter(name, required, helpMessage)
14+
{
15+
16+
#region Public methods
17+
18+
public static implicit operator string(CmdLineString s)
19+
{
20+
return s.Value;
21+
}
22+
23+
#endregion
24+
}
25+
26+
/// <summary>
27+
/// Provides a simple strongly typed interface to work with command line parameters.
28+
/// </summary>
29+
public class CmdLine
30+
{
31+
#region Fields
32+
33+
// A private dictonary containing the parameters.
34+
private readonly Dictionary<string, CmdLineParameter> parameters = [];
35+
36+
#endregion
37+
38+
#region cTor
39+
40+
/// <summary>
41+
/// Creats a new empty command line object.
42+
/// </summary>
43+
public CmdLine()
44+
{
45+
}
46+
47+
#endregion
48+
49+
#region Properties
50+
51+
/// <summary>
52+
/// Returns a command line parameter by the name.
53+
/// </summary>
54+
/// <param name="name">The name of the parameter (the word after the initial hyphen (-).</param>
55+
/// <returns>A reference to the named comman line object.</returns>
56+
public CmdLineParameter this[string name]
57+
{
58+
get
59+
{
60+
if (parameters.TryGetValue(name, out CmdLineParameter value) == false)
61+
{
62+
throw new CmdLineException(name, "Not a registered parameter.");
63+
}
64+
return value;
65+
}
66+
}
67+
68+
#endregion
69+
70+
#region Public methods
71+
72+
/// <summary>
73+
/// Registers a parameter to be used and adds it to the help screen.
74+
/// </summary>
75+
/// <param name="parameter">The parameter to add.</param>
76+
public void RegisterParameter(CmdLineParameter parameter)
77+
{
78+
if (parameters.ContainsKey(parameter.Name))
79+
{
80+
throw new CmdLineException(parameter.Name, "Parameter is already registered.");
81+
}
82+
parameters.Add(parameter.Name, parameter);
83+
}
84+
85+
/// <summary>
86+
/// Registers parameters to be used and adds hem to the help screen.
87+
/// </summary>
88+
/// <param name="parameters">The parameter to add.</param>
89+
public void RegisterParameter(CmdLineParameter[] parameters)
90+
{
91+
foreach (CmdLineParameter p in parameters)
92+
{
93+
RegisterParameter(p);
94+
}
95+
}
96+
97+
98+
/// <summary>
99+
/// Parses the command line and sets the value of each registered parmaters.
100+
/// </summary>
101+
/// <param name="args">The arguments array sent to main()</param>
102+
/// <returns>Any reminding strings after arguments has been processed.</returns>
103+
public string[] Parse(string[] args)
104+
{
105+
int i = 0;
106+
107+
List<string> new_args = [];
108+
109+
while (i < args.Length)
110+
{
111+
if (args[i].Length > 1 && args[i][0] == '-')
112+
{
113+
// The current string is a parameter name
114+
string key = args[i][1..].ToLower();
115+
string argsValue = string.Empty;
116+
i++;
117+
if (i < args.Length)
118+
{
119+
if (args[i].Length > 0 && args[i][0] == '-')
120+
{
121+
// The next string is a new parameter, do not nothing
122+
}
123+
else
124+
{
125+
// The next string is a value, read the value and move forward
126+
argsValue = args[i];
127+
i++;
128+
}
129+
}
130+
if (parameters.TryGetValue(key, out CmdLineParameter cmdLineParameter) == false)
131+
{
132+
throw new CmdLineException(key, "Parameter is not allowed.");
133+
}
134+
135+
if (cmdLineParameter.Exists)
136+
{
137+
throw new CmdLineException(key, "Parameter is specified more than once.");
138+
}
139+
140+
cmdLineParameter.SetValue(argsValue);
141+
}
142+
else
143+
{
144+
new_args.Add(args[i]);
145+
i++;
146+
}
147+
}
148+
149+
150+
// Check that required parameters are present in the command line.
151+
foreach (string key in parameters.Keys)
152+
{
153+
if (parameters[key].Required && parameters[key].Exists == false)
154+
{
155+
throw new CmdLineException(key, "Required parameter is not found.");
156+
}
157+
}
158+
159+
return new_args.ToArray();
160+
}
161+
162+
/// <summary>
163+
/// Generates the help screen.
164+
/// </summary>
165+
public string HelpScreen()
166+
{
167+
int len = 0;
168+
foreach (string key in parameters.Keys)
169+
{
170+
len = Math.Max(len, key.Length);
171+
}
172+
173+
string help = "\nParameters:\n\n";
174+
foreach (string key in parameters.Keys)
175+
{
176+
string s = "-" + parameters[key].Name;
177+
while (s.Length < len + 3)
178+
{
179+
s += " ";
180+
}
181+
s += parameters[key].Help + "\n";
182+
help += s;
183+
}
184+
return help;
185+
}
186+
187+
#endregion
188+
}
189+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Taken from https://cmdline.codeplex.com/
3+
*
4+
*/
5+
6+
//TODO: Replace with https://github.com/commandlineparser/commandline
7+
//TODO: or with this https://github.com/natemcmaster/CommandLineUtils
8+
namespace LogExpert.Classes.CommandLine
9+
{
10+
/// <summary>
11+
/// Represents an error occuring during command line parsing.
12+
/// </summary>
13+
public class CmdLineException : Exception
14+
{
15+
#region cTor
16+
17+
public CmdLineException(string parameter, string message)
18+
:
19+
base(string.Format("Syntax error of parameter -{0}: {1}", parameter, message))
20+
{
21+
}
22+
23+
public CmdLineException(string message)
24+
:
25+
base(message)
26+
{
27+
}
28+
29+
#endregion
30+
}
31+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Taken from https://cmdline.codeplex.com/
3+
*
4+
*/
5+
6+
//TODO: Replace with https://github.com/commandlineparser/commandline
7+
//TODO: or with this https://github.com/natemcmaster/CommandLineUtils
8+
namespace LogExpert.Classes.CommandLine
9+
{
10+
/// <summary>
11+
/// Represents an integer command line parameter.
12+
/// </summary>
13+
public class CmdLineInt : CmdLineParameter
14+
{
15+
#region Fields
16+
17+
private readonly int _max = int.MaxValue;
18+
private readonly int _min = int.MinValue;
19+
20+
#endregion
21+
22+
#region cTor
23+
24+
/// <summary>
25+
/// Creates a new instance of this class.
26+
/// </summary>
27+
/// <param name="name">Name of parameter.</param>
28+
/// <param name="required">Require that the parameter is present in the command line.</param>
29+
/// <param name="helpMessage">The explanation of the parameter to add to the help screen.</param>
30+
public CmdLineInt(string name, bool required, string helpMessage)
31+
: base(name, required, helpMessage)
32+
{
33+
}
34+
35+
/// <summary>
36+
/// Creates a new instance of this class.
37+
/// </summary>
38+
/// <param name="name">Name of parameter.</param>
39+
/// <param name="required">Require that the parameter is present in the command line.</param>
40+
/// <param name="helpMessage">The explanation of the parameter to add to the help screen.</param>
41+
/// <param name="min">The minimum value of the parameter.</param>
42+
/// <param name="max">The maximum valie of the parameter.</param>
43+
public CmdLineInt(string name, bool required, string helpMessage, int min, int max)
44+
: base(name, required, helpMessage)
45+
{
46+
_min = min;
47+
_max = max;
48+
}
49+
50+
#endregion
51+
52+
#region Properties
53+
54+
/// <summary>
55+
/// Returns the current value of the parameter.
56+
/// </summary>
57+
new public int Value { get; private set; }
58+
59+
#endregion
60+
61+
#region Public methods
62+
63+
/// <summary>
64+
/// Sets the value of the parameter.
65+
/// </summary>
66+
/// <param name="value">A string containing a integer expression.</param>
67+
public override void SetValue(string value)
68+
{
69+
base.SetValue(value);
70+
int i = 0;
71+
try
72+
{
73+
i = Convert.ToInt32(value);
74+
}
75+
catch (Exception)
76+
{
77+
throw new CmdLineException(Name, "Value is not an integer.");
78+
}
79+
if (i < _min)
80+
{
81+
throw new CmdLineException(Name, string.Format("Value must be greather or equal to {0}.", _min));
82+
}
83+
if (i > _max)
84+
{
85+
throw new CmdLineException(Name, string.Format("Value must be less or equal to {0}.", _max));
86+
}
87+
Value = i;
88+
}
89+
90+
/// <summary>
91+
/// A implicit converion to a int data type.
92+
/// </summary>
93+
/// <param name="s"></param>
94+
/// <returns></returns>
95+
public static implicit operator int(CmdLineInt s)
96+
{
97+
return s.Value;
98+
}
99+
100+
#endregion
101+
}
102+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Taken from https://cmdline.codeplex.com/
3+
*
4+
*/
5+
6+
//TODO: Replace with https://github.com/commandlineparser/commandline
7+
//TODO: or with this https://github.com/natemcmaster/CommandLineUtils
8+
namespace LogExpert.Classes.CommandLine
9+
{
10+
/// <summary>
11+
/// Represents a command line parameter.
12+
/// Parameters are words in the command line beginning with a hyphen (-).
13+
/// The value of the parameter is the next word in
14+
/// </summary>
15+
/// <remarks>
16+
/// Creates a new instance of this class.
17+
/// </remarks>
18+
/// <param name="name">Name of parameter.</param>
19+
/// <param name="required">Require that the parameter is present in the command line.</param>
20+
/// <param name="helpMessage">The explanation of the parameter to add to the help screen.</param>
21+
public class CmdLineParameter(string name, bool required, string helpMessage)
22+
{
23+
#region Properties
24+
25+
/// <summary>
26+
/// Returns the value of the parameter.
27+
/// </summary>
28+
public string Value { get; private set; } = "";
29+
30+
/// <summary>
31+
/// Returns the help message associated with the parameter.
32+
/// </summary>
33+
public string Help { get; } = helpMessage;
34+
35+
/// <summary>
36+
/// Returns true if the parameter was found in the command line.
37+
/// </summary>
38+
public bool Exists { get; private set; } = false;
39+
40+
/// <summary>
41+
/// Returns true if the parameter is required in the command line.
42+
/// </summary>
43+
public bool Required { get; } = required;
44+
45+
/// <summary>
46+
/// Returns the name of the parameter.
47+
/// </summary>
48+
public string Name { get; } = name;
49+
50+
#endregion
51+
52+
#region Public methods
53+
54+
/// <summary>
55+
/// Sets the value of the parameter.
56+
/// </summary>
57+
/// <param name="value">A string containing a integer expression.</param>
58+
public virtual void SetValue(string value)
59+
{
60+
Value = value;
61+
Exists = true;
62+
}
63+
64+
#endregion
65+
}
66+
}

0 commit comments

Comments
 (0)