parse parameter in csharp

To parse command-line arguments in a C# console application, you can use the args parameter of the Main method.

The args parameter is an array of strings, where each element is a command-line argument. You can loop through this array and use each argument as needed.

Here's an example of how to parse command-line arguments in C#:

main.cs
static void Main(string[] args)
{
    // Loop through each argument
    foreach (string arg in args)
    {
        // Check if the argument is a flag
        if (arg.StartsWith("-"))
        {
            // Parse the flag
            switch (arg)
            {
                case "-h":
                case "--help":
                    // Display help information
                    Console.WriteLine("Help information");
                    break;
                case "-v":
                case "--version":
                    // Display version information
                    Console.WriteLine("Version information");
                    break;
                default:
                    // Unknown flag
                    Console.WriteLine($"Unknown flag '{arg}'");
                    break;
            }
        }
        else
        {
            // Parse the argument
            // ...
        }
    }
}
925 chars
35 lines

In this example, we check if each argument is a flag (i.e. starts with "-"). If it's a flag, we parse it accordingly. If it's not a flag, we parse it as a regular argument.

Note that this is just a simple example. Depending on your specific use case, you may need to parse command-line arguments in a different way.

gistlibby LogSnag