#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
#if ENABLE_BROKEN_COMMAND_LINE_PARSER
namespace Nuclex.Support.Parsing {
  partial class CommandLine {
    /// Option being specified on an application's command line
    public class Option {
      /// Initializes a new option with only a name
      /// 
      ///   String segment containing the entire option as it was given on the command line
      /// 
      /// Absolute index the option name starts at
      /// Number of characters in the option name
      /// The newly created option
      internal Option(
        StringSegment raw,
        int nameStart, int nameLength
      )
        : this(raw, nameStart, nameLength, -1, -1) { }
      /// Creates a new option with a name and an assigned value
      /// 
      ///   String segment containing the entire option as it was given on the command line
      /// 
      /// Absolute index the option name starts at
      /// Number of characters in the option name
      /// Absolute index the value starts at
      /// Number of characters in the value
      /// The newly created option
      internal Option(
        StringSegment raw,
        int nameStart, int nameLength,
        int valueStart, int valueLength
      ) {
        this.raw = raw;
        this.nameStart = nameStart;
        this.nameLength = nameLength;
        this.valueStart = valueStart;
        this.valueLength = valueLength;
        Debug.Assert(this.nameStart != -1, "Name start index must not be -1");
        Debug.Assert(this.nameLength != -1, "Name length must not be -1");
      }
      /// Contains the raw string the command line argument was parsed from
      public string Raw {
        get { return this.raw.ToString(); }
      }
      /// Characters used to initiate this option
      public string Initiator {
        get {
          return this.raw.Text.Substring(
            this.raw.Offset, this.nameStart - this.raw.Offset
          );
        }
      }
      /// Name of the command line option
      public string Name {
        get {
          return this.raw.Text.Substring(this.nameStart, this.nameLength);
        }
      }
      /// Characters used to associate a value to this option
      public string Associator {
        get {
          int associatorStart = this.nameStart + this.nameLength;
          if(this.valueStart == -1) {
            int characterCount = (this.raw.Offset + this.raw.Count) - associatorStart;
            if(characterCount == 0) {
              return null;
            }
          }
          return this.raw.Text.Substring(associatorStart, 1);
        }
      }
      /// Name of the command line option
      public string Value {
        get {
          if(this.valueStart == -1) {
            return null;
          } else {
            return this.raw.Text.Substring(this.valueStart, this.valueLength);
          }
        }
      }
      /// 
      ///   Contains the entire option as it was specified on the command line
      /// 
      private StringSegment raw;
      /// Absolute index in the raw string the option name starts at
      private int nameStart;
      /// Number of characters in the option name
      private int nameLength;
      /// Absolute index in the raw string the value starts at
      private int valueStart;
      /// Number of characters in the value
      private int valueLength;
    }
  }
} // namespace Nuclex.Support.Parsing
#endif // ENABLE_BROKEN_COMMAND_LINE_PARSER