-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathHelpTextExtensions.cs
More file actions
45 lines (41 loc) · 1.63 KB
/
HelpTextExtensions.cs
File metadata and controls
45 lines (41 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace CommandLine
{
public static class HelpTextExtensions
{
/// <summary>
/// return true when errors contain HelpXXXError
/// </summary>
public static bool IsHelp(this IEnumerable<Error> errs)
{
if (errs.Any(x => x.Tag == ErrorType.HelpRequestedError ||
x.Tag == ErrorType.HelpVerbRequestedError))
return true;
//when AutoHelp=false in parser, help is disabled and Parser raise UnknownOptionError
return errs.Any(x => (x is UnknownOptionError ee ? ee.Token : "") == "help");
}
/// <summary>
/// return true when errors contain VersionXXXError
/// </summary>
public static bool IsVersion(this IEnumerable<Error> errs)
{
if (errs.Any(x => x.Tag == ErrorType.VersionRequestedError))
return true;
//when AutoVersion=false in parser, Version is disabled and Parser raise UnknownOptionError
return errs.Any(x => (x is UnknownOptionError ee ? ee.Token : "") == "version");
}
/// <summary>
/// redirect errs to Console.Error, and to Console.Out for help/version error
/// </summary>
public static TextWriter Output(this IEnumerable<Error> errs)
{
if (errs.IsHelp() || errs.IsVersion())
return Console.Out;
return Console.Error;
}
}
}