Skip to content

Commit 7302722

Browse files
committed
Update ArgumentsManager.cs
ArgumentsManager: - Renamed ApplicationName into AppName - Added AppDescription - Added readonly modifier on _options - Added constructor with description - Added ShowUsage method - Added GetUsage method ArgumentsManager.Argument class: - Added ToString method ArgumentsManager.Argument.ArgValues: - Removed setter from Description
1 parent 0be59f6 commit 7302722

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

ProgramArgumentsManager/ArgumentsManager.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ namespace ProgramArgumentsManager
66
{
77
public class ArgumentsManager
88
{
9-
public string ApplicationName { get; }
9+
public string AppName { get; }
10+
public string AppDescription { get; }
1011
public string Version { get; }
1112

12-
private Dictionary<Argument, Argument.ArgValues> _options;
13+
private readonly Dictionary<Argument, Argument.ArgValues> _options;
1314

1415
public ArgumentsManager(string name) : this(name, "1.0.0") { }
1516

16-
public ArgumentsManager(string name, string version)
17+
public ArgumentsManager(string name, string version) : this(name, version, null) { }
18+
19+
public ArgumentsManager(string name, string version, string description)
1720
{
18-
ApplicationName = name;
21+
AppName = name;
1922
Version = version;
23+
AppDescription = description;
2024

2125
_options = new Dictionary<Argument, Argument.ArgValues>();
2226
}
@@ -67,8 +71,24 @@ public void Parse(string[] args)
6771

6872
public string GetValue(string arg) => _options[arg].Values is null ? null : string.Join(" ", _options[arg].Values);
6973

74+
public void ShowUsage() => Console.WriteLine(GetUsage());
75+
76+
public string GetUsage()
77+
{
78+
string usage = AppName + ":\n\n";
79+
if (!(AppDescription is null))
80+
usage += "\t" + AppDescription.Replace("\n", "\n\t") + "\n\n";
81+
82+
usage += "USAGE: " + AppDomain.CurrentDomain.FriendlyName + "\n";
83+
84+
return _options.Aggregate(usage,
85+
(current, option) =>
86+
current + option.Key.ToString().PadLeft(20) + " = " + option.Value.Description + "\n");
87+
}
88+
7089
private class Argument
7190
{
91+
/*public enum Format
7292
private readonly string[] _names;
7393

7494
private Argument(string name)
@@ -82,6 +102,8 @@ public Argument(string[] names)
82102

83103
private bool Equals(Argument other) => other._names.Any(s => _names.Contains(s));
84104

105+
public override string ToString() => string.Join(", ", _names);
106+
85107
public override bool Equals(object obj)
86108
{
87109
if (obj is null)
@@ -112,7 +134,7 @@ public override int GetHashCode()
112134

113135
public class ArgValues
114136
{
115-
public string Description { get; set; }
137+
public string Description { get; }
116138

117139
public List<string> Values { get; set; }
118140
public bool Specified { get; set; }

0 commit comments

Comments
 (0)