You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using System.CommandLine;namespace CommandLineTest;internalclassProgram{privatestaticasyncTask<int>Main(string[]args){varfileOption=newOption<FileInfo?>(
name:"--file",
description:"The file to read and display on the console.");varrootCommand=new RootCommand("Sample app for System.CommandLine");
rootCommand.AddOption(fileOption);
rootCommand.SetHandler((file)=> ReadFile(file!),
fileOption);returnawait rootCommand.InvokeAsync(args);}privatestaticvoidReadFile(FileInfofile){
File.ReadLines(file.FullName).ToList().ForEach(Console.WriteLine);}}
You might consider updating the getting started docs to note the problem and/or update the path used for the dotnet run example - for example: dotnet run -- --file .\bin\Debug\net8.0\CommandLineTest.runtimeconfig.json
Following the getting started tutorial:
and with a
launchSettings.json
:If you start the project from Visual Studio, then it works as expected and writes the contents of the file to the console.
If you start the built project from the
bin\Debug\net8.0
directory*, then it works as expected and writes the contents of the file to the console.If you start the project using
dotnet run -- --file CommandLineTest.runtimeconfig.json
, then you get an exception:This is due to difference in how current directory is set when running from Visual Studio vs. CLI.
FileInfo
(andDirectoryInfo
) end up using current directory to normalize relative paths.If you add the following lines to
Main()
and run from CLI and Visual Studio you can observe the differences:You might consider updating the getting started docs to note the problem and/or update the path used for the
dotnet run
example - for example:dotnet run -- --file .\bin\Debug\net8.0\CommandLineTest.runtimeconfig.json
Related:
Environment.CurrentDirectory
has wrong value when usingdotnet run
sdk#30724dotnet run
and VS F5 is different sdk#9949The text was updated successfully, but these errors were encountered: