Skip to content

Commit 49bd0a0

Browse files
committed
Update ArgumentsManager.cs
Updated AddArguments method Updated Argument class : - Changed _names get property into a _names readonly variable - Removed unused constructors - Removed Contains method - Adding Equals, GetHashCode, == operator and != operator methods
1 parent 4e1a2c1 commit 49bd0a0

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

ProgramArgumentsManager/ArgumentsManager.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.CodeDom;
32
using System.Collections.Generic;
43
using System.Linq;
54

@@ -24,8 +23,8 @@ public ArgumentsManager(string name, string version)
2423

2524
public void AddArguments(string format, string description)
2625
{
27-
format = format.Trim();
28-
26+
format = format.Replace(" ", string.Empty);
27+
2928
if (!format.Contains("-") && !format.Contains("--"))
3029
throw new ArgumentException("Le format '" + format + " n'est pas comaptible en tant que paramètre !",
3130
nameof(format));
@@ -42,16 +41,40 @@ public void AddArguments(string format, string description)
4241

4342
private class Argument
4443
{
45-
private string[] _names { get; }
44+
private readonly string[] _names;
4645

47-
public Argument(string name) : this(new []{name}) { }
48-
public Argument(string name1, string name2) : this(new[] {name1, name2}) { }
4946
public Argument(string[] names)
5047
{
5148
_names = names;
5249
}
5350

54-
public bool Contain(string name) => _names.Contains(name);
51+
private bool Equals(Argument other) => other._names.Any(s => _names.Contains(s));
52+
53+
public override bool Equals(object obj)
54+
{
55+
if (obj is null)
56+
return false;
57+
if (ReferenceEquals(this, obj))
58+
return true;
59+
60+
return obj.GetType() == GetType() && Equals((Argument) obj);
61+
}
62+
63+
public override int GetHashCode()
64+
{
65+
// Same HashCode for each instance to force the use of the Equals method (for the Dictionnary key)
66+
return 0;
67+
}
68+
69+
public static bool operator ==(Argument left, Argument right)
70+
{
71+
return Equals(left, right);
72+
}
73+
74+
public static bool operator !=(Argument left, Argument right)
75+
{
76+
return !Equals(left, right);
77+
}
5578
}
5679
}
5780
}

0 commit comments

Comments
 (0)