0

I'm currently writing a C# console application and would like to give the user the ability to chose what feature to be executed. For example, when I start MyApp.exe -help the help should show, or when I start MyApp.exe -shutdown the computer should shutdown. How do I pass/check that?

My current code doesn't seem to work:

static void Main(string[] args)
{
        if (args.Equals("-help"))
        {
            Console.WriteLine("Some help here...");
        }
}
5
  • 4
    Clue: args is an array. And then read learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… or dotnetperls.com/main
    – ADyson
    Apr 5, 2020 at 13:32
  • 1
    It might be best to find a command line argument parser library. Take a look at this question for some ideas. It appears that there's a Microsoft one. Apr 5, 2020 at 13:37
  • @ADyson: I have already read this, but it doesn't really help me.
    – Daruk
    Apr 5, 2020 at 13:37
  • 2
    args.Equals("-help") checks if the array object is equal to -help, not if one of its items equals -help. How might you achieve the latter (which is what you want)? Apr 5, 2020 at 13:38
  • 1
    Why doesn't it help? Look more carefully at how they access a specific argument value, versus how you've done it. As John says, you're testing the entire array, not any specific item within it. This is not unique to command line arguments, it's the same for any array
    – ADyson
    Apr 5, 2020 at 13:40

3 Answers 3

2
    static void Main(string[] args)
    {
        ProcesArguments(args);
    }

    static void ProcesArguments(string[] parameters)
    {
        foreach (var parameter in parameters)
        {
            switch (parameter.ToLower())
            {
                case "-help":
                    DisplayHelp();
                    break;
                case "-reboot":
                    RebootComputer();
                    break;
            }
        }
    }

    static void DisplayHelp()
    {
        Console.WriteLine($"Syntax: {System.AppDomain.CurrentDomain.FriendlyName} <Parameter>");
        // ...
    }

    static void RebootComputer()
    {
        // ...
    }
2

Args is an array, so you have to reference the argument by its index. So args[0] corresponds to the first argument passed in. In addition, check the array length first to ensure an argument was even provided before you try to access it.

static void Main(string[] args)
{
    if (args.Length > 0 && args[0] == "-help")
    {
        Console.WriteLine("Some help here...");
    }
}
1

args is an array so you need to look for -help occurrence in it. You can achieve it using String.Contains() method.

static void Main(string[] args)
{
    if (args.Contains("-help"))
    {
        Console.WriteLine("Some help here...");
    }
}
2
  • @Daruk you're right, String.Contains() sits in System namespace. I'll fix my answer. Apr 5, 2020 at 13:54
  • 1
    args is an array, it isn't a string Apr 5, 2020 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.