简体   繁体   中英

read command line switch

I'm trying to read user arguments in a C# application. I know how to read them based on position with

string[] args = Environment.GetCommandLineArgs();

but I'd like to read them from switches such as

app.exe /f /d:foo

I'm really struggling to find any information on doing this...

Why don't you just parse the array of arguments passed and act based on them, like this

foreach (string arg in args)
{
    switch (arg.Substring(0, 2).ToUpper())
    {
        case "/F":
            // process argument...
            break;
        case "/Z":
            // process arg...
            break;
        case "/D":
            paramD = arg.Substring(3);
            break;
        default:
            // do other stuff...
            break;
    }
}
void Main(string[] args )
{
    foreach( var arg in args )
    {
        // process each arg if needed, i.e., 
        // remove '-', '/', uppercase, whatever
        switch(arg)
        {
            case "blah":
            // ...
        }
    }
}

Libraries do exist for this purpose which make the whole process a lot easier, but if this is a one off app it may not be worth it. Depends on how complex your command line arguments are.

I've seen a couple of answers that loop through the args string collection - the problem is you need the next item after you hit the switch you are looking for. I used the array index of the collection in order to get the next item.

In my sample I'm looking for a server and a port /S and /P. I hope this helps someone:

        var server = string.Empty;
        var port = string.Empty;

        for(var x = 0; x < args.Count(); x++)
        {
            switch (args[x].Trim().ToUpper())
            {
                case "/S":
                    server = args[x + 1];
                    break;
                case "/P":
                    port = args[x + 1];
                    break;                                        
            }
        }

What about,

// first is exe of executing program
string[] args = Environment.CommandLine.Split('/').Skip(1).ToArray();
foreach (string arg in args)
{
    string value = arg.Trim();
    switch (value)
    {
        case "f":
            //...
            continue;
    }
    if (value.StartsWith("d:"))
    {
        value = value.Substring(2);
        // ...
        continue;
    }
}

Well, basically you already done. Process that string[] array you get from the framework and you done. There is no built-in way to achieve what you're asking for.

Like 3rd parties solution can have a look on

C#/.NET Command Line Arguments Parser

I am the author of an open source .NET command-line library that may suit your needs: C# CLI .

You may also want to read this question for other suggestions.

I believe you'll have to roll your own implementation. After writing unit tests to evaluate the new method, I might start thinking along the lines of ...

foreach (string arg in args)
{
    flagF = flagF || arg == "/f"; // assuming F is boolean
    flagD = flagD ?? (arg.Substring(0,3) =="/d:" ? arg.Substring(3) : null);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM