Skip to content

Commit 008c54a

Browse files
committed
make multitarget possible
1 parent 713b42e commit 008c54a

40 files changed

Lines changed: 230 additions & 107 deletions

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.ComponentModel.DataAnnotations;
76
using System.IO;
87
using System.Linq;
98
using System.Reflection;
@@ -13,6 +12,7 @@
1312
using System.Threading.Tasks;
1413
using McMaster.Extensions.CommandLineUtils.Abstractions;
1514
using McMaster.Extensions.CommandLineUtils.Conventions;
15+
using McMaster.Extensions.CommandLineUtils.Extensions;
1616
using McMaster.Extensions.CommandLineUtils.HelpText;
1717
using McMaster.Extensions.CommandLineUtils.Internal;
1818

@@ -221,7 +221,7 @@ public IEnumerable<string> Names
221221
{
222222
get
223223
{
224-
if (!string.IsNullOrEmpty(Name))
224+
if (!Name.IsNullOrEmpty())
225225
{
226226
yield return Name;
227227
}
@@ -499,7 +499,7 @@ internal CommandLineApplication AddSubcommand(string name, Type modelType, Sourc
499499

500500
private void AssertCommandNameIsUnique(string? name, CommandLineApplication? commandToIgnore)
501501
{
502-
if (string.IsNullOrEmpty(name))
502+
if (name.IsNullOrEmpty())
503503
{
504504
return;
505505
}

src/CommandLineUtils/Conventions/OptionAttributeConvention.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using McMaster.Extensions.CommandLineUtils.Extensions;
56
using McMaster.Extensions.CommandLineUtils.SourceGeneration;
67

78
namespace McMaster.Extensions.CommandLineUtils.Conventions
@@ -32,33 +33,33 @@ public virtual void Apply(ConventionContext context)
3233
var (template, shortName, longName) = GetOptionNames(optMeta);
3334

3435
// Check for same-class conflicts (options in the same provider with conflicting names)
35-
if (!string.IsNullOrEmpty(shortName) && addedShortOptions.TryGetValue(shortName, out var existingShort))
36+
if (!shortName.IsNullOrEmpty() && addedShortOptions.TryGetValue(shortName, out var existingShort))
3637
{
3738
throw new InvalidOperationException(
3839
Strings.OptionNameIsAmbiguous(shortName, optMeta.PropertyName, optMeta.DeclaringType, existingShort.PropertyName, existingShort.DeclaringType));
3940
}
40-
if (!string.IsNullOrEmpty(longName) && addedLongOptions.TryGetValue(longName, out var existingLong))
41+
if (!longName.IsNullOrEmpty() && addedLongOptions.TryGetValue(longName, out var existingLong))
4142
{
4243
throw new InvalidOperationException(
4344
Strings.OptionNameIsAmbiguous(longName, optMeta.PropertyName, optMeta.DeclaringType, existingLong.PropertyName, existingLong.DeclaringType));
4445
}
4546

4647
// Check if option already exists from parent command (inherited options)
47-
if (!string.IsNullOrEmpty(shortName) && context.Application._shortOptions.ContainsKey(shortName))
48+
if (!shortName.IsNullOrEmpty() && context.Application._shortOptions.ContainsKey(shortName))
4849
{
4950
continue; // Skip - option already registered by parent
5051
}
51-
if (!string.IsNullOrEmpty(longName) && context.Application._longOptions.ContainsKey(longName))
52+
if (!longName.IsNullOrEmpty() && context.Application._longOptions.ContainsKey(longName))
5253
{
5354
continue; // Skip - option already registered by parent
5455
}
5556

5657
// Track this option
57-
if (!string.IsNullOrEmpty(shortName))
58+
if (!shortName.IsNullOrEmpty())
5859
{
5960
addedShortOptions[shortName] = optMeta;
6061
}
61-
if (!string.IsNullOrEmpty(longName))
62+
if (!longName.IsNullOrEmpty())
6263
{
6364
addedLongOptions[longName] = optMeta;
6465
}
@@ -74,18 +75,18 @@ private static (string template, string? shortName, string? longName) GetOptionN
7475
string? shortName = meta.ShortName;
7576
string? longName = meta.LongName;
7677

77-
if (string.IsNullOrEmpty(template))
78+
if (template.IsNullOrEmpty())
7879
{
7980
// Build template from ShortName/LongName
80-
if (!string.IsNullOrEmpty(shortName) && !string.IsNullOrEmpty(longName))
81+
if (!shortName.IsNullOrEmpty() && !longName.IsNullOrEmpty())
8182
{
8283
template = $"-{shortName}|--{longName}";
8384
}
84-
else if (!string.IsNullOrEmpty(longName))
85+
else if (!longName.IsNullOrEmpty())
8586
{
8687
template = $"--{longName}";
8788
}
88-
else if (!string.IsNullOrEmpty(shortName))
89+
else if (!shortName.IsNullOrEmpty())
8990
{
9091
template = $"-{shortName}";
9192
}
@@ -99,17 +100,17 @@ private static (string template, string? shortName, string? longName) GetOptionN
99100
else
100101
{
101102
// Parse short/long names from template if not already set
102-
if (string.IsNullOrEmpty(shortName) || string.IsNullOrEmpty(longName))
103+
if (shortName.IsNullOrEmpty() || longName.IsNullOrEmpty())
103104
{
104105
var parts = template.Split('|');
105106
foreach (var part in parts)
106107
{
107108
var trimmed = part.Trim();
108-
if (trimmed.StartsWith("--") && string.IsNullOrEmpty(longName))
109+
if (trimmed.StartsWith("--") && longName.IsNullOrEmpty())
109110
{
110111
longName = trimmed.Substring(2).Split(' ', '<', ':', '=')[0];
111112
}
112-
else if (trimmed.StartsWith("-") && string.IsNullOrEmpty(shortName))
113+
else if (trimmed.StartsWith("-") && shortName.IsNullOrEmpty())
113114
{
114115
shortName = trimmed.Substring(1).Split(' ', '<', ':', '=')[0];
115116
}
@@ -175,12 +176,12 @@ private void AddOptionFromMetadata(ConventionContext context, CommandOption opti
175176
}
176177

177178
// Register names for duplicate checking
178-
if (!string.IsNullOrEmpty(option.ShortName))
179+
if (!option.ShortName.IsNullOrEmpty())
179180
{
180181
context.Application._shortOptions.TryAdd(option.ShortName, null!);
181182
}
182183

183-
if (!string.IsNullOrEmpty(option.LongName))
184+
if (!option.LongName.IsNullOrEmpty())
184185
{
185186
context.Application._longOptions.TryAdd(option.LongName, null!);
186187
}

src/CommandLineUtils/Conventions/OptionAttributeConventionBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.ComponentModel.DataAnnotations;
77
using System.Linq;
88
using System.Reflection;
9+
using McMaster.Extensions.CommandLineUtils.Extensions;
910
using McMaster.Extensions.CommandLineUtils.Validation;
1011

1112
namespace McMaster.Extensions.CommandLineUtils.Conventions
@@ -38,7 +39,7 @@ private protected void AddOption(ConventionContext context, CommandOption option
3839
throw new InvalidOperationException(Strings.NoValueTypesMustBeBoolean);
3940
}
4041

41-
if (!string.IsNullOrEmpty(option.ShortName))
42+
if (!option.ShortName.IsNullOrEmpty())
4243
{
4344
if (context.Application._shortOptions.TryGetValue(option.ShortName, out var otherProp))
4445
{
@@ -53,7 +54,7 @@ private protected void AddOption(ConventionContext context, CommandOption option
5354
context.Application._shortOptions.Add(option.ShortName, prop);
5455
}
5556

56-
if (!string.IsNullOrEmpty(option.LongName))
57+
if (!option.LongName.IsNullOrEmpty())
5758
{
5859
if (context.Application._longOptions.TryGetValue(option.LongName, out var otherProp))
5960
{

src/CommandLineUtils/Conventions/SubcommandAttributeConvention.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Reflection;
77
using McMaster.Extensions.CommandLineUtils.Abstractions;
88
using McMaster.Extensions.CommandLineUtils.Errors;
9+
using McMaster.Extensions.CommandLineUtils.Extensions;
910
using McMaster.Extensions.CommandLineUtils.SourceGeneration;
1011

1112
namespace McMaster.Extensions.CommandLineUtils.Conventions
@@ -46,7 +47,7 @@ public virtual void Apply(ConventionContext context)
4647
private static string GetSubcommandName(Type subcommandType, ICommandMetadataProvider provider)
4748
{
4849
var commandInfo = provider.CommandInfo;
49-
if (!string.IsNullOrEmpty(commandInfo?.Name))
50+
if (!(commandInfo?.Name).IsNullOrEmpty())
5051
{
5152
// Use the explicit name as-is
5253
return commandInfo.Name;
@@ -58,7 +59,10 @@ private static string GetSubcommandName(Type subcommandType, ICommandMetadataPro
5859

5960
private void AddSubcommandFromMetadata(
6061
ConventionContext context,
61-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type subcommandType,
62+
#if NET6_0_OR_GREATER
63+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
64+
#endif
65+
Type subcommandType,
6266
ICommandMetadataProvider provider,
6367
string name)
6468
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Nate McMaster.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
6+
namespace McMaster.Extensions.CommandLineUtils.Extensions
7+
{
8+
internal static class DictionaryExtensions
9+
{
10+
#if !NET6_0_OR_GREATER
11+
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
12+
{
13+
if (dictionary.ContainsKey(key))
14+
{
15+
return false;
16+
}
17+
dictionary.Add(key, value);
18+
return true;
19+
}
20+
#endif
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Nate McMaster.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Diagnostics.CodeAnalysis;
5+
6+
namespace McMaster.Extensions.CommandLineUtils.Extensions
7+
{
8+
internal static class StringExtensions
9+
{
10+
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? value) => string.IsNullOrEmpty(value);
11+
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value) => string.IsNullOrWhiteSpace(value);
12+
}
13+
}

src/CommandLineUtils/HelpText/HangingIndentWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Linq;
66
using System.Text;
7+
using McMaster.Extensions.CommandLineUtils.Extensions;
78

89
namespace McMaster.Extensions.CommandLineUtils.HelpText
910
{
@@ -46,7 +47,7 @@ public HangingIndentWriter(int indentSize, int? maxLineLength = null, bool inden
4647
/// <returns>Dynamically wrapped description with explicit newlines preserved.</returns>
4748
public string Write(string? input)
4849
{
49-
if (string.IsNullOrWhiteSpace(input))
50+
if (input.IsNullOrWhiteSpace())
5051
{
5152
return string.Empty;
5253
}

src/CommandLineUtils/Internal/CommandLineProcessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.IO;
99
using System.Linq;
1010
using McMaster.Extensions.CommandLineUtils.Abstractions;
11+
using McMaster.Extensions.CommandLineUtils.Extensions;
1112

1213
namespace McMaster.Extensions.CommandLineUtils
1314
{
@@ -325,7 +326,7 @@ private bool ProcessUnexpectedArg(string argTypeName, string? argValue = null)
325326

326327
var suggestions = Enumerable.Empty<string>();
327328

328-
if (_currentCommand.MakeSuggestionsInErrorMessage && !string.IsNullOrEmpty(value))
329+
if (_currentCommand.MakeSuggestionsInErrorMessage && !value.IsNullOrEmpty())
329330
{
330331
suggestions = SuggestionCreator.GetTopSuggestions(_currentCommand, value);
331332
}

src/CommandLineUtils/Internal/ReflectionHelper.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,32 @@ public bool Equals(MethodInfo? x, MethodInfo? y)
188188
return true;
189189
}
190190

191-
return x != null && y != null && x.HasSameMetadataDefinitionAs(y);
191+
if (x == null || y == null)
192+
{
193+
return false;
194+
}
195+
196+
#if NET_6_0_OR_GREATER
197+
return x.HasSameMetadataDefinitionAs(y);
198+
#else
199+
return x.MetadataToken == y.MetadataToken && x.Module.Equals(y.Module);
200+
#endif
192201
}
193202

194203
public int GetHashCode(MethodInfo obj)
195204
{
205+
#if NET_6_0_OR_GREATER
196206
return obj.HasMetadataToken() ? obj.GetMetadataToken().GetHashCode() : 0;
207+
#else
208+
try
209+
{
210+
return obj.MetadataToken.GetHashCode();
211+
}
212+
catch
213+
{
214+
return 0;
215+
}
216+
#endif
197217
}
198218
}
199219

src/CommandLineUtils/Internal/SuggestionCreator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Linq;
6+
using McMaster.Extensions.CommandLineUtils.Extensions;
67

78
namespace McMaster.Extensions.CommandLineUtils
89
{
@@ -45,17 +46,17 @@ private static IEnumerable<string> GetCandidates(CommandLineApplication command)
4546

4647
foreach (var option in command.GetOptions().Where(o => o.ShowInHelpText))
4748
{
48-
if (!string.IsNullOrEmpty(option.LongName))
49+
if (!option.LongName.IsNullOrEmpty())
4950
{
5051
yield return option.LongName;
5152
}
5253

53-
if (!string.IsNullOrEmpty(option.ShortName))
54+
if (!option.ShortName.IsNullOrEmpty())
5455
{
5556
yield return option.ShortName;
5657
}
5758

58-
if (!string.IsNullOrEmpty(option.SymbolName))
59+
if (!option.SymbolName.IsNullOrEmpty())
5960
{
6061
yield return option.SymbolName;
6162
}

0 commit comments

Comments
 (0)