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();
    }
}
8
  • args[1]; this may not work with your current code Commented Mar 20, 2018 at 6:36
  • 1
    you need to add null check for second argument, cuz its possible that second argument is not sent and is null Commented Mar 20, 2018 at 6:38
  • Pass arguments by separated by white space Commented Mar 20, 2018 at 6: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 :) Commented Mar 20, 2018 at 6: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. Commented Mar 20, 2018 at 6:43

1 Answer 1

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();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, the second option was going to be my next question, so you hit it on the mark. Thanks, everyone.
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: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.