Reputation: 230
I am running an inherited project written in C# inside Visual Studio Code. In order for this application to run, it needs to take command line input (-t, -h, etc). How do I test this from inside Visual Studio?
Currently (I've been learning dotnet, C#, VS, etc as I go) I have a hello world program I can run from vsc's terminal. For a reason I haven't been able to pinpoint, probably how I installed it, dotnet run
isn't recognized - I have to feed it an explicit path to dotnet.exe: C:\Program Files\dotnet\dotnet.exe run
How can I do this when the program requires command line input? My shot in the dark of C:\Program Files\dotnet\dotnet.exe run -t
predictably didn't work, but I'm not sure what else to try.
Thanks!
Upvotes: 5
Views: 8422
Reputation: 450
Since OP is specifically asking about how to do this in Visual Studio Code, they are likely using VSCode's run feature (not the dotnet CLI as other answers assume), to run their application. In this case, the proper way to supply command line arguments is via the .vscode\launch.json
file.
Add an args
array attribute to your configuration and populate it with the arguments you would like to pass.
"configurations": [
{
// ... ,
"args": ["-arg1", "-arg2"]
}
Upvotes: 1
Reputation: 51
Run with Terminal
When you run your code using terminal you must add ' -- ' to tell dotnet you are running your code with an argument/s
C:>dotnet run --
<your arguments here>
Run with Debugger
"configurations": [
{
"name": ".NET Core Launch (console)",
"args": [], // PUT YOUR ARGUMENTS HERE
...
}
]
Upvotes: 5
Reputation: 21
I tried to add a comment to Nico's answer but I lack sufficient reputation points. I was confused by the dash character in front of each arg: "-arg1 -arg2 (etc)". For clarity I would like to point out that .NET Core 2.1 seems to not need this. In the case of my console app, it takes a date for the first arg, an integer for the second, then an operator (+ or -) for the third arg. If I entered the following:
C:\>dotnet run -- -7/13/2018 -30 -+
I found that the leading dash in front of each arg was passed into the program along with the intended arg and I ended up trying to date parse "-7/13/2018"
I got the expected result when I entered it like this:
C:\>dotnet run -- 7/13/2018 30 +
Upvotes: 0
Reputation: 365
Upvotes: -1
Reputation: 12683
If you are using dotnet.exe run
to start your application you need add the --
switch statement to instruct dotnet.exe
to pass the arguments to your application. For example
dotnet.exe run -- -arg1 -arg2 (etc)
notice the --
after the dotnet
arguments and before your program specific arguments.
Upvotes: 4