I made a program for parsing command line arguments... My code:
for (var x = 0; x < args.Length; x++)
{
switch (args[x].Trim())
{
case "--message":
if (args[x + 1] == null) // <= the problem
{
Console.WriteLine("Option {0} requires an argument.", args[x]);
Environment.Exit(1);
}
else
{
Console.WriteLine("Your message: " + args[x + 1]);
Environment.Exit(0);
}
break;
}
}
So when I type: 'myprogram.exe --message mymessage', the program prints my message without any errors. But my problem is: If no message is specified: 'myprogram.exe --message', it crashes because args[x + 1] doesn't exists. How can I check if args[x + 1] exists?
PS: Thanks for downvote!
Thank you for your answers! -AppPrinter