Skip to content

Commit 7d0389b

Browse files
authored
Merge pull request microsoft#1941 from tyrielv/tyrielv/net10-system-commandline
Migrate from CommandLineParser to System.CommandLine
2 parents 7669d6c + f0c8cb8 commit 7d0389b

30 files changed

Lines changed: 1017 additions & 552 deletions

Directory.Packages.props

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<!-- CLI -->
9-
<PackageVersion Include="CommandLineParser" Version="2.6.0" />
10-
118
<!-- Serialization -->
129
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
1310

@@ -43,7 +40,7 @@
4340
Future packages: pre-declared for Phase 2+ branches to avoid
4441
merge conflicts on this file. Not yet referenced by any project.
4542
-->
46-
<PackageVersion Include="System.CommandLine" Version="2.0.3" />
43+
<PackageVersion Include="System.CommandLine" Version="2.0.5" />
4744
<PackageVersion Include="System.IO.Pipes.AccessControl" Version="8.0.0" />
4845
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
4946
</ItemGroup>

GVFS/FastFetch/FastFetch.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="CommandLineParser" />
15+
<PackageReference Include="System.CommandLine" />
1616
<PackageReference Include="MicroBuild.Core" ExcludeAssets="none" />
1717
</ItemGroup>
1818

GVFS/FastFetch/FastFetchVerb.cs

Lines changed: 111 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using CommandLine;
21
using GVFS.Common;
32
using GVFS.Common.Git;
43
using GVFS.Common.Http;
54
using GVFS.Common.Prefetch;
65
using GVFS.Common.Tracing;
76
using System;
7+
using System.CommandLine;
88

99
namespace FastFetch
1010
{
11-
[Verb("fastfetch", HelpText = "Fast-fetch a branch")]
1211
public class FastFetchVerb
1312
{
1413
// Testing has shown that more than 16 download threads does not improve
@@ -19,131 +18,149 @@ public class FastFetchVerb
1918
private const int ExitFailure = 1;
2019
private const int ExitSuccess = 0;
2120

22-
[Option(
23-
'c',
24-
"commit",
25-
Required = false,
26-
HelpText = "Commit to fetch")]
2721
public string Commit { get; set; }
2822

29-
[Option(
30-
'b',
31-
"branch",
32-
Required = false,
33-
HelpText = "Branch to fetch")]
3423
public string Branch { get; set; }
3524

36-
[Option(
37-
"cache-server-url",
38-
Required = false,
39-
Default = "",
40-
HelpText = "Defines the url of the cache server")]
4125
public string CacheServerUrl { get; set; }
4226

43-
[Option(
44-
"chunk-size",
45-
Required = false,
46-
Default = 4000,
47-
HelpText = "Sets the number of objects to be downloaded in a single pack")]
4827
public int ChunkSize { get; set; }
4928

50-
[Option(
51-
"checkout",
52-
Required = false,
53-
Default = false,
54-
HelpText = "Checkout the target commit into the working directory after fetching")]
5529
public bool Checkout { get; set; }
5630

57-
[Option(
58-
"force-checkout",
59-
Required = false,
60-
Default = false,
61-
HelpText = "Force FastFetch to checkout content as if the current repo had just been initialized." +
62-
"This allows you to include more folders from the repo that were not originally checked out." +
63-
"Can only be used with the --checkout option.")]
6431
public bool ForceCheckout { get; set; }
6532

66-
[Option(
67-
"search-thread-count",
68-
Required = false,
69-
Default = 0,
70-
HelpText = "Sets the number of threads to use for finding missing blobs. (0 for number of logical cores)")]
7133
public int SearchThreadCount { get; set; }
7234

73-
[Option(
74-
"download-thread-count",
75-
Required = false,
76-
Default = 0,
77-
HelpText = "Sets the number of threads to use for downloading. (0 for number of logical cores)")]
7835
public int DownloadThreadCount { get; set; }
7936

80-
[Option(
81-
"index-thread-count",
82-
Required = false,
83-
Default = 0,
84-
HelpText = "Sets the number of threads to use for indexing. (0 for number of logical cores)")]
8537
public int IndexThreadCount { get; set; }
8638

87-
[Option(
88-
"checkout-thread-count",
89-
Required = false,
90-
Default = 0,
91-
HelpText = "Sets the number of threads to use for checkout. (0 for number of logical cores)")]
9239
public int CheckoutThreadCount { get; set; }
9340

94-
[Option(
95-
'r',
96-
"max-retries",
97-
Required = false,
98-
Default = 10,
99-
HelpText = "Sets the maximum number of attempts for downloading a pack")]
100-
10141
public int MaxAttempts { get; set; }
10242

103-
[Option(
104-
"git-path",
105-
Default = "",
106-
Required = false,
107-
HelpText = "Sets the path and filename for git.exe if it isn't expected to be on %PATH%.")]
10843
public string GitBinPath { get; set; }
109-
110-
[Option(
111-
"folders",
112-
Required = false,
113-
Default = "",
114-
HelpText = "A semicolon-delimited list of folders to fetch")]
44+
11545
public string FolderList { get; set; }
11646

117-
[Option(
118-
"folders-list",
119-
Required = false,
120-
Default = "",
121-
HelpText = "A file containing line-delimited list of folders to fetch")]
12247
public string FolderListFile { get; set; }
12348

124-
[Option(
125-
"Allow-index-metadata-update-from-working-tree",
126-
Required = false,
127-
Default = false,
128-
HelpText = "When specified, index metadata (file times and sizes) is updated from disk if not already in the index. " +
129-
"This flag should only be used when the working tree is known to be in a good state. " +
130-
"Do not use this flag if the working tree is not 100% known to be good as it would cause 'git status' to misreport.")]
13149
public bool AllowIndexMetadataUpdateFromWorkingTree { get; set; }
13250

133-
[Option(
134-
"verbose",
135-
Required = false,
136-
Default = false,
137-
HelpText = "Show all outputs on the console in addition to writing them to a log file")]
13851
public bool Verbose { get; set; }
13952

140-
[Option(
141-
"parent-activity-id",
142-
Required = false,
143-
Default = "",
144-
HelpText = "The GUID of the caller - used for telemetry purposes.")]
14553
public string ParentActivityId { get; set; }
14654

55+
public static RootCommand BuildRootCommand()
56+
{
57+
RootCommand rootCommand = new RootCommand("Fast-fetch a branch");
58+
59+
Option<string> commitOption = new Option<string>("--commit", new[] { "-c" }) { Description = "Commit to fetch" };
60+
rootCommand.Add(commitOption);
61+
62+
Option<string> branchOption = new Option<string>("--branch", new[] { "-b" }) { Description = "Branch to fetch" };
63+
rootCommand.Add(branchOption);
64+
65+
Option<string> cacheServerUrlOption = new Option<string>("--cache-server-url")
66+
{
67+
Description = "Defines the url of the cache server",
68+
DefaultValueFactory = (_) => ""
69+
};
70+
rootCommand.Add(cacheServerUrlOption);
71+
72+
Option<int> chunkSizeOption = new Option<int>("--chunk-size")
73+
{
74+
Description = "Sets the number of objects to be downloaded in a single pack",
75+
DefaultValueFactory = (_) => 4000
76+
};
77+
rootCommand.Add(chunkSizeOption);
78+
79+
Option<bool> checkoutOption = new Option<bool>("--checkout") { Description = "Checkout the target commit into the working directory after fetching" };
80+
rootCommand.Add(checkoutOption);
81+
82+
Option<bool> forceCheckoutOption = new Option<bool>("--force-checkout") { Description = "Force FastFetch to checkout content as if the current repo had just been initialized." };
83+
rootCommand.Add(forceCheckoutOption);
84+
85+
Option<int> searchThreadCountOption = new Option<int>("--search-thread-count") { Description = "Sets the number of threads to use for finding missing blobs. (0 for number of logical cores)", DefaultValueFactory = (_) => 0 };
86+
rootCommand.Add(searchThreadCountOption);
87+
88+
Option<int> downloadThreadCountOption = new Option<int>("--download-thread-count") { Description = "Sets the number of threads to use for downloading. (0 for number of logical cores)", DefaultValueFactory = (_) => 0 };
89+
rootCommand.Add(downloadThreadCountOption);
90+
91+
Option<int> indexThreadCountOption = new Option<int>("--index-thread-count") { Description = "Sets the number of threads to use for indexing. (0 for number of logical cores)", DefaultValueFactory = (_) => 0 };
92+
rootCommand.Add(indexThreadCountOption);
93+
94+
Option<int> checkoutThreadCountOption = new Option<int>("--checkout-thread-count") { Description = "Sets the number of threads to use for checkout. (0 for number of logical cores)", DefaultValueFactory = (_) => 0 };
95+
rootCommand.Add(checkoutThreadCountOption);
96+
97+
Option<int> maxRetriesOption = new Option<int>("--max-retries", new[] { "-r" })
98+
{
99+
Description = "Sets the maximum number of attempts for downloading a pack",
100+
DefaultValueFactory = (_) => 10
101+
};
102+
rootCommand.Add(maxRetriesOption);
103+
104+
Option<string> gitPathOption = new Option<string>("--git-path")
105+
{
106+
Description = "Sets the path and filename for git.exe if it isn't expected to be on %PATH%.",
107+
DefaultValueFactory = (_) => ""
108+
};
109+
rootCommand.Add(gitPathOption);
110+
111+
Option<string> foldersOption = new Option<string>("--folders")
112+
{
113+
Description = "A semicolon-delimited list of folders to fetch",
114+
DefaultValueFactory = (_) => ""
115+
};
116+
rootCommand.Add(foldersOption);
117+
118+
Option<string> foldersListOption = new Option<string>("--folders-list")
119+
{
120+
Description = "A file containing line-delimited list of folders to fetch",
121+
DefaultValueFactory = (_) => ""
122+
};
123+
rootCommand.Add(foldersListOption);
124+
125+
Option<bool> allowIndexMetadataOption = new Option<bool>("--Allow-index-metadata-update-from-working-tree") { Description = "When specified, index metadata is updated from disk if not already in the index." };
126+
rootCommand.Add(allowIndexMetadataOption);
127+
128+
Option<bool> verboseOption = new Option<bool>("--verbose") { Description = "Show all outputs on the console in addition to writing them to a log file" };
129+
rootCommand.Add(verboseOption);
130+
131+
Option<string> parentActivityIdOption = new Option<string>("--parent-activity-id")
132+
{
133+
Description = "The GUID of the caller - used for telemetry purposes.",
134+
DefaultValueFactory = (_) => ""
135+
};
136+
rootCommand.Add(parentActivityIdOption);
137+
138+
rootCommand.SetAction((ParseResult result) =>
139+
{
140+
FastFetchVerb verb = new FastFetchVerb();
141+
verb.Commit = result.GetValue(commitOption);
142+
verb.Branch = result.GetValue(branchOption);
143+
verb.CacheServerUrl = result.GetValue(cacheServerUrlOption) ?? "";
144+
verb.ChunkSize = result.GetValue(chunkSizeOption);
145+
verb.Checkout = result.GetValue(checkoutOption);
146+
verb.ForceCheckout = result.GetValue(forceCheckoutOption);
147+
verb.SearchThreadCount = result.GetValue(searchThreadCountOption);
148+
verb.DownloadThreadCount = result.GetValue(downloadThreadCountOption);
149+
verb.IndexThreadCount = result.GetValue(indexThreadCountOption);
150+
verb.CheckoutThreadCount = result.GetValue(checkoutThreadCountOption);
151+
verb.MaxAttempts = result.GetValue(maxRetriesOption);
152+
verb.GitBinPath = result.GetValue(gitPathOption) ?? "";
153+
verb.FolderList = result.GetValue(foldersOption) ?? "";
154+
verb.FolderListFile = result.GetValue(foldersListOption) ?? "";
155+
verb.AllowIndexMetadataUpdateFromWorkingTree = result.GetValue(allowIndexMetadataOption);
156+
verb.Verbose = result.GetValue(verboseOption);
157+
verb.ParentActivityId = result.GetValue(parentActivityIdOption) ?? "";
158+
verb.Execute();
159+
});
160+
161+
return rootCommand;
162+
}
163+
147164
public void Execute()
148165
{
149166
Environment.ExitCode = this.ExecuteWithExitCode();

GVFS/FastFetch/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CommandLine;
1+
using System.CommandLine;
22
using GVFS.PlatformLoader;
33

44
namespace FastFetch
@@ -8,8 +8,8 @@ public class Program
88
public static void Main(string[] args)
99
{
1010
GVFSPlatformLoader.Initialize();
11-
Parser.Default.ParseArguments<FastFetchVerb>(args)
12-
.WithParsed(fastFetch => fastFetch.Execute());
11+
RootCommand rootCommand = FastFetchVerb.BuildRootCommand();
12+
rootCommand.Parse(args).Invoke();
1313
}
1414
}
1515
}

GVFS/GVFS.FunctionalTests.LockHolder/AcquireGVFSLock.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using CommandLine;
21
using GVFS.Common;
32
using GVFS.Common.NamedPipes;
43
using GVFS.Platform.Windows;
54
using System;
5+
using System.CommandLine;
66
using System.Diagnostics;
77
using System.Runtime.InteropServices;
88

@@ -12,13 +12,25 @@ public class AcquireGVFSLockVerb
1212
{
1313
private static string fullCommand = "GVFS.FunctionalTests.LockHolder";
1414

15-
[Option(
16-
"skip-release-lock",
17-
Default = false,
18-
Required = false,
19-
HelpText = "Skip releasing the GVFS lock when exiting the program.")]
2015
public bool NoReleaseLock { get; set; }
2116

17+
public static RootCommand BuildRootCommand()
18+
{
19+
RootCommand rootCommand = new RootCommand();
20+
21+
Option<bool> skipReleaseLockOption = new Option<bool>("--skip-release-lock") { Description = "Skip releasing the GVFS lock when exiting the program." };
22+
rootCommand.Add(skipReleaseLockOption);
23+
24+
rootCommand.SetAction((ParseResult result) =>
25+
{
26+
AcquireGVFSLockVerb verb = new AcquireGVFSLockVerb();
27+
verb.NoReleaseLock = result.GetValue(skipReleaseLockOption);
28+
verb.Execute();
29+
});
30+
31+
return rootCommand;
32+
}
33+
2234
public void Execute()
2335
{
2436
string errorMessage;

GVFS/GVFS.FunctionalTests.LockHolder/GVFS.FunctionalTests.LockHolder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="CommandLineParser" />
9+
<PackageReference Include="System.CommandLine" />
1010
</ItemGroup>
1111

1212
<ItemGroup>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using CommandLine;
1+
using System.CommandLine;
22

33
namespace GVFS.FunctionalTests.LockHolder
44
{
55
public class Program
66
{
77
public static void Main(string[] args)
88
{
9-
Parser.Default.ParseArguments<AcquireGVFSLockVerb>(args)
10-
.WithParsed(acquireGVFSLock => acquireGVFSLock.Execute());
9+
RootCommand rootCommand = AcquireGVFSLockVerb.BuildRootCommand();
10+
rootCommand.Parse(args).Invoke();
1111
}
1212
}
1313
}

GVFS/GVFS.Mount/GVFS.Mount.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="CommandLineParser" />
19+
<PackageReference Include="System.CommandLine" />
2020
</ItemGroup>
2121

2222
</Project>

0 commit comments

Comments
 (0)