Share via


Checking if command line arguments are empty.

Question

Tuesday, September 18, 2018 11:08 PM

I know that if you put only args.length == 0 it would work but how about if you want to check the first argument then later if the second one is empty? Code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                // if there in to text
                MessageBox.Show("Please enter an argument. There is no argument provided!", "Error",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            } else
            {
                // if there is text
                if(args[0].ToLower() == "zip")
                {
                    //if its zip
                    if (args[1] == null)
                    {
                        MessageBox.Show("Please enter an argument. There is no argument provided!", "Error",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    } else
                    {

                    }
                } else if (args[0].ToLower() == "unzip")
                {
                    //if its unzip
                    if (args[1] == null)
                    {
                        MessageBox.Show("Please enter an argument. There is no argument provided!", "Error",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {

                    }
                }
                else
                {
                    // if its neither
                    MessageBox.Show(args[0] + " is invalid. Please enter zip or unzip!", "Error",
   MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }
        }
    }
}

Sincerely,

Griffin

All replies (6)

Wednesday, September 19, 2018 12:25 PM âś…Answered

if (args[0].ToLower() == "zip")
{
    //if its zip
    //if (args[1] == null)
    if(args.Length == 1) // <<<
    {

I tried but it did not work. But, now I understand how .length works. The number should be 2 not 1 as it is getting the amount of arguments not how many characters there are. I changed it to two and everything appears to be working! Thanks for your help!

Sincerely, Griffin

This is the finished code that ended up working for me.

if (args.Length == 2)
                    {

                        Console.WriteLine("it works second arg");
                    } else
                    {
                        MessageBox.Show("Please enter an argument. There is no argument provided!", "Error",
                           MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

Tuesday, September 18, 2018 11:30 PM

if (args[0].ToLower() == "zip")
{
    //if its zip
    //if (args[1] == null)
    if(args.Length == 1) // <<<
    {

- Wayne


Wednesday, September 19, 2018 2:50 AM

Hi Griffin G,

Thank you for posting here.

For your question, you could try to use for loop.

 for (int i = 0; i < args.Length-1; i++)
            {
                if (args[i].Length == 0)
                {
                    // if there in to text
                    MessageBox.Show("Please enter an argument. There is no argument provided!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    // if there is text
                    if (args[i].ToLower() == "zip")
                    {
                        //if its zip
                        if (args[i+1] == null)
                        {
                            MessageBox.Show("Please enter an argument. There is no argument provided!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {

                        }
                    }
                    else if (args[i].ToLower() == "unzip")
                    {
                        //if its unzip
                        if (args[i+1] == null)
                        {
                            MessageBox.Show("Please enter an argument. There is no argument provided!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        // if its neither
                        MessageBox.Show(args[0] + " is invalid. Please enter zip or unzip!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }

                }
            }

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, September 19, 2018 3:48 AM

For your question, you could try to use for loop.

 for (int i = 0; i < args.Length-1; i++)
            {
                if (args[i].Length == 0)
                {
                    // if there in to text
                    MessageBox.Show("Please enter an argument. There is no argument provided!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    // if there is text
                    if (args[i].ToLower() == "zip")
                    {
                        //if its zip
                        if (args[i+1] == null)
                        {
                            MessageBox.Show("Please enter an argument. There is no argument provided!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {

                        }
                    }
                    else if (args[i].ToLower() == "unzip")
                    {
                        //if its unzip
                        if (args[i+1] == null)
                        {
                            MessageBox.Show("Please enter an argument. There is no argument provided!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        // if its neither
                        MessageBox.Show(args[0] + " is invalid. Please enter zip or unzip!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }

                }
            }

Best Regards,

Wendy

No, Wendy. Your code does *not* work correctly. Did you actually test it?

For example, if the user supplies one argument such as "zip" or "unzip"
then your code will NOT issue any messages. It should issue an error 
message under those conditions. e.g. - A correct operation in the first
argument but no file specified on which to perform that operation.

Further, if NO arguments are passed at all your code will NOT issue any
error messages.

If args.Length == 0 your for loop will never get entered.

If args.Length == 1 your for loop will never get entered.

>if (args[i+1] == null)

If there is no argument at args[i+1] then this will throw an out-of-bounds
exception.

>if (args[i].Length == 0)

This will check the length of the string at args[i] - and could only test
true if an empty string were passed as an argument.

Why use a for loop when the application is intended to always accept two
and only two arguments?

  • Wayne

Wednesday, September 19, 2018 7:36 AM

Try the next approach too:

switch( args.ElementAtOrDefault( 0 )?.ToLower() )

{

case "zip":

    Zip( args );

    break;

case "unzip":

    Unzip( args );

    break;

case null:

    // Error: no arguments

    break;

default:

    // Error: unreconised first argument

    break;

}

 

where Zip and Unzip are separate functions that use a similar switch to analyse the second argument.


Wednesday, September 19, 2018 1:03 PM

if (args[0].ToLower() == "zip")
{
    //if its zip
    //if (args[1] == null)
    if(args.Length == 1) // <<<
    {

I tried but it did not work. But, now I understand how .length works. The number should be 2 not 1 as it is getting the amount of arguments not how many characters there are. I changed it to two and everything appears to be working! 

This is the finished code that ended up working for me.

if (args.Length == 2)
                    {

                        Console.WriteLine("it works second arg");
                    } else
                    {
                        MessageBox.Show("Please enter an argument. There is no argument provided!", "Error",
                           MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

>I tried but it did not work.

In what way did it "not work"? If you made the change I suggested **i*n the ***
two places it was needed then your code would indeed "work".

That is, it would check to ensure that the first argument is either "zip"
or "unzip". If not it would give a message. If it is either of those two
strings then it will check to ensure that there is a second argument.
If there isn't then it will issue a message.

The only thing fundamentally wrong with your first example was the
attempt to access a non-existent array element when an argument was
missing.

Your reworked code simply approaches the checking from a different angle,
by first ensuring that there are two arguments regardless of their
contents. That circumvents the need to check for a missing argument in
two different places in your code, as in your first example. But you must 
still verify that the arguments are correct.

>The number should be 2 not 1

No, not in your first code. It was checking to see if there was only one
argument and issuing an error message if that was true. But in your revised
code in your "answer" you have changed the logic to check for a valid
condition (2 arguments present) rather than checking for an invalid condition
(only one argument) as in your original code. Either approach will "work",
assuming that the rest of the code is correct.

References:

Main() and command-line arguments (C# Programming Guide)
/en-us/dotnet/csharp/programming-guide/main-and-command-args/

Command-Line Arguments (C# Programming Guide)
/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments

  • Wayne