Click here to Skip to main content
15,120,814 members
Articles / Programming Languages / C#
Posted 28 May 2006


16 bookmarked

Extracts Arguments Passed by User to a Console Application

Rate me:
Please Sign up or sign in to vote.
2.82/5 (7 votes)
28 May 2006CPOL2 min read
This article shows how to extract arguments passed by user to a console application


There are many ways of getting the values of arguments from Main(string[] args). When you write a console type application that expects a user to pass some parameters, for example...

copyFile /source:c:\test.txt /destination:g:\new_test.txt

... you'll need to grab:

  • c:\test.txt as the source file, and
  • g:\new_test.txt as destination

CmdArgsExtrator.dll can help you achieve this in just a few lines of code.

Step 1 - Preparation

  1. Download and extract CmdArgsExtractor.dll
  2. Open an existing or create a new "Console Application" project
  3. Add a reference to CmdArgsExtractor.dll

Step 2 - Using Constructors

Option 1

Let's say, we expect a user to pass arguments in the following format:
"/a:alpha", "/b:beta", "/t:theta"

Image 1

First, declare your valid prefixes.
For the above example, you'll code this as follows:

string[] prefixes = new string[] { "/a:", "/b:", "/t:" };

Now instantiate the CmdArgsExtractor class, where:

  • '/' is the argument separator, and
  • ':' is the argument value separator
CmdArgExtractor cae = new CmdArgExtractor(prefixes, '/', ':');

Option 2

If your program takes arguments in the following format...

Image 2

... then you would instantiate the class in this way:

CmdArgExtractor cae = new CmdArgExtractor('/');

Now you are ready to use the class methods.

Step 3 - Validation

There are a couple of things you may want to check before extracting arguments specified by the user.

1. Do We Have Any Arguments At All?

if ((args.Length != 0) && (args[0].Contains("?"))) {
   // show syntax

2. Did the User Supply Valid Prefixes?

if (!cae.ValidArgsPrefixes(args)) {
   // ... show syntax

Step 4 - Extracting the Arguments

The class implements three methods:

GetArgValues(string[] args)

This returns an array of argument values, and can be used when arguments are expected to be in either "/alpha /beta" or "/a:alpha /b:beta" format.

GetArgsTwoDimArray(string[] args)

This is useful when the arguments are in the format "/a:alpha /b:beta" and you want to code different options for each argument value.
It returns a two-dimensional array where, the first dimension elements are "qualifiers" - for the above example, they will be "a" and "b", and the second dimension elements are the argument values - "alpha" and "beta"

GetArgValuesSimple(string[] args)

This is the same as GetArgValues but is used when you expect arguments in the format "/alpha /beta". This method again will return an array containing argument values, e.g. "alpha" and "beta".


using System;

namespace Xealcom.Utils {

   /// <span class="code-SummaryComment"><summary></span>
   /// This just a simple demo test app.
   /// <span class="code-SummaryComment"></summary></span>
   class Program {
      /// <span class="code-SummaryComment"><summary></span>
      /// Expects 3 arguments in the format '/arg:param'
      /// <span class="code-SummaryComment"></summary></span>
      static void Main(string[] args) {

         // assume that this is what user passed as parameters
         // this line is only for demo purposes
         args = new string[] { "/a:alpha", "/b:beta", "/t:theta" };

         // validate the number of args if necessary
         if (args.Length != 3) {
            // show syntax

         // Check if user is asking for help with the syntax
         if ((args.Length != 0) && (args[0].Contains("?"))) {
            // show syntax

         // instantiate the CmdArgsExtrator
         // this is what we expect as args prefixes
         string[] prefixes = new string[] { "/a:", "/b:", "/t:" };
         CmdArgExtractor cae = new CmdArgExtractor(prefixes, '/', ':');

         // validate prefixes
         if (!cae.ValidArgsPrefixes(args)) {
            // ... show syntax perhaps

         // Get only the values of args in one-dim array
         // this method is useful if you expect arguments without 
         // args value separator like this "/alpha /beta /theta"
         string[] myArgs = cae.GetArgValues(args);
         Console.WriteLine("---- GetArgValues output ----");
         foreach (string arg in myArgs) {

         // Get 2-dim array where,
         // 1st dim - are args qualifiers, e.g. "a" in "/a:alpha"
         // 2nd dim - are the values, e.g. "alpha" in "/a:alpha"
         string[,] my2dArr = cae.GetArgsTwoDimArray(args);
         Console.WriteLine("---- GetArgsTwoDimArray output ----");
         for (int i = 0; i < my2dArr.GetLength(1); i++) {
            Console.WriteLine("Arg qualifier: {0}\tValue: {1}",
                               my2dArr[0, i], my2dArr[1, i]);

Hope this helps.


  • 28th May, 2006: Initial post


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


About the Author

Alex B. Clarke
Web Developer
United Kingdom United Kingdom
Alex B. Clarke is a professioinal software developer specialising in .Net, C#, Asp.Net, SQL, ADSI, CDO, and other technolgies. He writes applications that automate business processes.

Comments and Discussions

GeneralValid prefix vs Required Prefix Pin
James Cooke26-May-10 17:55
MemberJames Cooke26-May-10 17:55 
GeneralConcerning Source Code Pin
lew2630-May-06 1:58
Memberlew2630-May-06 1:58 

I think your artical is great but can't seem to be able to download the source code. When i go to download it, it say it doesn't exist and to contact the author so if you could send me the source code and or the .dll would be great.

GeneralRe: Concerning Source Code Pin
Alex B. Clarke30-May-06 3:07
MemberAlex B. Clarke30-May-06 3:07 
GeneralRe: Concerning Source Code Pin
lew2630-May-06 3:31
Memberlew2630-May-06 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.