Title here
Summary here
Command-line arguments are a common way to parameterize execution of programs. For example, dotnet run Program.cs
uses run
and Program.cs
arguments to the dotnet
command.
using System;
class Program
{
static void Main(string[] args)
{
// Environment.GetCommandLineArgs() provides access to raw command-line
// arguments. Note that the first value in this array
// is the path to the program, and args[1..] holds the
// arguments to the program.
string[] argsWithProg = Environment.GetCommandLineArgs();
string[] argsWithoutProg = args;
// You can get individual args with normal indexing.
string arg = args[2];
Console.WriteLine(string.Join(" ", argsWithProg));
Console.WriteLine(string.Join(" ", argsWithoutProg));
Console.WriteLine(arg);
}
}
To experiment with command-line arguments it’s best to build a binary with dotnet build
first.
$ dotnet build
$ dotnet run -- a b c d
/path/to/program.dll a b c d
a b c d
c
Next we’ll look at more advanced command-line processing with flags.