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...");
        }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Daruk
  • 3
  • 1
  • 4
    Clue: `args` is an _array_. And then read https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments or https://www.dotnetperls.com/main – ADyson Apr 05 '20 at 13:32
  • 1
    It might be best to find a command line argument parser library. Take a look at [this question](https://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c) for some ideas. It appears that there's a [Microsoft one](https://learn.microsoft.com/en-us/archive/msdn-magazine/2019/march/net-parse-the-command-line-with-system-commandline). – ProgrammingLlama Apr 05 '20 at 13:37
  • @ADyson: I have already read this, but it doesn't really help me. – Daruk Apr 05 '20 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)? – ProgrammingLlama Apr 05 '20 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 05 '20 at 13:40

3 Answers3

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()
    {
        // ...
    }
Steve D
  • 21
  • 2
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...");
    }
}
Tawab Wakil
  • 1,737
  • 18
  • 33
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...");
    }
}
Jarek Danielak
  • 394
  • 4
  • 18