Skip to content

Commit ab06c31

Browse files
authored
Merge pull request #29 from dotnet-campus/t/lvyi/gnu
支持为命令行添加过滤器(这样能全局处理某些选项)
2 parents 67f160e + 7cf27c8 commit ab06c31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1161
-409
lines changed

build/Version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>3.2.1</Version>
3+
<Version>3.3.0</Version>
44
</PropertyGroup>
55
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.IO;
3+
4+
using dotnetCampus.Cli.Properties;
5+
6+
namespace dotnetCampus.Cli
7+
{
8+
internal class DefaultOptions
9+
{
10+
[Option(LocalizableDescription = nameof(LocalizableStrings.SamplePropertyDescription))]
11+
public string? DefaultText { get; set; }
12+
13+
[Option(LocalizableDescription = nameof(LocalizableStrings.SampleDirectoryPropertyDescription))]
14+
public DirectoryInfo? DefaultDirectory { get; set; }
15+
16+
internal void Run()
17+
{
18+
Console.WriteLine("默认行为执行……");
19+
}
20+
}
21+
}

samples/dotnetCampus.CommandLine.Sample/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static void Main(string[] args)
99
{
1010
CommandLine.Parse(args, LocalizableStrings.ResourceManager)
1111
.AddStandardHandlers()
12+
.AddHandler<DefaultOptions>(o => o.Run())
1213
.AddHandler<SampleOptions>(o => o.Run())
1314
.Run();
1415
}

samples/dotnetCampus.CommandLine.Sample/Properties/LocalizableStrings.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/dotnetCampus.CommandLine.Sample/Properties/LocalizableStrings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="SampleDirectoryPropertyDescription" xml:space="preserve">
121+
<value>Pass any directory into this option.</value>
122+
</data>
123+
<data name="SampleFilePropertyDescription" xml:space="preserve">
124+
<value>Pass any file into this option.</value>
125+
</data>
120126
<data name="SamplePropertyDescription" xml:space="preserve">
121127
<value>Output any text passed to this option.</value>
122128
</data>

samples/dotnetCampus.CommandLine.Sample/Properties/LocalizableStrings.zh-CN.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="SampleDirectoryPropertyDescription" xml:space="preserve">
121+
<value>允许传入一个目录路径。</value>
122+
</data>
123+
<data name="SampleFilePropertyDescription" xml:space="preserve">
124+
<value>允许传入一个文件路径。</value>
125+
</data>
120126
<data name="SamplePropertyDescription" xml:space="preserve">
121127
<value>输出传入的任何类型的字符串。</value>
122128
</data>

samples/dotnetCampus.CommandLine.Sample/Properties/launchSettings.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,33 @@
33
"Debug": {
44
"commandName": "Project"
55
},
6-
"--help": {
6+
"Debug --help": {
77
"commandName": "Project",
88
"commandLineArgs": "--help"
99
},
10-
"--version": {
10+
"Debug --version": {
1111
"commandName": "Project",
1212
"commandLineArgs": "--version"
1313
},
14-
"sample": {
14+
"Debug sample": {
1515
"commandName": "Project",
1616
"commandLineArgs": "sample --sample-property \"Hello World\""
17+
},
18+
"Debug sample --help": {
19+
"commandName": "Project",
20+
"commandLineArgs": "sample --help"
21+
},
22+
"Debug unknown": {
23+
"commandName": "Project",
24+
"commandLineArgs": "unknown --sample-property \"Hello World\""
25+
},
26+
"Debug unknown --help": {
27+
"commandName": "Project",
28+
"commandLineArgs": "unknown --help"
29+
},
30+
"Debug --options": {
31+
"commandName": "Project",
32+
"commandLineArgs": "--sample-property \"Hello World\""
1733
}
1834
}
1935
}

samples/dotnetCampus.CommandLine.Sample/SampleOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23

34
using dotnetCampus.Cli.Properties;
45

@@ -8,10 +9,14 @@ namespace dotnetCampus.Cli
89
internal class SampleOptions
910
{
1011
[Option(LocalizableDescription = nameof(LocalizableStrings.SamplePropertyDescription))]
11-
public string? SampleProperty { get; set; }
12+
public string? SampleText { get; set; }
13+
14+
[Option(LocalizableDescription = nameof(LocalizableStrings.SampleFilePropertyDescription))]
15+
public FileInfo? SampleFile { get; set; }
1216

1317
internal void Run()
1418
{
19+
Console.WriteLine("示例行为执行……");
1520
}
1621
}
1722
}

src/dotnetCampus.CommandLine/CommandLine.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Collections.ObjectModel;
54
using System.Diagnostics;
65
using System.Diagnostics.CodeAnalysis;
76
using System.Diagnostics.Contracts;
@@ -13,6 +12,7 @@
1312
using System.Threading.Tasks;
1413
using System.Web;
1514

15+
using dotnetCampus.Cli.Compatibility;
1616
using dotnetCampus.Cli.Core;
1717
using dotnetCampus.Cli.StateMachine;
1818

@@ -35,7 +35,8 @@ namespace dotnetCampus.Cli
3535
public sealed class CommandLine : ICommandLineHandlerBuilder, IEnumerable<ListGroupItem>
3636
{
3737
private readonly ListGroup<SingleOptimizedStrings> _optionArgs;
38-
private readonly List<CommandLineVerbMatch<Task<int>>> _toMatchList = new List<CommandLineVerbMatch<Task<int>>>();
38+
private readonly List<CommandLineFilterMatch> _filterCreatorList = new List<CommandLineFilterMatch>();
39+
private readonly List<CommandLineTypeMatcher<Task<int>>> _toMatchList = new List<CommandLineTypeMatcher<Task<int>>>();
3940

4041
private CommandLine(ListGroup<SingleOptimizedStrings> optionArgs, ResourceManager? resourceManager)
4142
{
@@ -48,10 +49,15 @@ private CommandLine(ListGroup<SingleOptimizedStrings> optionArgs, ResourceManage
4849
/// </summary>
4950
internal ResourceManager? ResourceManager { get; }
5051

52+
/// <summary>
53+
/// 收集的过滤器。
54+
/// </summary>
55+
internal IReadOnlyList<CommandLineFilterMatch> FilterMatchList => _filterCreatorList;
56+
5157
/// <summary>
5258
/// 收集的谓词处理方法。
5359
/// </summary>
54-
internal IReadOnlyList<CommandLineVerbMatch<Task<int>>> ToMatchList => _toMatchList;
60+
internal IReadOnlyList<CommandLineTypeMatcher<Task<int>>> VerbMatchList => _toMatchList;
5561

5662
/// <summary>
5763
/// 自动查找命令行类型 <typeparamref name="T"/> 的解析器,然后解析出参数 <typeparamref name="T"/> 的一个新实例。
@@ -170,8 +176,14 @@ public T As<T>(ICommandLineOptionParser<T> parser)
170176
return parser.Commit();
171177
}
172178

173-
internal void AddMatch<TVerb>(Func<string?, MatchHandleResult<Task<int>>> match)
174-
=> _toMatchList.Add(new CommandLineVerbMatch<Task<int>>(typeof(TVerb), match));
179+
internal void AddMatch(CommandLineFilterMatch filterCreator)
180+
=> _filterCreatorList.Add(filterCreator);
181+
182+
internal void AddMatch<TVerb>(CommandLineTypeMatcher<Task<int>> matcher)
183+
=> _toMatchList.Add(matcher);
184+
185+
//internal void AddMatch<TVerb>(Func<string?, CommandLineTypeMatchResult<Task<int>>> match)
186+
// => _toMatchList.Add(new CommandLineTypeMatcher<Task<int>>(typeof(TVerb), match));
175187

176188
/// <summary>
177189
/// 将命令行参数转换为字符串值的字典。Key 为选项,Value 为选项后面的值。

src/dotnetCampus.CommandLine/CommandLineAsyncHandlerBuilder.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33

4-
using dotnetCampus.Cli.StateMachine;
5-
6-
using static dotnetCampus.Cli.Utils.CommandLineHelpers;
4+
using static dotnetCampus.Cli.Utils.CommandLineRunner;
75

86
namespace dotnetCampus.Cli
97
{
@@ -34,16 +32,6 @@ internal CommandLineAsyncHandlerBuilder(CommandLine commandLine)
3432
/// 2. 最多只会有一个谓词处理方法被执行,此方法会返回唯一那个处理方法的退出代码。
3533
/// </remarks>
3634
/// <returns>用于异步等待谓词处理方法退出代码的异步任务。</returns>
37-
public Task<int> RunAsync()
38-
{
39-
var possibleVerb = FindPossibleVerb(CommandLine);
40-
foreach (var exitCode in new HandleVerbStateMachine<Task<int>>(CommandLine.ToMatchList).Run(possibleVerb))
41-
{
42-
return exitCode;
43-
}
44-
45-
ThrowIfVerbNotMatchedAsync(possibleVerb);
46-
return Task.FromResult(0);
47-
}
35+
public Task<int> RunAsync() => RunCoreAsync(CommandLine);
4836
}
4937
}

0 commit comments

Comments
 (0)