Click here to Skip to main content
12,831,317 members (33,354 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 28 May 2006

Extracts Arguments Passed by User to a Console Application

, 28 May 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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"

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...

... 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.

You may also be interested in...


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 
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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170326.1 | Last Updated 28 May 2006
Article Copyright 2006 by Alex B. Clarke
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid