Click here to Skip to main content
15,886,664 members
Articles / Programming Languages / C#

C# Command Line Parsing

Rate me:
Please Sign up or sign in to vote.
4.71/5 (22 votes)
1 Sep 2008CC (ASA 2.5)7 min read 76.3K   1.1K   103  
A simple base class to extract command line options
/*
 * This work is licenced under the Creative Commons Attribution 3.0 Unported License. 
 * To view a copy of this licence, visit http://creativecommons.org/licenses/by/3.0/ 
 * or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, 
 * California 94105, USA
 * 
 * This license lets you distribute, remix, tweak, and build upon this work, even 
 * commercially, as long as you credit me for the original creation. You can do this
 * by leaving this comment block intact.
 * 
 *      Keith Fletcher
 *      29 Aug 2008 
 *      mailto:fletcher.keith@gmail.com
 *   
 *      Description: Enumerator to support simple iteration over the OptionCollection class.
 *      Version:     1.0
 * 
 */

namespace keithfletcher.org
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Supports a simple iteration over the OptionCollection class.
    /// </summary>
    public sealed class OptionEnumerator : IEnumerator<IOption>
    {
        #region OptionEnumerator Declarations
        int _index = -1;
        bool _anonymousOptionsOnly = false;
        OptionCollection _options = null;
        #endregion OptionEnumerator Declarations

        #region OptionEnumerator Constructors
        internal OptionEnumerator(OptionCollection options)
        {
            _options = options;
            _anonymousOptionsOnly = false;
        }

        internal OptionEnumerator(OptionCollection options, bool anonymousOptionsOnly)
        {
            _options = options;
            _anonymousOptionsOnly = anonymousOptionsOnly;
        }
        #endregion OptionEnumerator Constructors

        #region OptionEnumerator Methods
                /// <summary>
        /// Dispose this enumerator
        /// </summary>
        public void Dispose()
        {            
            _options = null;
        }   

        /// <summary>
        /// Advances the enumerator to the next element of the collection.
        /// </summary>
        /// <returns>true if the enumerator was successfully advanced to the next element; 
        /// false if the enumerator has passed the end of the collection. </returns>
        public bool MoveNext()
        {   
            return (++_index < ((_anonymousOptionsOnly) ? _options.AnonymousCount : _options.Count));
        }

        /// <summary>
        /// Sets the enumerator to its initial position, which is before the first element in the collection.
        /// </summary>
        public void Reset()
        {
            _index = -1;
        }
        #endregion OptionEnumerator Methods
        
        #region OptionEnumerator Properties
        /// <summary>
        /// Gets the current element in the collection. 
        /// </summary>
        public IOption Current
        {
            get 
            {
                if ((_options == null) ||
                    (_index == -1) ||
                    (_index >= ((_anonymousOptionsOnly) ? _options.AnonymousCount : _options.Count)))
                    throw new InvalidOperationException();

                return _options[_index, _anonymousOptionsOnly];
            }
        }

        /// <summary>
        /// Gets the current element in the collection. 
        /// </summary>
        object System.Collections.IEnumerator.Current
        {
            get { return Current; }
        }
        #endregion OptionEnumerator Properties
    }    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Software Developer (Senior) Educos Vision Services
Australia Australia
Keith Fletcher is an avid C# developer, who has been developing software professionally for roughly the last ten years. He started off way back with a Texas Instruments TI-89A, progressed to IBM compatibles, took a brief detour into Electrical Engineering, and finally came back home to computer software development.

Comments and Discussions