src/Knifer/Gimela.Knifer.CommandLines.Foundation/CommandLineParser.cs
/// <summary>
/// Parses the passed command line arguments and returns the result
/// in a CommandLineOptions object.
/// </summary>
/// <param name="args">Array of command line arguments</param>
/// <param name="list">Array of command line arguments</param>
/// <returns>Object containing the parsed command line</returns>
Parses the passed command line arguments and returns the result
in a CommandLineOptions object.
Parses the passed command line arguments and returns the result in a CommandLineOptions object .
public static CommandLineOptions Parse(string[] args, params string[] singleOptionList)
    {
      if (args == null)
        throw new ArgumentNullException("args");
      CommandLineOptions cmdOptions = new CommandLineOptions();
      int index = 0;
      if (index < args.Length)
      {
        string token = NextToken(args, ref index);
        while (!string.IsNullOrEmpty(token))
        {
          if (IsArgument(token))
          {
            string arg = token.TrimStart(OptionStartWithChars).TrimEnd(OptionEqualChar);
            string value = string.Empty;
            if (arg.Contains(OptionEqualChar))
            {
              string[] r = arg.Split(new char[] { OptionEqualChar }, 2);
              if (r.Length == 2)
              {
                arg = r[0];
                value = r[1];
              }
            }
            bool isSingleOption = false;
            if (singleOptionList != null)
            {
              for (int i = 0; i < singleOptionList.Length; i++)
              {
                if (arg == singleOptionList[i])
                {
                  isSingleOption = true;
                  break;
                }
              }
            }
            while (!isSingleOption && string.IsNullOrEmpty(value))
            {
              index++;
              if (index < args.Length)
              {
                string next = NextToken(args, ref index);
                if (!string.IsNullOrEmpty(next))
                {
                  if (IsArgument(next))
                  {
                    index--;
                    value = MagicOptionValue;
                    break;
                  }
                  else if (next != OptionEqualChar.ToString())
                  {
                    value = next.TrimStart(OptionEqualChar);
                  }
                }
              }
              else
              {
                index--;
                value = MagicOptionValue;
                break;
              }
            }
            if (cmdOptions.Arguments.ContainsKey(arg))
              throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                "Option used in invalid context -- {0}", "option with the same argument."));
            if (value == MagicOptionValue)
            {
              cmdOptions.Arguments.Add(arg, string.Empty);
            }
            else
            {
              cmdOptions.Arguments.Add(arg, value.TrimStart('\'').TrimEnd('\''));
            }
          }
          else
          {
            if (cmdOptions.Parameters.Contains(token))
              throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                "Option used in invalid context -- {0}", "option with the same argument."));
            cmdOptions.Parameters.Add(token.TrimStart('\'').TrimEnd('\''));
          }
          index++;
          if (index < args.Length)
          {
            token = NextToken(args, ref index);
          }
          else
          {
            break;
          }
        }
      }
      return cmdOptions;
    }
public static CommandLineOptions Parse ( string [ ] args , params string [ ] singleOptionList ) { if ( args == null ) throw new ArgumentNullException ( " " ) ; CommandLineOptions cmdOptions = new CommandLineOptions ( ) ; int index = 0 ; if ( index < args . Length ) { string token = NextToken ( args , ref index ) ; while ( ! string . IsNullOrEmpty ( token ) ) { if ( IsArgument ( token ) ) { string arg = token . TrimStart ( OptionStartWithChars ) . TrimEnd ( OptionEqualChar ) ; string value = string . Empty ; if ( arg . Contains ( OptionEqualChar ) ) { string [ ] r = arg . Split ( new char [ ] { OptionEqualChar } , 2 ) ; if ( r . Length == 2 ) { arg = r [ 0 ] ; value = r [ 1 ] ; } } bool isSingleOption = false ; if ( singleOptionList != null ) { for ( int i = 0 ; i < singleOptionList . Length ; i ++ ) { if ( arg == singleOptionList [ i ] ) { isSingleOption = true ; break ; } } } while ( ! isSingleOption && string . IsNullOrEmpty ( value ) ) { index ++ ; if ( index < args . Length ) { string next = NextToken ( args , ref index ) ; if ( ! string . IsNullOrEmpty ( next ) ) { if ( IsArgument ( next ) ) { index -- ; value = MagicOptionValue ; break ; } else if ( next != OptionEqualChar . ToString ( ) ) { value = next . TrimStart ( OptionEqualChar ) ; } } } else { index -- ; value = MagicOptionValue ; break ; } } if ( cmdOptions . Arguments . ContainsKey ( arg ) ) throw new CommandLineException ( string . Format ( CultureInfo . CurrentCulture , " " , " " ) ) ; if ( value == MagicOptionValue ) { cmdOptions . Arguments . Add ( arg , string . Empty ) ; } else { cmdOptions . Arguments . Add ( arg , value . TrimStart ( ' \' ' ) . TrimEnd ( ' \' ' ) ) ; } } else { if ( cmdOptions . Parameters . Contains ( token ) ) throw new CommandLineException ( string . Format ( CultureInfo . CurrentCulture , " " , " " ) ) ; cmdOptions . Parameters . Add ( token . TrimStart ( ' \' ' ) . TrimEnd ( ' \' ' ) ) ; } index ++ ; if ( index < args . Length ) { token = NextToken ( args , ref index ) ; } else { break ; } } } return cmdOptions ; }
Parses the passed command line arguments and returns the result
in a CommandLineOptions object.
Parses the passed command line arguments and returns the result in a CommandLineOptions object .
// arg was specified with an '=' sign, so we need // to split the string into the arg and value, but only // if there is no space between the '=' and the arg and value. // single option do not need a following parameter // find following parameter // push the token back onto the stack so // it gets picked up on next pass as an arg // save the value (trimming any '=' from the start) // save the pair // save stand-alone parameter
Object containing the parsed command line
Object containing the parsed command line
args
string[]
Array of command line arguments
Array of command line arguments
list
Array of command line arguments
Array of command line arguments