9

This is from a newer C# guy,

I have been back and forth through different questions posed on here but I haven't found anything that answers directly to what I need to know.

I have a console application that I want to pass arguments to, through the command line. This is what I have so far and it's working for a single argument, now I got to add another but I can't seem to figure out where to start.

static void Main(string[] args)
{
    if (args == null || args.Length== 0)
    {
        Console.WriteLine("that's not it");
        help();
    }
    else
    {
        for (int i = 0; i < args.Length; i++)
        {
            backupfolder = args[i];
        }
        checks();
    }
}

If I take everything out of my else statement how can I set what the args are and assign? Will the below work ?

static void Main(string[] args)
{
    if (args == null || args.Length== 0)
    {
        Console.WriteLine("that's not it");
        help();
    }
    else
    {
        string backupfolder = args[0];
        string filetype = args[1];
        checks();
    }
}
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
user3542866
  • 181
  • 12
  • `args[1];` this may not work with your current code – Agent_Orange Mar 20 '18 at 06:36
  • 1
    you need to add null check for second argument, cuz its possible that second argument is not sent and is null – Agent_Orange Mar 20 '18 at 06:38
  • Pass arguments by separated by white space – Gaurang Dave Mar 20 '18 at 06:38
  • 2
    Well, you can't be sure if you actually received 2 arguments unless you check it first, right? If you don't have 2nd argument, i.e args[1], you will get an exception. *Edit: And that's a pretty good formatted question for a "newer C# guy", you have my upvote :) – uTeisT Mar 20 '18 at 06:38
  • Start with `args.Length != 2` *(in the first check)* when there should be always two arguments, or `args.Length >= 2` when there should be at least two arguments. – Julo Mar 20 '18 at 06:43
  • Possible duplicate of [read command line switch](https://stackoverflow.com/q/9742924/1260204) – Igor Mar 20 '18 at 06:57
  • Possible duplicate of [Best way to parse command line arguments in C#?](https://stackoverflow.com/q/491595/1260204) – Igor Mar 20 '18 at 06:59
  • Consider using https://www.nuget.org/packages/commandlineparser/ – mjwills Mar 20 '18 at 07:00

1 Answers1

7

You need to check the length of the args array before attempting to retrieve values from it:

static void Main(string[] args)
{
    // There must be at least 2 command line arguments...
    if (args == null || args.Length < 2)
    {
        Console.WriteLine("that's not it");
        help();
    }
    else 
    {
        string backupfolder = args[0]; 
        string filetype = args[1];
        checks();
    }
}

Another option, if you want to allow passing only some of the expected arguments:

static void Main(string[] args)
{
    // There must be at least 1 command line arguments.
    if (args == null || args.Length < 1)
    {
        Console.WriteLine("that's not it");
        help();
    }
    else 
    {
        // You already know there is at least one argument here...
        string backupfolder = args[0]; 
        // Check if there is a second argument, 
        // provide a default value if it's missing
        string filetype = (args.Length > 1) ? args[1] : "" ;
        checks();
    }
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Thank you, the second option was going to be my next question, so you hit it on the mark. Thanks, everyone. – user3542866 Mar 20 '18 at 06:59
  • 2
    Although most hobby programmers feel a bit like cheating by using libraries, in this case I can really recommend the Command Line Parser package on NuGet: https://www.nuget.org/packages/commandlineparser. It allows you to have *NIX-like command lines: `--backup myfolder -t filetype --file file1 --file file2 -f file3` and have them automatically parsed into a class. – CompuChip Mar 20 '18 at 07:22