Skip to content

Commit

Permalink
Add ShowErrors() method, support indented output
Browse files Browse the repository at this point in the history
  • Loading branch information
kcartlidge committed Aug 21, 2023
1 parent 0df9dc2 commit 9124ba2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
24 changes: 24 additions & 0 deletions ArgsParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,5 +463,29 @@ public void OptionsAndFlagsProvided_JustDashes_HasErrors()
Assert.Contains("Option received with no name", result.ArgumentErrors.Values.ToList());
Assert.Contains("Flag received with no name", result.ArgumentErrors.Values.ToList());
}

/// <summary>
/// For ease of development you can uncomment this test
/// and perform whatever actions needed.
///
/// NO CHANGES TO THIS TEST SHOULD BE CHECKED IN.
/// </summary>
[Test]
public void TestbedForDevelopment()
{
//var args = new string[] { "-run", "data", "Site Title", "--serve", "-ignore", "-port", "3000" };
//var parser = new Parser(args)
// .SupportsOption<int>("port", "Port to start the dev server on", 1337)
// .RequiresOption<string>("read", "Folder to read the site from", "site")
// .RequiresOption<string>("write", "Folder to write the result to")
// .SupportsFlag("serve", "Start the site going in a dev server")
// .SupportsFlag("force", "Overwrite any destination content");

//parser.Help(2);
//parser.Parse();
//parser.ShowErrors(2);

Assert.Pass();
}
}
}
25 changes: 22 additions & 3 deletions ArgsParser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ public Parser SupportsFlag(string flagName, string info)
/// Displays informative help for all flags and options via the Console (stdout).
/// Groups into sections for clarity, and sorts within those sections.
/// </summary>
public Parser Help()
public Parser Help(int indent = 0)
{
if (indent < 0) throw new Exception($"A negative indent ({indent}) is not allowed.");

var pad = "".PadLeft(indent);
var req = "(required)";
var width = Math.Max(maxOptionWidth, maxFlagWidth);
var optionWidth = width;
Expand All @@ -84,15 +87,15 @@ public Parser Help()
{
var required = option.Value.IsRequired ? req : "";
var key = option.Key.PadRight(optionWidth);
Console.WriteLine($"-{key} <value> {option.Value.Info} {required}");
Console.WriteLine($"{pad}-{key} <value> {option.Value.Info} {required}");
}
var flagWidth = width + (knownOptions.Any() ? " <value>".Length : 0);
if (knownFlags.Any()) Console.WriteLine();
foreach (var flag in knownFlags.OrderByDescending(x => x.Value.IsRequired).ThenBy(y => y.Key))
{
var required = flag.Value.IsRequired ? req : "";
var key = flag.Key.PadRight(flagWidth);
Console.WriteLine($"-{key} {flag.Value.Info} {required}");
Console.WriteLine($"{pad}-{key} {flag.Value.Info} {required}");
}
Console.WriteLine();
return this;
Expand Down Expand Up @@ -196,6 +199,22 @@ public Parser Parse()
return this;
}

/// <summary>Displays a list of key/value argument error(s).</summary>
public void ShowErrors(int indent = 0)
{
if (HasErrors == false) return;
if (indent < 0) throw new Exception($"A negative indent ({indent}) is not allowed.");

var pad = "".PadLeft(indent);
Console.WriteLine();
foreach (var error in ExpectationErrors)
Console.WriteLine($"{pad}{error.Value}");
foreach (var error in ArgumentErrors)
Console.WriteLine($"{pad}{error.Value}");
Console.WriteLine();
return;
}

/// <summary>Is the named flag present?</summary>
public bool IsFlagProvided(string flagName)
{
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 21st August 2023 - v4.0.2

- Add `ShowErrors()` method
- Support indented output

## 21st August 2023 - v4.0.1

- Fix `Help()` when no options or no flags
Expand Down

0 comments on commit 9124ba2

Please sign in to comment.