locked
Checking if command line arguments are empty. RRS feed

  • Question

  • 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

    Tuesday, September 18, 2018 11:08 PM

Answers


  • 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);
                        }

    • Marked as answer by Garlfin Wednesday, September 19, 2018 12:25 PM
    • Edited by Garlfin Wednesday, September 19, 2018 12:26 PM
    Wednesday, September 19, 2018 12:25 PM

All replies

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

    - Wayne

    • Proposed as answer by Ante Meridian Tuesday, September 18, 2018 11:35 PM
    • Unproposed as answer by Garlfin Wednesday, September 19, 2018 12:26 PM
    Tuesday, September 18, 2018 11:30 PM
  • 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 MSDNFSF@microsoft.com.

    Wednesday, September 19, 2018 2:50 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 3:48 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 7:36 AM

  • 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);
                        }

    • Marked as answer by Garlfin Wednesday, September 19, 2018 12:25 PM
    • Edited by Garlfin Wednesday, September 19, 2018 12:26 PM
    Wednesday, September 19, 2018 12:25 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 in 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)
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/

    Command-Line Arguments (C# Programming Guide)
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments

    - Wayne

    Wednesday, September 19, 2018 1:03 PM