Skip to content

Commit f442e9a

Browse files
committed
Add help text on error
1 parent 465cbd0 commit f442e9a

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

src/CommandLine/Core/SpecificationPropertyRules.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
22

3+
using CSharpx;
4+
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
6-
using CSharpx;
78

89
namespace CommandLine.Core
910
{
@@ -46,7 +47,7 @@ group o by o.Option.Group.GetValueOrDefault(null) into g
4647

4748
if (errorGroups.Any())
4849
{
49-
return errorGroups.Select(gr => new MissingGroupOptionError(gr.Key));
50+
return errorGroups.Select(gr => new MissingGroupOptionError(gr.Key, gr.Select(g => new NameInfo(g.Option.ShortName, g.Option.LongName))));
5051
}
5152

5253
return Enumerable.Empty<Error>();

src/CommandLine/Error.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
22

33
using System;
4+
using System.Collections.Generic;
45

56
namespace CommandLine
67
{
@@ -69,7 +70,11 @@ public enum ErrorType
6970
/// <summary>
7071
/// Value of <see cref="CommandLine.InvalidAttributeConfigurationError"/> type.
7172
/// </summary>
72-
InvalidAttributeConfigurationError
73+
InvalidAttributeConfigurationError,
74+
/// <summary>
75+
/// Value of <see cref="CommandLine.MissingGroupOptionError"/> type.
76+
/// </summary>
77+
MissingGroupOptionError
7378

7479
}
7580

@@ -532,16 +537,23 @@ public sealed class MissingGroupOptionError : Error
532537
public const string ErrorMessage = "At least one option in a group must have value.";
533538

534539
private readonly string group;
540+
private readonly IEnumerable<NameInfo> names;
535541

536-
internal MissingGroupOptionError(string group)
537-
: base(ErrorType.HelpRequestedError, true)
542+
internal MissingGroupOptionError(string group, IEnumerable<NameInfo> names)
543+
: base(ErrorType.MissingGroupOptionError)
538544
{
539545
this.group = group;
546+
this.names = names;
540547
}
541548

542549
public string Group
543550
{
544551
get { return group; }
545552
}
553+
554+
public IEnumerable<NameInfo> Names
555+
{
556+
get { return names; }
557+
}
546558
}
547559
}

src/CommandLine/Text/SentenceBuilder.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
22

3+
using CommandLine.Infrastructure;
4+
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
68
using System.Text;
7-
using CommandLine.Infrastructure;
89

910
namespace CommandLine.Text
1011
{
@@ -41,7 +42,7 @@ public static SentenceBuilder Create()
4142
/// <summary>
4243
/// Gets a delegate that returns usage text block heading text.
4344
/// </summary>
44-
public abstract Func<string> UsageHeadingText { get; }
45+
public abstract Func<string> UsageHeadingText { get; }
4546

4647
/// <summary>
4748
/// Get a delegate that returns the help text of help command.
@@ -53,7 +54,7 @@ public static SentenceBuilder Create()
5354
/// Get a delegate that returns the help text of vesion command.
5455
/// The delegates must accept a boolean that is equal <value>true</value> for options; otherwise <value>false</value> for verbs.
5556
/// </summary>
56-
public abstract Func<bool, string> VersionCommandText { get; }
57+
public abstract Func<bool, string> VersionCommandText { get; }
5758

5859
/// <summary>
5960
/// Gets a delegate that handles singular error formatting.
@@ -67,7 +68,7 @@ public static SentenceBuilder Create()
6768
/// </summary>
6869
public abstract Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors { get; }
6970

70-
private class DefaultSentenceBuilder : SentenceBuilder
71+
private class DefaultSentenceBuilder : SentenceBuilder
7172
{
7273
public override Func<string> RequiredWord
7374
{
@@ -140,6 +141,13 @@ public override Func<Error, string> FormatError
140141
case ErrorType.SetValueExceptionError:
141142
var setValueError = (SetValueExceptionError)error;
142143
return "Error setting value to option '".JoinTo(setValueError.NameInfo.NameText, "': ", setValueError.Exception.Message);
144+
case ErrorType.MissingGroupOptionError:
145+
var missingGroupOptionError = (MissingGroupOptionError)error;
146+
return "At least one option from group '".JoinTo(
147+
missingGroupOptionError.Group,
148+
"' (",
149+
string.Join(", ", missingGroupOptionError.Names.Select(n => n.NameText)),
150+
") is required.");
143151
}
144152
throw new InvalidOperationException();
145153
};
@@ -153,8 +161,8 @@ public override Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutua
153161
return errors =>
154162
{
155163
var bySet = from e in errors
156-
group e by e.SetName into g
157-
select new { SetName = g.Key, Errors = g.ToList() };
164+
group e by e.SetName into g
165+
select new { SetName = g.Key, Errors = g.ToList() };
158166

159167
var msgs = bySet.Select(
160168
set =>
@@ -169,7 +177,7 @@ group e by e.SetName into g
169177
(from x in
170178
(from s in bySet where !s.SetName.Equals(set.SetName) from e in s.Errors select e)
171179
.Distinct()
172-
select "'".JoinTo(x.NameInfo.NameText, "', ")).ToArray());
180+
select "'".JoinTo(x.NameInfo.NameText, "', ")).ToArray());
173181

174182
return
175183
new StringBuilder("Option")

0 commit comments

Comments
 (0)