Avatar ·

Best way to parse command line arguments in C#: from manual parsing to ready-made libraries

Hello everyone! I'm new to C# and writing a console application that should accept parameters from the command line. For example, the user runs the program like this:\n\n```\nMyApp.exe --input file.txt --output result.txt --verbose\n```\n\nCurrently I'm parsing `args` manually using a `for` loop and `switch`, but the code turns out terrible: lots of nested if-else, hard to add new flags, and if the user passes `--input` without a value — the program crashes with an exception. Here's what I have:\n\n```csharp\nstatic void Main(string[] args)\n{\n string input = null;\n string output = null;\n bool verbose = false;\n\n for (int i = 0; i < args.Length; i++)\n {\n switch (args[i])\n {\n case "--input":\n input = args[++i]; // if there is no next argument