Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,

I am wondering if we can pass different count of commandline arguments to a console application.

for example:
test.exe
test.exe arg1
test.exe arg1 arg2
test.exe arg1 arg2 arg3

all the above 4 cmdprompt invocations must work.
Is it possible?

i tried the params option as shown below but its failing with Array out of bound error.
Any other way can we achieve?
Function overloading is also working, but i dont prefer that as i dont want to copy the same logic again and again for different count of args.

What I have tried:

<pre lang="c#">
static void Main(params string[] args)
        {
            try
            {
                int iResult = 0;
                if (args.Length == 0)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                }
                else
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        mainObject.lstArguements.Add(args[i]);
                    }

                    string argHTTPS = args[0];
                    string argCustDBName = args[1];
            }
            catch (Exception ex)
            {
                MessageBox.Show("FATAL ERROR: " + ex.Message);
            }
        }


Its failing somewhere in this statement
string argCustDBName = args[1];

when i pass only 1 argument. when i pass 2 arguments it works. Can we somehow overcome this, to support dynamic number of arguments?

i want when user passes only 1 arg then it should save in [0] and [1] should be empty or null.
And when user passes only 2 arg then it should save in both [0] and [1]
and if user passes 3 arg then it should save in [0], [1], [2].

Please help
Posted
Updated 22-Aug-18 4:09am

C#
string argHTTPS = args[0];
string argCustDBName = args[1];

That will only ever work if you have two or more parameters. If you want it to be dynamic then you need check how many parameters are being passed in. Start by setting default values for all the optional parameters, and then replace any that are provided on the command line.
 
Share this answer
 
v2
Your code doesn't make any sense. If the user doesn't pass in any args, it starts a Windows Forms app.

If the user does pass in arguments, you add every argument to what appears to be a List (I have no idea because you never show the definition of mainObject!), assign the first two arguments to two different variables even if only 1 argument is supplied(!) and then throw all of that out and do nothing with any of it.

Oh, and you're code is missing a closing curly brace just before the catch statement.

One way of assigning arguments to individual variables is to check for the number of arguments:
C#
// Init the variables to empty strings for default values.
string argHTTPS = string.Empty;
string argCustDbName = string.Empty;

if (args.Count() > 0)
{
    argHTTPS = args[0];
}

if (args.Count() > 1)
{
    argCustDbName = args[1];
}

But, like I said, you're not doing anything with these variables and I have no idea what mainObject is, so I think you're doing something that you should not be doing in the Main method of your startup code.
 
Share this answer
 
Comments
Dinesh Kumar Dora 23-Aug-18 2:22am    
Hi Dave thanks for the response. This is corporate confidential code so obviously i cant publish all the logic or details. Why this variable is used? why list is used? what is mainobject? etc.. But wrt to the question whatever is relevant information, i have provided. And yes, i missed that closing curly braces while formatting the code snippet.

Anyways, i have decided to condition the logic based on the number of arguments passed. This is not exactly what i was looking for, but atleast it works. Thanks everyone for the suggestions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900