Example #1
0
        public bool ParseCommandLine(string[] fragments, out Command parsedCommand)
        {
            bool    success         = true;
            Command templateCommand = null;

            parsedCommand = null;

            // Determine which command we are parsing
            if (fragments.Length > 0)
            {
                if (_commandMap.TryGetValue(fragments[0], out templateCommand))
                {
                    parsedCommand = new Command(fragments[0], templateCommand.CommandDelegate);
                }
                else
                {
                    _logger.WriteLine("Parser: Unknown command '{0}' ", fragments[0]);
                    success = false;
                }
            }
            else
            {
                _logger.WriteLine("Parser: No Command Given");
                success = false;
            }

            // Parse all of the given arguments
            if (success && (fragments.Length % 2) == 1)
            {
                for (int fragment_index = 1; success && fragment_index < fragments.Length; fragment_index += 2)
                {
                    string argumentName        = fragments[fragment_index].Substring(1); // Skip the '-' in front of the argument name
                    string argumentStringValue = fragments[fragment_index + 1];

                    CommandArgument templateArgument = templateCommand.GetArgumentByName(argumentName);

                    if (templateArgument != null)
                    {
                        CommandArgument parsedArgument = templateArgument.Clone();

                        if (parsedArgument.Parse(argumentStringValue))
                        {
                            parsedCommand.AddArgument(parsedArgument);
                        }
                        else
                        {
                            _logger.WriteLine("Parser: Malformed argument value '{0}' for argument '{1}'",
                                              argumentStringValue, argumentName);
                            success = false;
                        }
                    }
                    else
                    {
                        _logger.WriteLine("Parser: Unknown argument '{0}'", argumentName);
                        success = false;
                    }
                }
            }

            // Make sure all required arguments were given
            if (success)
            {
                foreach (CommandArgument templateArgument in templateCommand.ArgumentMap.Values)
                {
                    if (!templateArgument.ArgumentOptional &&
                        !parsedCommand.HasArgumentWithName(templateArgument.ArgumentName))
                    {
                        _logger.WriteLine("Parser: Missing required argument '{0}'", templateArgument.ArgumentName);
                        success = false;
                        break;
                    }
                }
            }

            return(success);
        }
Example #2
0
 public void AddArgument(CommandArgument argument)
 {
     _argumentMap.Add(argument.ArgumentName, argument);
 }