Skip to content

Commit 2d697b3

Browse files
committed
Move some validation earlier
1 parent aca8a2d commit 2d697b3

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/nbgv/Program.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ private static Parser BuildCommandLine()
8585
{
8686
var install = new Command("install", "Prepares a project to have version stamps applied using Nerdbank.GitVersioning.")
8787
{
88-
new Option<string>(new[] { "--path", "-p" }, "The path to the directory that should contain the version.json file. The default is the root of the git repo."),
89-
new Option<string>(new[] { "--version", "-v" }, $"The initial version to set. The default is {DefaultVersionSpec}."),
88+
new Option<string>(new[] { "--path", "-p" }, "The path to the directory that should contain the version.json file. The default is the root of the git repo.").LegalFilePathsOnly(),
89+
new Option<string>(new[] { "--version", "-v" }, () => DefaultVersionSpec, $"The initial version to set."),
9090
new Option<string[]>(new[] { "--source", "-s" }, $"The URI(s) of the NuGet package source(s) used to determine the latest stable version of the {PackageId} package. This setting overrides all of the sources specified in the NuGet.Config files.")
9191
{
9292
Argument = new Argument<string[]>(() => Array.Empty<string>())
@@ -100,7 +100,7 @@ private static Parser BuildCommandLine()
100100

101101
var getVersion = new Command("get-version", "Gets the version information for a project.")
102102
{
103-
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the current directory."),
103+
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the current directory.").LegalFilePathsOnly(),
104104
new Option<string[]>("--metadata", "Adds an identifier to the build metadata part of a semantic version.")
105105
{
106106
Argument = new Argument<string[]>(() => Array.Empty<string>())
@@ -120,15 +120,15 @@ private static Parser BuildCommandLine()
120120

121121
var setVersion = new Command("set-version", "Updates the version stamp that is applied to a project.")
122122
{
123-
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the root directory of the repo that spans the current directory, or an existing version.json file, if applicable."),
123+
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the root directory of the repo that spans the current directory, or an existing version.json file, if applicable.").LegalFilePathsOnly(),
124124
new Argument<string>("version", "The version to set."),
125125
};
126126

127127
setVersion.Handler = CommandHandler.Create<string, string>(OnSetVersionCommand);
128128

129129
var tag = new Command("tag", "Creates a git tag to mark a version.")
130130
{
131-
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the root directory of the repo that spans the current directory, or an existing version.json file, if applicable."),
131+
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the root directory of the repo that spans the current directory, or an existing version.json file, if applicable.").LegalFilePathsOnly(),
132132
new Argument<string>("versionOrRef", () => DefaultRef, $"The a.b.c[.d] version or git ref to be tagged.")
133133
{
134134
Arity = ArgumentArity.ZeroOrOne,
@@ -139,7 +139,7 @@ private static Parser BuildCommandLine()
139139

140140
var getCommits = new Command("get-commits", "Gets the commit(s) that match a given version.")
141141
{
142-
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the root directory of the repo that spans the current directory, or an existing version.json file, if applicable."),
142+
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the root directory of the repo that spans the current directory, or an existing version.json file, if applicable.").LegalFilePathsOnly(),
143143
new Option<bool>(new[] { "--quiet", "-q" }, "Use minimal output."),
144144
new Argument<string>("version", "The a.b.c[.d] version to find."),
145145
};
@@ -148,7 +148,7 @@ private static Parser BuildCommandLine()
148148

149149
var cloud = new Command("cloud", "Communicates with the ambient cloud build to set the build number and/or other cloud build variables.")
150150
{
151-
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory used to calculate the version. The default is the current directory. Ignored if the -v option is specified."),
151+
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory used to calculate the version. The default is the current directory. Ignored if the -v option is specified.").LegalFilePathsOnly(),
152152
new Option<string[]>("--metadata", "Adds an identifier to the build metadata part of a semantic version.")
153153
{
154154
Argument = new Argument<string[]>(() => Array.Empty<string>())
@@ -173,7 +173,7 @@ private static Parser BuildCommandLine()
173173

174174
var prepareRelease = new Command("prepare-release", "Prepares a release by creating a release branch for the current version and adjusting the version on the current branch.")
175175
{
176-
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the current directory."),
176+
new Option<string>(new[] { "--project", "-p" }, "The path to the project or project directory. The default is the current directory.").LegalFilePathsOnly(),
177177
new Option<string>("--nextVersion", "The version to set for the current branch. If omitted, the next version is determined automatically by incrementing the current version."),
178178
new Option<string>("--versionIncrement", "Overrides the 'versionIncrement' setting set in version.json for determining the next version of the current branch."),
179179
new Option<string>(new[] { "--format", "-f" }, $"The format to write information about the release. Allowed values are: {string.Join(", ", SupportedFormats)}. The default is {DefaultOutputFormat}.").FromAmong(SupportedFormats),
@@ -196,12 +196,12 @@ private static Parser BuildCommandLine()
196196
prepareRelease,
197197
};
198198

199-
var builder = new CommandLineBuilder(root);
200-
builder
199+
return new CommandLineBuilder(root)
201200
.UseDefaults()
202201
.UseMiddleware(context =>
203202
{
204203
// System.CommandLine 0.1 parsed arguments after optional --. Restore that behavior for compatibility.
204+
// TODO: Remove this middleware when https://github.com/dotnet/command-line-api/issues/1238 is resolved.
205205
if (context.ParseResult.UnparsedTokens.Count > 0)
206206
{
207207
var arguments = context.ParseResult.CommandResult.Command.Arguments;
@@ -214,9 +214,8 @@ private static Parser BuildCommandLine()
214214
.ToArray());
215215
}
216216
}
217-
}, (MiddlewareOrder)(-3000)); // MiddlewareOrderInternal.ExceptionHandler so [parse] directive is accurate.
218-
219-
return builder.Build();
217+
}, (MiddlewareOrder)(-3000)) // MiddlewareOrderInternal.ExceptionHandler so [parse] directive is accurate.
218+
.Build();
220219
}
221220

222221
private static int MainInner(string[] args)
@@ -290,7 +289,7 @@ private static int OnInstallCommand(string path, string version, IReadOnlyList<s
290289
var setVersionExitCode = OnSetVersionCommand(path, version);
291290
if (setVersionExitCode != (int)ExitCodes.OK)
292291
{
293-
return (int)setVersionExitCode;
292+
return setVersionExitCode;
294293
}
295294
}
296295
}

0 commit comments

Comments
 (0)