OptionResult.cs :  » Development » Command-Line-Option-Parsing » CommandLine » OptParse » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » Command Line Option Parsing 
Command Line Option Parsing » CommandLine » OptParse » OptionResult.cs
/* This file is part of the CSharpOptParse .NET C# library
 *
 * The library is hosted at http://csharpoptparse.sf.net
 *
 * Copyright (C) 2005 by Andrew Robinson
 *
 * This source code is open source, protected under the GNU GPL Version 2, June 1991
 * Please see http://opensource.org/licenses/gpl-license.php for information and
 * specifics on this license.
 */
using System;
using System.Collections;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Diagnostics;

namespace CommandLine.OptParse{
  /// <summary>
  /// Stores the information on the result of parsing an option
  /// <seealso cref="Parser"/>
  /// <seealso cref="IOptionResults"/>
  /// <seealso cref="OptionResultsDictionary"/>
  /// </summary>
  public class OptionResult 
  {
    #region Members
    private ArrayList        _values;
    private OptionDefinition _defintion;
    private int              _numDefinitions;
    #endregion Members

    #region Constructors
    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="def">Definition of the option this result if for</param>
    public OptionResult(OptionDefinition def)
    {
      _defintion = def;
    }
    #endregion Constructors

    #region Properties
    /// <summary>
    /// Get the definition for this result
    /// </summary>
    public OptionDefinition Defintion 
    {
      get { return _defintion; }
    }


    /// <summary>
    /// Get or set the number of times this option was defined
    /// </summary>
    /// <remarks>
    /// If the type of the option supports multiple definitions or multiple values, then
    /// this property specifies how many times the option was defined
    /// </remarks>
    public int NumDefinitions 
    {
      get { return _numDefinitions; }
      set { _numDefinitions = value; }
    }


    /// <summary>
    /// Get if this property has been defined
    /// </summary>
    public bool IsDefined
    {
      get { return _numDefinitions > 0; }
    }


    /// <summary>
    /// Get or set the value of the option
    /// </summary>
    /// <remarks>
    /// This value will always be null for <see cref="OptValType.Flag"/> style arguments and
    /// may be null for <see cref="OptValType.ValueOpt"/> and 
    /// <see cref="OptValType.MultValue"/> options.
    /// <para>
    /// If the option is defined multiple times, this will get or set the first value given
    /// </para>
    /// </remarks>
    public object Value 
    {
      get 
      { 
        if (_values == null || _values.Count == 0)
          return null;
        else
          return _values[0]; 
      }

      set 
      { 
        CheckType(value);

        if (_values == null)
          AddValue(value);
        else
          _values[0] = value; 
      }
    }


    /// <summary>
    /// Get or set all the values given for this option
    /// <seealso cref="AddValue(object)"/>
    /// </summary>
    /// <remarks>
    /// This property allows access to all the values of the option for use with 
    /// <see cref="OptValType.MultValue"/> type of options
    /// </remarks>
    public ArrayList Values
    {
      get { return _values; }
    }
    #endregion Properties

    #region Methods
    /// <summary>
    /// Add a value
    /// </summary>
    /// <remarks>The value should already be converted to the correct type</remarks>
    /// <param name="value">The value to add</param>
    public void AddValue(object value)
    {
      CheckType(value);
      if (_values == null)
        _values = new ArrayList();
      
      _values.Add(value);
    }

    
    private void CheckType(object value)
    {
      if (value == null && typeof(ValueType).IsAssignableFrom(_defintion.ValueType))
        throw new InvalidValueException("Null is not supported for this type");
      else if (value == null)
        return;
      else if (_defintion.ValueType.IsAssignableFrom(value.GetType()) == false)
        throw new InvalidValueException("Value is not of the correct type");
    }
    #endregion Methods
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.