Skip to content

Commit 4703c74

Browse files
committed
Add errs.Output extension method to auto direct help/version to Console.Out and errors to Console.Error
1 parent d69be74 commit 4703c74

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/CommandLine/HelpTextExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2-
2+
using System;
3+
using System.IO;
34
using System.Collections.Generic;
45
using System.Linq;
56

@@ -28,6 +29,15 @@ public static bool IsVersion(this IEnumerable<Error> errs)
2829
return true;
2930
//when AutoVersion=false in parser, Version is disabled and Parser raise UnknownOptionError
3031
return errs.Any(x => (x is UnknownOptionError ee ? ee.Token : "") == "version");
32+
}
33+
/// <summary>
34+
/// redirect errs to Console.Error, and to Console.Out for help/version error
35+
/// </summary>
36+
public static TextWriter Output(this IEnumerable<Error> errs)
37+
{
38+
if (errs.IsHelp() || errs.IsVersion())
39+
return Console.Out;
40+
return Console.Error;
3141
}
3242
}
3343
}

0 commit comments

Comments
 (0)