Using LINQ to Query the Command Line args

[I just happened upon this one.. this is pretty cool (not sure how it performs, but then again you are probably only talking a few cycles difference)].

I typically parse command line arguments like this:

   1: // Class/Namespace deleted for brevity
   2: static void Main(string[] args)
   3: {
   4:     bool AAArgumentExists = false;
   5:     foreach(string arg in args)
   6:     {
   7:         switch(arg)
   8:         {
   9:             case "-aa":
  10:                 AAArgumentExists = false;
  11:                 break;
  12:             // Additional cases follow
  13:         }
  14:     }
  15: }

 

Today, I just started thinking about this code... I really don't like it.  It doesn't really evoke what I want to say.  I want my code to say "does such and such argument exist in the string array."
Using LINQ we get a cleaner (in my opinion) variation of the above and it "says" what I want it to "say."

   1: // Class/Namespace deleted for brevity
   2: static void Main(string[] args)
   3: {
   4:     bool AAArgumentExists = (from arg in args
   5:                                   where arg == "-aa"
   6:                                   select arg != "").FirstOrDefault();
   7: }

Of course something else I tend to do is have an argument for passing in a  date value (just something I seem to run into on a regular basis).  Here's the code snippet to do that (I wish it were a little more terse... but I'm still a newbie to LINQ).

   1: DateTime RunDate;
   2: String RunDateString = (from arg in args
   3:                         where DateTime.TryParse(arg, out RunDate)
   4:                         select arg).FirstOrDefault();
   5: if (RunDateString == null || RunDateString == "")
   6:     RunDate = DateTime.MinValue; /* Or whatever default value you want to use */

BTW, the above technique could be applied to a number of other data types.

Print | posted on Monday, November 26, 2007 8:19 AM

Feedback

# re: Using LINQ to Query the Command Line args

left by dbalzer at 11/26/2007 12:24 PM Gravatar

I like the simplicity of that.  It certainly seems like a more elegant solution to me.  I will be curious to see what kind of performance you get from it.  

# re: Using LINQ to Query the Command Line args

left by George Tsiokos at 11/26/2007 3:37 PM Gravatar

You could also use:

bool AAArgumentExists = args.Contains("-aa");

# re: Using LINQ to Query the Command Line args

left by jkimble at 11/26/2007 5:37 PM Gravatar

George,

You know when I did this earlier today, Contains was not in my intellisense and I believed it and moved on... I guess maybe I rely too much on Intellisense...

Jay

# re: Using LINQ to Query the Command Line args

left by George Tsiokos at 11/26/2007 8:58 PM Gravatar

I had no idea this extension method existed until I began my quest to find out how to do a "where x in (a,b,c)" in linq to SQL...

# re: Using LINQ to Query the Command Line args

left by jkimble at 11/27/2007 5:13 AM Gravatar

Ahhh... You know what I must have had R# (ReSharper) enabled and it doesn't do the new VS2008 stuff (but the main tip I have gotten is to use ctl+8 to turn it off on a file by file basis)

# re: Using LINQ to Query the Command Line args

left by David in NZ at 11/29/2007 4:03 PM Gravatar

Rather than this line:

if (RunDateString == null || RunDateString == "")

how about...

if (string.IsNullOrEmpty(RunDateString))

# re: Using LINQ to Query the Command Line args

left by jkimble at 11/29/2007 4:56 PM Gravatar

David in NZ,

You are correct.. I could have done it that way... I was operating on the sample pretty quickly...

Title  
Name
Email (never displayed)
Url
Comments   
Please add 3 and 5 and type the answer here:
div>