Exemplo n.º 1
0
        public IEnumerable <string> GetHelp(IContext targetContext)
        {
            var lines = new List <string>();

            var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext).ToList();

            var prefixLength = optionDefinitions.Select(x => x.ToString().Length).Max();

            foreach (var optionDefinition in optionDefinitions)
            {
                var prefix = optionDefinition.ToString();

                var stringBuilder = new StringBuilder();
                stringBuilder.Append(prefix);

                for (var i = prefix.Length; i < prefixLength; i++)
                {
                    stringBuilder.Append(" ");
                }

                stringBuilder.Append($" {optionDefinition.HelpText}");
                lines.Add(stringBuilder.ToString());
            }

            return(lines);
        }
Exemplo n.º 2
0
        public IEnumerable <string> GetHelp(IContext targetContext)
        {
            var lines = new List <string>();

            var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext).ToList();

            var prefixLength = optionDefinitions.Select(x => x.ToString().Length).Max();

            foreach (var optionDefinition in optionDefinitions)
            {
                var prefix = optionDefinition.ToString();

                for (var i = prefix.Length; i < prefixLength; i++)
                {
                    prefix += " ";
                }

                var line = string.Format("{0} {1}", prefix, optionDefinition.HelpText);

                lines.Add(line);
            }

            return(lines);
        }
Exemplo n.º 3
0
        public IValidationContext Parse(List <string> commandLineArguments, IContext targetContext)
        {
            var validationContext = new ValidationContext();

            var quoteSplitCharacters = targetContext.QuoteSplitCharacters.ToArray();

            targetContext.OriginalCommandLine = string.Join(" ", commandLineArguments);

            var isHelp = commandLineArguments.Any(commandLineArgument => commandLineArgument.IsHelp(quoteSplitCharacters));

            if (isHelp)
            {
                targetContext.IsHelp = true;
                return(validationContext);
            }

            var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext);

            var handledOptions = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            Log.Debug("Parsing command line");

            for (var i = 0; i < commandLineArguments.Count; i++)
            {
                var commandLineArgument = commandLineArguments[i];

                try
                {
                    // Allow the first one to be a non-switch
                    if (i == 0 && !commandLineArguments[i].IsSwitch(quoteSplitCharacters))
                    {
                        var emptyOptionDefinition = (from x in optionDefinitions
                                                     where !x.HasSwitch()
                                                     select x).FirstOrDefault();

                        if (emptyOptionDefinition == null)
                        {
                            var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoEmptySwitch"), commandLineArgument);
                            Log.Debug(message);
                            validationContext.Add(BusinessRuleValidationResult.CreateError(message));
                            continue;
                        }

                        UpdateContext(targetContext, emptyOptionDefinition, commandLineArgument);
                        handledOptions.Add(emptyOptionDefinition.ShortName);
                        continue;
                    }

                    if (!commandLineArgument.IsSwitch(quoteSplitCharacters))
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoSwitch"), commandLineArgument);
                        Log.Debug(message);
                        validationContext.Add(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var value = string.Empty;

                    var optionDefinition = (from x in optionDefinitions
                                            where x.IsSwitch(commandLineArgument, quoteSplitCharacters)
                                            select x).FirstOrDefault();
                    var isKnownDefinition = (optionDefinition != null);
                    if (!isKnownDefinition)
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseSwitchNotRecognized"), commandLineArgument);
                        Log.Debug(message);
                        validationContext.Add(BusinessRuleValidationResult.CreateWarning(message));

                        // Try to read the next value, but keep in mind that some options might
                        // not have a value passed into it
                        var potentialValue = (i < commandLineArguments.Count - 1) ? commandLineArguments[i + 1] : string.Empty;
                        if (!string.IsNullOrWhiteSpace(potentialValue) && potentialValue.IsSwitch(quoteSplitCharacters))
                        {
                            potentialValue = string.Empty;
                        }

                        value = potentialValue;
                    }

                    targetContext.RawValues[commandLineArgument.TrimSwitchPrefix()] = value;

                    if (!isKnownDefinition)
                    {
                        continue;
                    }

                    if (!optionDefinition.AcceptsValue)
                    {
                        // Assume boolean switch
                        value = "true";
                    }
                    else
                    {
                        if (commandLineArguments.Count <= i + 1)
                        {
                            var message = string.Format(_languageService.GetString("CommandLine_CannotParseValueMissing"), commandLineArgument);
                            Log.Info(message);
                            validationContext.Add(BusinessRuleValidationResult.CreateWarning(message));
                            continue;
                        }

                        value = commandLineArguments[++i];
                    }

                    UpdateContext(targetContext, optionDefinition, value);
                    handledOptions.Add(optionDefinition.ShortName);
                }
                catch (Exception ex)
                {
                    validationContext.Add(BusinessRuleValidationResult.CreateError(_languageService.GetString("CommandLine_CannotParseExceptionOccurred"), commandLineArgument, ex.Message));
                }
            }

            ValidateMandatorySwitches(validationContext, optionDefinitions, handledOptions);

            Log.Debug("Finishing the context");

            targetContext.Finish();

            return(validationContext);
        }
Exemplo n.º 4
0
        public IValidationContext Parse(List <string> commandLineArguments, IContext targetContext)
        {
            var validationContext = new ValidationContext();

            targetContext.OriginalCommandLine = string.Join(" ", commandLineArguments);

            var isHelp = commandLineArguments.Any(commandLineArgument => commandLineArgument.IsHelp());

            if (isHelp)
            {
                targetContext.IsHelp = true;
                return(validationContext);
            }

            var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext);

            var handledOptions = new HashSet <char>();

            Log.Debug("Parsing command line");

            for (var i = 0; i < commandLineArguments.Count; i++)
            {
                var commandLineArgument = commandLineArguments[i];

                try
                {
                    // Allow the first one to be a non-switch
                    if (i == 0)
                    {
                        if (!commandLineArguments[i].IsSwitch())
                        {
                            var emptyOptionDefinition = (from x in optionDefinitions
                                                         where !x.HasSwitch()
                                                         select x).FirstOrDefault();

                            if (emptyOptionDefinition == null)
                            {
                                var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoEmptySwitch"), commandLineArgument);
                                Log.Error(message);
                                validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(message));
                                continue;
                            }

                            UpdateContext(targetContext, emptyOptionDefinition, commandLineArgument);
                            handledOptions.Add(emptyOptionDefinition.ShortName);
                            continue;
                        }
                    }

                    if (!commandLineArgument.IsSwitch())
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoSwitch"), commandLineArgument);
                        Log.Warning(message);
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var optionDefinition = (from x in optionDefinitions
                                            where x.IsSwitch(commandLineArgument)
                                            select x).FirstOrDefault();
                    if (optionDefinition == null)
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseSwitchNotRecognized"), commandLineArgument);
                        Log.Warning(message);
                        validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var value = string.Empty;
                    if (!optionDefinition.AcceptsValue)
                    {
                        // Assume boolean switch
                        value = "true";
                    }
                    else
                    {
                        if (commandLineArguments.Count <= i + 1)
                        {
                            var message = string.Format(_languageService.GetString("CommandLine_CannotParseValueMissing"), commandLineArgument);
                            Log.Warning(message);
                            validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message));
                            continue;
                        }

                        value = commandLineArguments[++i];
                    }

                    UpdateContext(targetContext, optionDefinition, value);
                    handledOptions.Add(optionDefinition.ShortName);
                }
                catch (Exception ex)
                {
                    validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(_languageService.GetString("CommandLine_CannotParseExceptionOccurred"), commandLineArgument, ex.Message));
                }
            }

            Log.Debug("Checking if all required options are specified");

            foreach (var optionDefinition in optionDefinitions)
            {
                if (optionDefinition.IsMandatory)
                {
                    if (!handledOptions.Contains(optionDefinition.ShortName))
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_RequiredSwitchNotSpecified"), optionDefinition);
                        Log.Error(message);
                        validationContext.AddFieldValidationResult(FieldValidationResult.CreateError(optionDefinition.GetSwitchDisplay(), message));
                    }
                }
            }

            Log.Debug("Finishing the context");

            targetContext.Finish();

            return(validationContext);
        }