Skip to content

Commit 513a407

Browse files
committed
Update ArgumentsManager.cs
Edited Argument class: - Added a new private constructor - Added an implicit operator from string - Added ArgValues public class Edited the ArgumentsManager: - Replaced the the values type into _options from string to Argument.ArgValues - Added Parse method - Added IsSpecified method - Added GetValues and GetValue method
1 parent e11d09a commit 513a407

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

ProgramArgumentsManager/ArgumentsManager.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ArgumentsManager
99
public string ApplicationName { get; }
1010
public string Version { get; }
1111

12-
private Dictionary<Argument, string> _options;
12+
private Dictionary<Argument, Argument.ArgValues> _options;
1313

1414
public ArgumentsManager(string name) : this(name, "1.0.0") { }
1515

@@ -18,9 +18,11 @@ public ArgumentsManager(string name, string version)
1818
ApplicationName = name;
1919
Version = version;
2020

21-
_options = new Dictionary<Argument, string>();
21+
_options = new Dictionary<Argument, Argument.ArgValues>();
2222
}
2323

24+
private bool IsArgument(string s) => s.StartsWith("-") || s.StartsWith("--");
25+
2426
public void AddArguments(string format, string description)
2527
{
2628
format = format.Replace(" ", string.Empty);
@@ -36,13 +38,43 @@ public void AddArguments(string format, string description)
3638
throw new ArgumentException("Le format '" + format + " n'est pas comaptible en tant que paramètre !",
3739
nameof(format));
3840

39-
_options.Add(new Argument(formats), description);
41+
_options.Add(new Argument(formats), new Argument.ArgValues(description));
4042
}
4143

44+
public void Parse(string[] args)
45+
{
46+
string lastArg = null;
47+
foreach (string arg in args)
48+
{
49+
if (IsArgument(arg))
50+
{
51+
lastArg = arg;
52+
_options[lastArg].Specified = true;
53+
}
54+
else
55+
{
56+
_options[lastArg].Values.Add(arg);
57+
}
58+
}
59+
60+
foreach (Argument.ArgValues argValues in _options.Values.Where(argValues => !argValues.Specified))
61+
argValues.Values = null;
62+
}
63+
64+
public bool IsSpecified(string arg) => _options[arg].Specified;
65+
66+
public List<string> GetValues(string arg) => _options[arg].Values;
67+
68+
public string GetValue(string arg) => string.Join(" ", _options[arg]);
69+
4270
private class Argument
4371
{
4472
private readonly string[] _names;
4573

74+
private Argument(string name)
75+
{
76+
_names = new []{name};
77+
}
4678
public Argument(string[] names)
4779
{
4880
_names = names;
@@ -75,6 +107,23 @@ public override int GetHashCode()
75107
{
76108
return !Equals(left, right);
77109
}
110+
111+
public static implicit operator Argument(string s) => new Argument(s);
112+
113+
public class ArgValues
114+
{
115+
public string Description { get; set; }
116+
117+
public List<string> Values { get; set; }
118+
public bool Specified { get; set; }
119+
120+
public ArgValues(string description)
121+
{
122+
Description = description;
123+
Values = new List<string>();
124+
Specified = false;
125+
}
126+
}
78127
}
79128
}
80129
}

0 commit comments

Comments
 (0)