/// <summary>
        /// Gets the long name of the command; used to display the help message.
        /// </summary>
        /// <param name="option">The option to get the long name for.</param>
        /// <returns></returns>
        protected virtual string GetOptionLongName(CommandlineOption option)
        {
            if (option.RequiredPosition == int.MaxValue)
            {
                var sb = new StringBuilder();

                sb.Append(OptionPrefixes[0]);
                sb.Append(OptionPrefixes[0]);
                sb.Append(option.LongName);

                return(sb.ToString());
            }

            return(option.LongName);
        }
        protected override void DoParse(string[] arguments)
        {
            StringBuilder sb = new StringBuilder();

            foreach (string t in arguments)
            {
                sb.Append(" ");
                if (t.Contains(" ") && !(t.StartsWith("\"") && t.EndsWith("\"")))
                {
                    sb.AppendFormat("\"{0}\"", t);
                }
                else
                {
                    sb.Append(t);
                }
            }

            string allArguments = sb.ToString().Trim();

            CommandlineParserState mode             = CommandlineParserState.SearchCommandStart;
            int               characterIndex        = 0;
            int               oldCharacterIndex     = 0;
            string            currentArgument       = string.Empty;
            int               paramSearchStartIndex = 0;
            bool              longArg        = false;
            CommandlineOption currentCommand = null;

            while (characterIndex < allArguments.Length)
            {
                switch (mode)
                {
                case CommandlineParserState.SearchCommandStart:
                    if (IsOptionStart(allArguments, characterIndex))
                    {
                        longArg         = (IsLongOption(allArguments, characterIndex));
                        currentArgument = string.Empty;
                        mode            = CommandlineParserState.GetCommand;
                    }
                    else
                    {
                        currentArgument += allArguments[characterIndex];
                    }
                    characterIndex++;
                    if (longArg)
                    {
                        characterIndex++;
                    }
                    break;

                case CommandlineParserState.GetCommand:
                    if (char.IsWhiteSpace(allArguments[characterIndex]))
                    {
                        mode = CommandlineParserState.SearchCommandStart;
                    }
                    currentArgument += allArguments[characterIndex];
                    currentCommand   = GetArgument(currentArgument, longArg);
                    if (currentCommand != null)
                    {
                        if (DebugParser)
                        {
                            ParserLog.AppendLine(string.Format("Command: {0}", currentArgument));
                        }

                        currentArgument = string.Empty;

                        if (currentCommand.Flags.HasAll(CommandlineOptionFlags.HasParameter))
                        {
                            oldCharacterIndex = characterIndex + 1;
                            mode = CommandlineParserState.SearchParameterStart;
                        }
                        else
                        {
                            DispatchedOptions.Add(currentCommand);
                            DispatchOption(currentCommand, string.Empty);
                            currentCommand = null;
                        }
                    }
                    characterIndex++;
                    break;

                case CommandlineParserState.SearchParameterStart:
                    if (longArg)
                    {
                        if (char.IsWhiteSpace(allArguments[characterIndex]))
                        {
                            characterIndex++;
                        }
                        else
                        {
                            mode = CommandlineParserState.GetParameter;
                            paramSearchStartIndex = characterIndex;
                        }
                    }
                    else
                    {
                        if (!char.IsWhiteSpace(allArguments[characterIndex]))
                        {
                            currentArgument += allArguments[characterIndex];
                            characterIndex++;
                            if (string.IsNullOrEmpty(currentArgument))
                            {
                                mode = CommandlineParserState.SearchParameterStart;
                            }
                            if (GetArgument(currentArgument, false) != null)
                            {
                                currentArgument = string.Empty;
                            }
                        }
                        else
                        {
                            while (characterIndex < allArguments.Length && char.IsWhiteSpace(allArguments[characterIndex]))
                            {
                                characterIndex++;
                            }
                            if (characterIndex < allArguments.Length)
                            {
                                mode = CommandlineParserState.GetParameter;
                                paramSearchStartIndex = characterIndex;
                            }
                            else
                            {
                                mode = CommandlineParserState.SearchCommandStart;
                            }
                        }
                    }
                    break;

                case CommandlineParserState.GetParameter:
                    if (!char.IsWhiteSpace(allArguments[characterIndex]) ||
                        currentArgument.StartsWith("\"", StringComparison.CurrentCulture))
                    {
                        currentArgument += allArguments[characterIndex];
                        characterIndex++;
                        if (characterIndex < allArguments.Length ||
                            (currentArgument.StartsWith("\"", StringComparison.CurrentCulture) &&
                             !currentArgument.EndsWith("\"", StringComparison.CurrentCulture)))
                        {
                            continue;
                        }
                    }

                    if (DebugParser)
                    {
                        ParserLog.AppendLine(string.Format("Parameter: {0}", currentArgument));
                    }

                    string param = currentArgument.StartsWith("\"") && currentArgument.EndsWith("\"")
                             ? currentArgument.Substring(1, currentArgument.Length - 2)
                             : currentArgument;

                    DispatchedOptions.Add(currentCommand);
                    DispatchOption(currentCommand, param);
                    currentCommand = null;

                    allArguments    = allArguments.Remove(paramSearchStartIndex, currentArgument.Length).TrimEnd();
                    currentArgument = string.Empty;
                    characterIndex  = oldCharacterIndex;
                    mode            = CommandlineParserState.GetCommand;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            if (currentArgument != string.Empty)
            {
                switch (mode)
                {
                case CommandlineParserState.SearchCommandStart:
                case CommandlineParserState.GetCommand:
                    if (DebugParser)
                    {
                        ParserLog.AppendLine(string.Format("Unkown Command: {0}", currentArgument));
                    }
                    UnknownArguments.Add(currentArgument);
                    break;

                case CommandlineParserState.GetParameter:
                    if (DebugParser)
                    {
                        ParserLog.AppendLine(string.Format("Parameter (on exit): {0}", currentArgument));
                    }
                    DispatchedOptions.Add(currentCommand);
                    DispatchOption(currentCommand, currentArgument);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
 /// <summary>
 /// Gets the short name of the option; used to display the help message.
 /// </summary>
 /// <returns>The string prepended with the first ArgumentPrefixList item.</returns>
 protected virtual string GetOptionShortName(CommandlineOption option)
 {
     return(option.RequiredPosition == int.MaxValue
        ? OptionPrefixes[0] + option.Name
        : option.Name);
 }
 /// <summary>
 /// Gets the commandline option short and long name.
 /// </summary>
 /// <param name="option">The commandline option to get the short and long names from.</param>
 /// <returns></returns>
 protected virtual string GetOptionNames(CommandlineOption option)
 {
     return(!string.IsNullOrEmpty(option.LongName)
        ? GetOptionShortName(option) + ", " + GetOptionLongName(option)
        : GetOptionShortName(option));
 }
 /// <summary>
 /// Dispatches the command.
 /// </summary>
 /// <param name="option">The commmandline argument.</param>
 /// <param name="parameter">The paramater.</param>
 /// <returns>null</returns>
 protected void DispatchOption(CommandlineOption option, string parameter)
 {
     option.Action(this, parameter.Trim());
 }
Esempio n. 6
0
        protected override void DoParse(string[] arguments)
        {
            SortOptionsByRequiredPostion();

            // First process all options that have required positions
            foreach (CommandlineOption currentOption in Options.Where(currentCommand => currentCommand.RequiredPosition != int.MaxValue && currentCommand.RequiredPosition < arguments.Length))
            {
                if (DebugParser)
                {
                    ParserLog.AppendLine(string.Format("Dispatching {0}; value {1}", currentOption.Name,
                                                       arguments[currentOption.RequiredPosition]));
                }
                string arg = arguments[currentOption.RequiredPosition];

                if (string.IsNullOrEmpty(arg) || IsOptionStart(arg, 0))
                {
                    continue;
                }

                // Dispatch option and mark argument handled (null)
                DispatchedOptions.Add(currentOption);
                DispatchOption(currentOption, arguments[currentOption.RequiredPosition]);
                arguments[currentOption.RequiredPosition] = null;
            }

            // Process the all other (not null) arguments
            foreach (string currentArgument in arguments.Where(currentArgument => !string.IsNullOrEmpty(currentArgument)))
            {
                // Option should start with an OptionPrefixes character
                if (!IsOptionStart(currentArgument, 0))
                {
                    if (DebugParser)
                    {
                        ParserLog.AppendLine(string.Format("{0}: No option start found", currentArgument));
                    }
                    UnknownArguments.Add(currentArgument);
                    continue;
                }

                // Split current argument in a command and a parameter string
                string parameter = string.Empty;
                string command   = currentArgument.Substring(1);

                int index = command.IndexOfAny(ArgumentValueSeperator);
                if (index > 0)
                {
                    parameter = command.Substring(index + 1);
                    command   = command.Substring(0, index);
                }

                if (DebugParser)
                {
                    ParserLog.AppendLine(string.Format("Option: {0}, Parameter: {1}", command, parameter));
                }

                // Find the option handling the current command string
                CommandlineOption currentOption = GetArgument(command, true, StringComparison.InvariantCultureIgnoreCase) ??
                                                  GetArgument(command, false, StringComparison.InvariantCultureIgnoreCase);

                if (currentOption != null)
                {
                    // If option found check if the parameter is required
                    if (currentOption.Flags.HasAny(CommandlineOptionFlags.HasParameter) && string.IsNullOrEmpty(parameter))
                    {
                        if (DebugParser)
                        {
                            ParserLog.AppendLine(string.Format("{0}: Missing required parameter.", currentOption.Name));
                        }
                        UnknownArguments.Add(string.Format("{0}: Missing required parameter.", command));
                    }
                    else
                    {
                        // Dispatch the option
                        if (DebugParser)
                        {
                            ParserLog.AppendLine(string.Format("Dispatching {0}; value {1}", currentOption.Name, parameter));
                        }
                        DispatchedOptions.Add(currentOption);
                        DispatchOption(currentOption, parameter);
                    }
                }
                else
                {
                    // Invalid option
                    if (DebugParser)
                    {
                        ParserLog.AppendLine(string.Format("Invalid argument: {0}", command));
                    }
                    UnknownArguments.Add(command);
                }
            }
        }
Esempio n. 7
0
 protected override string GetOptionLongName(CommandlineOption commandLineArgument)
 {
     return(commandLineArgument.RequiredPosition == int.MaxValue
        ? OptionPrefixes[0] + commandLineArgument.LongName
        : commandLineArgument.LongName);
 }