Skip to content

Commit 5dad6ed

Browse files
committed
Added AutoHelp and AutoVersion properties to control adding of implicit 'help' and 'version' options/verbs.
1 parent 286b724 commit 5dad6ed

File tree

8 files changed

+91
-14
lines changed

8 files changed

+91
-14
lines changed

src/CommandLine/Core/InstanceBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public static ParserResult<T> Build<T>(
2323
StringComparer nameComparer,
2424
bool ignoreValueCase,
2525
CultureInfo parsingCulture,
26+
bool autoHelp,
27+
bool autoVersion,
2628
IEnumerable<ErrorType> nonFatalErrors)
2729
{
2830
var typeInfo = factory.MapValueOrDefault(f => f().GetType(), typeof(T));
@@ -129,7 +131,7 @@ join sp in specPropsWithValue on prms.Name.ToLower() equals sp.Property.Name.ToL
129131
};
130132

131133
var preprocessorErrors = arguments.Any()
132-
? arguments.Preprocess(PreprocessorGuards.Lookup(nameComparer))
134+
? arguments.Preprocess(PreprocessorGuards.Lookup(nameComparer, autoHelp, autoVersion))
133135
: Enumerable.Empty<Error>();
134136

135137
var result = arguments.Any()

src/CommandLine/Core/InstanceChooser.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public static ParserResult<object> Choose(
1818
IEnumerable<string> arguments,
1919
StringComparer nameComparer,
2020
CultureInfo parsingCulture,
21+
bool autoHelp,
22+
bool autoVersion,
2123
IEnumerable<ErrorType> nonFatalErrors)
2224
{
2325
Func<ParserResult<object>> choose = () =>
@@ -30,13 +32,13 @@ public static ParserResult<object> Choose(
3032

3133
var verbs = Verb.SelectFromTypes(types);
3234

33-
return preprocCompare("help")
35+
return (autoHelp && preprocCompare("help"))
3436
? MakeNotParsed(types,
3537
MakeHelpVerbRequestedError(verbs,
3638
arguments.Skip(1).FirstOrDefault() ?? string.Empty, nameComparer))
37-
: preprocCompare("version")
39+
: (autoVersion && preprocCompare("version"))
3840
? MakeNotParsed(types, new VersionRequestedError())
39-
: MatchVerb(tokenizer, verbs, arguments, nameComparer, parsingCulture, nonFatalErrors);
41+
: MatchVerb(tokenizer, verbs, arguments, nameComparer, parsingCulture, autoHelp, autoVersion, nonFatalErrors);
4042
};
4143

4244
return arguments.Any()
@@ -50,6 +52,8 @@ private static ParserResult<object> MatchVerb(
5052
IEnumerable<string> arguments,
5153
StringComparer nameComparer,
5254
CultureInfo parsingCulture,
55+
bool autoHelp,
56+
bool autoVersion,
5357
IEnumerable<ErrorType> nonFatalErrors)
5458
{
5559
return verbs.Any(a => nameComparer.Equals(a.Item1.Name, arguments.First()))
@@ -62,6 +66,8 @@ private static ParserResult<object> MatchVerb(
6266
nameComparer,
6367
false,
6468
parsingCulture,
69+
autoHelp,
70+
autoVersion,
6571
nonFatalErrors)
6672
: MakeNotParsed(verbs.Select(v => v.Item2), new BadVerbSelectedError(arguments.First()));
6773
}

src/CommandLine/Core/PreprocessorGuards.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ namespace CommandLine.Core
99
static class PreprocessorGuards
1010
{
1111
public static IEnumerable<Func<IEnumerable<string>, IEnumerable<Error>>>
12-
Lookup(StringComparer nameComparer)
12+
Lookup(StringComparer nameComparer, bool autoHelp, bool autoVersion)
1313
{
14-
return new List<Func<IEnumerable<string>, IEnumerable<Error>>>
15-
{
16-
HelpCommand(nameComparer),
17-
VersionCommand(nameComparer)
18-
};
14+
var list = new List<Func<IEnumerable<string>, IEnumerable<Error>>>();
15+
if (autoHelp)
16+
list.Add(HelpCommand(nameComparer));
17+
if (autoVersion)
18+
list.Add(VersionCommand(nameComparer));
19+
return list;
1920
}
2021

2122
public static Func<IEnumerable<string>, IEnumerable<Error>> HelpCommand(StringComparer nameComparer)

src/CommandLine/Parser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public ParserResult<T> ParseArguments<T>(IEnumerable<string> args)
9999
settings.NameComparer,
100100
settings.CaseInsensitiveEnumValues,
101101
settings.ParsingCulture,
102+
settings.AutoHelp,
103+
settings.AutoVersion,
102104
HandleUnknownArguments(settings.IgnoreUnknownArguments)),
103105
settings);
104106
}
@@ -128,6 +130,8 @@ public ParserResult<T> ParseArguments<T>(Func<T> factory, IEnumerable<string> ar
128130
settings.NameComparer,
129131
settings.CaseInsensitiveEnumValues,
130132
settings.ParsingCulture,
133+
settings.AutoHelp,
134+
settings.AutoVersion,
131135
HandleUnknownArguments(settings.IgnoreUnknownArguments)),
132136
settings);
133137
}
@@ -157,6 +161,8 @@ public ParserResult<object> ParseArguments(IEnumerable<string> args, params Type
157161
args,
158162
settings.NameComparer,
159163
settings.ParsingCulture,
164+
settings.AutoHelp,
165+
settings.AutoVersion,
160166
HandleUnknownArguments(settings.IgnoreUnknownArguments)),
161167
settings);
162168
}

src/CommandLine/ParserSettings.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class ParserSettings : IDisposable
2020
private bool caseInsensitiveEnumValues;
2121
private TextWriter helpWriter;
2222
private bool ignoreUnknownArguments;
23+
private bool autoHelp;
24+
private bool autoVersion;
2325
private CultureInfo parsingCulture;
2426
private bool enableDashDash;
2527
private int maximumDisplayWidth;
@@ -31,6 +33,8 @@ public ParserSettings()
3133
{
3234
caseSensitive = true;
3335
caseInsensitiveEnumValues = false;
36+
autoHelp = true;
37+
autoVersion = true;
3438
parsingCulture = CultureInfo.InvariantCulture;
3539
try
3640
{
@@ -118,6 +122,24 @@ public bool IgnoreUnknownArguments
118122
set { PopsicleSetter.Set(Consumed, ref ignoreUnknownArguments, value); }
119123
}
120124

125+
/// <summary>
126+
/// Gets or sets a value indicating whether implicit option or verb 'help' should be supported.
127+
/// </summary>
128+
public bool AutoHelp
129+
{
130+
get { return autoHelp; }
131+
set { PopsicleSetter.Set(Consumed, ref autoHelp, value); }
132+
}
133+
134+
/// <summary>
135+
/// Gets or sets a value indicating whether implicit option or verb 'version' should be supported.
136+
/// </summary>
137+
public bool AutoVersion
138+
{
139+
get { return autoVersion; }
140+
set { PopsicleSetter.Set(Consumed, ref autoVersion, value); }
141+
}
142+
121143
/// <summary>
122144
/// Gets or sets a value indicating whether enable double dash '--' syntax,
123145
/// that forces parsing of all subsequent tokens as values.

src/CommandLine/Text/HelpText.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class HelpText
3131
private StringBuilder optionsHelp;
3232
private bool addDashesToOption;
3333
private bool addEnumValuesToHelpText;
34+
private bool autoHelp;
35+
private bool autoVersion;
3436

3537
/// <summary>
3638
/// Initializes a new instance of the <see cref="CommandLine.Text.HelpText"/> class.
@@ -113,6 +115,8 @@ public HelpText(SentenceBuilder sentenceBuilder, string heading, string copyrigh
113115
this.sentenceBuilder = sentenceBuilder;
114116
this.heading = heading;
115117
this.copyright = copyright;
118+
this.autoHelp = true;
119+
this.autoVersion = true;
116120
}
117121

118122
/// <summary>
@@ -183,6 +187,24 @@ public bool AddEnumValuesToHelpText
183187
set { addEnumValuesToHelpText = value; }
184188
}
185189

190+
/// <summary>
191+
/// Gets or sets a value indicating whether implicit option or verb 'help' should be supported.
192+
/// </summary>
193+
public bool AutoHelp
194+
{
195+
get { return autoHelp; }
196+
set { autoHelp = value; }
197+
}
198+
199+
/// <summary>
200+
/// Gets or sets a value indicating whether implicit option or verb 'version' should be supported.
201+
/// </summary>
202+
public bool AutoVersion
203+
{
204+
get { return autoVersion; }
205+
set { autoVersion = value; }
206+
}
207+
186208
/// <summary>
187209
/// Gets the <see cref="SentenceBuilder"/> instance specified in constructor.
188210
/// </summary>
@@ -676,8 +698,11 @@ private IEnumerable<Specification> GetSpecificationsFromType(Type type)
676698
{
677699
var specs = type.GetSpecifications(Specification.FromProperty);
678700
var optionSpecs = specs
679-
.OfType<OptionSpecification>()
680-
.Concat(new[] { MakeHelpEntry(), MakeVersionEntry() });
701+
.OfType<OptionSpecification>();
702+
if (autoHelp)
703+
optionSpecs = optionSpecs.Concat(new [] { MakeHelpEntry() });
704+
if (autoVersion)
705+
optionSpecs = optionSpecs.Concat(new [] { MakeVersionEntry() });
681706
var valueSpecs = specs
682707
.OfType<ValueSpecification>()
683708
.OrderBy(v => v.Index);
@@ -707,15 +732,20 @@ private static Maybe<Tuple<UsageAttribute, IEnumerable<Example>>> GetUsageFromTy
707732

708733
private IEnumerable<Specification> AdaptVerbsToSpecifications(IEnumerable<Type> types)
709734
{
710-
return (from verbTuple in Verb.SelectFromTypes(types)
735+
var optionSpecs = from verbTuple in Verb.SelectFromTypes(types)
711736
select
712737
OptionSpecification.NewSwitch(
713738
string.Empty,
714739
verbTuple.Item1.Name,
715740
false,
716741
verbTuple.Item1.HelpText,
717742
string.Empty,
718-
verbTuple.Item1.Hidden)).Concat(new[] { MakeHelpEntry(), MakeVersionEntry() });
743+
verbTuple.Item1.Hidden);
744+
if (autoHelp)
745+
optionSpecs = optionSpecs.Concat(new [] { MakeHelpEntry() });
746+
if (autoVersion)
747+
optionSpecs = optionSpecs.Concat(new [] { MakeVersionEntry() });
748+
return optionSpecs;
719749
}
720750

721751
private HelpText AddOptionsImpl(

tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ private static ParserResult<T> InvokeBuild<T>(string[] arguments)
2929
StringComparer.Ordinal,
3030
false,
3131
CultureInfo.InvariantCulture,
32+
true,
33+
true,
3234
Enumerable.Empty<ErrorType>());
3335
}
3436

@@ -42,6 +44,8 @@ private static ParserResult<T> InvokeBuildEnumValuesCaseIgnore<T>(string[] argum
4244
StringComparer.Ordinal,
4345
true,
4446
CultureInfo.InvariantCulture,
47+
true,
48+
true,
4549
Enumerable.Empty<ErrorType>());
4650
}
4751

@@ -54,6 +58,8 @@ private static ParserResult<T> InvokeBuildImmutable<T>(string[] arguments)
5458
StringComparer.Ordinal,
5559
false,
5660
CultureInfo.InvariantCulture,
61+
true,
62+
true,
5763
Enumerable.Empty<ErrorType>());
5864
}
5965

@@ -451,6 +457,8 @@ public void Double_dash_force_subsequent_arguments_as_values()
451457
StringComparer.Ordinal,
452458
false,
453459
CultureInfo.InvariantCulture,
460+
true,
461+
true,
454462
Enumerable.Empty<ErrorType>());
455463

456464
// Verify outcome

tests/CommandLine.Tests/Unit/Core/InstanceChooserTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ private static ParserResult<object> InvokeChoose(
2323
arguments,
2424
StringComparer.Ordinal,
2525
CultureInfo.InvariantCulture,
26+
true,
27+
true,
2628
Enumerable.Empty<ErrorType>());
2729
}
2830

0 commit comments

Comments
 (0)