Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Using Multiple Arguments

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();
    }
}
like image 904
user3542866 Avatar asked Mar 20 '18 06:03

user3542866


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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();
    }
}
like image 158
Zohar Peled Avatar answered Oct 27 '22 03:10

Zohar Peled