public OptionInfo(string text, int startOffset, int endOffset, IOptionDefinition optionDefinition) { myText = text; myStartOffset = startOffset; myEndoffset = endOffset; myOptionDefinition = optionDefinition; }
public static IList <KeyValuePair <IOptionDefinition, OptionValue> > TransformOptionValues( IEnumerable <KeyValuePair <IOptionDefinition, IList <string> > > rawOptionValues) { if (rawOptionValues == null) { return(Array.Empty <KeyValuePair <IOptionDefinition, OptionValue> >()); } return(rawOptionValues .Select( ov => { IOptionDefinition optionDefinition = ov.Key; ValueTransformer transformer = optionDefinition.Transformer; try { return new KeyValuePair <IOptionDefinition, OptionValue>( optionDefinition, new OptionValue(ov.Value, transformer.Transform(ov.Value))); } catch (ArgParsingException) { throw; } catch { throw new ArgParsingException( ArgsParsingErrorCode.TransformValueFailed, $"Option: {ov.Key.ToString()}; Values: {String.Join(" ", ov.Value)}."); } }) .ToArray()); }
private void AddOption(IOptionDefinition optionDefinition) { var value = optionDefinition.OptionStringValue; if (value != null) { var optionName = GetOptionNameWithoutQuotes(optionDefinition); string optionValueText = value.GetText(); int startOffset = value.GetTreeStartOffset().Offset; if ((optionValueText.Length > 0) && ("\"".Equals(optionValueText.Substring(0, 1)))) { optionValueText = optionValueText.Substring(1, optionValueText.Length - 1); startOffset++; } int endOffset = value.GetTreeEndOffset().Offset; if ((optionValueText.Length > 0) && ("\"".Equals(optionValueText.Substring(optionValueText.Length - 1, 1)))) { optionValueText = optionValueText.Substring(0, optionValueText.Length - 1); endOffset--; } if (optionValueText.Length > 0) { if (OptionDeclaredElements.NamespacesOptions.Contains(optionName)) { if (OptionDeclaredElements.ShortNamespacesOptions.Contains(optionName)) { myShortNamespaces.Add(new OptionInfo(optionValueText, startOffset, endOffset, optionDefinition)); } else { AddNamespaceOption(endOffset, startOffset, optionValueText); if (optionName == OptionDeclaredElements.ParserPackageNamespaceOption) { myPackage = optionValueText; int dotIndex = myPackage.LastIndexOf('.'); myPackage = myPackage.Substring(0, dotIndex); } } } else if (OptionDeclaredElements.ClassesOptions.Contains(optionName)) { if (OptionDeclaredElements.ClassesOptionsWithShortNamespace.Contains(optionName)) { myClassesWithShortNamespace.Add(new OptionInfo(optionValueText, startOffset, endOffset, optionDefinition)); } else { AddClassesOption(optionDefinition, endOffset, startOffset, optionValueText); } } else if (OptionDeclaredElements.MethodsOptions.Contains(optionName)) { AddMethodOption(optionDefinition, endOffset, startOffset, optionValueText); } } } }
public OptionToken(IOptionDefinition definition, string value) { if (definition.Type != OptionType.KeyValue) { throw new ArgumentException( $"This constructor is for key-value definition only. While current is {definition.Type}."); } Definition = definition; Value = value ?? throw new ArgumentNullException(nameof(value)); }
public OptionToken(IOptionDefinition definition) { if (definition.Type != OptionType.Flag) { throw new ArgumentException( $"This constructor is for flag definition only. While current is {definition.Type}"); } Definition = definition; Value = null; }
protected static IPreParsingState HandleKeyValueOptionArgument( ICommandDefinition command, PreParserResultBuilder resultBuilder, string argument) { IOptionDefinition kvOption = ResolveKeyValueOptionLabel( command, argument); return(kvOption != null ? new WaitingValueState(command, kvOption, argument, resultBuilder) : null); }
void ValidateRequiredKeyValues() { IOptionDefinition notPresentedRequiredOption = command.GetRegisteredOptions() .Where(o => o.Type == OptionType.KeyValue && o.IsRequired) .FirstOrDefault(o => !keyValues.ContainsKey(o)); if (notPresentedRequiredOption != null) { throw new ArgParsingException( ArgsParsingErrorCode.RequiredOptionNotPresent, notPresentedRequiredOption.Symbol.ToString()); } }
public void RegisterOption(IOptionDefinition option) { Debug.Assert(option != null); IOptionDefinition conflictOptionDefinition = options.FirstOrDefault(o => o.IsConflict(option)); if (conflictOptionDefinition != null) { throw new ArgumentException( $"The option definition '{option}' conflicts with definition '{conflictOptionDefinition}'"); } options.Add(option); }
private static string GetOptionNameWithoutQuotes(IOptionDefinition optionDefinition) { string optionName = optionDefinition.OptionName.GetText(); if ((optionName.Length > 0) && ("\"".Equals(optionName.Substring(0, 1)))) { optionName = optionName.Substring(1, optionName.Length - 1); } if ((optionName.Length > 0) && ("\"".Equals(optionName.Substring(optionName.Length - 1, 1)))) { optionName = optionName.Substring(0, optionName.Length - 1); } return(optionName); }
public WaitingValueState( ICommandDefinition command, IOptionDefinition kvOption, string labelArgument, PreParserResultBuilder resultBuilder) { Debug.Assert(command != null); Debug.Assert(kvOption != null); Debug.Assert(resultBuilder != null); this.command = command; this.kvOption = kvOption; this.labelArgument = labelArgument; this.resultBuilder = resultBuilder; }
private void AddMethodOption(IOptionDefinition optionDefinition, int endOffset, int startOffset, string optionValueText) { int dotIndex = optionValueText.LastIndexOf('.'); var className = optionValueText.Substring(0, dotIndex); var classes = optionDefinition.GetPsiServices().CacheManager.GetDeclarationsCache(optionDefinition.GetPsiModule(), false, true).GetTypeElementsByCLRName(className); if (classes.Count == 0) { string optionName = GetOptionNameWithoutQuotes(optionDefinition); if (OptionDeclaredElements.UnnecessaryOptionNames.Contains(optionName)) { return; } } var map = GeneratedRangeMapFactory.CreateGeneratedRangeMap(myFile); map.Add(new TreeTextRange <Generated>(new TreeOffset(8), new TreeOffset(optionValueText.Length + 8)), new TreeTextRange <Original>(new TreeOffset(startOffset), new TreeOffset(endOffset))); myGeneratedMethodBody.Append(new GenerationResults(CSharpLanguage.Instance, "var a = " + optionValueText + ";\n", map)); }
private void VisitOptionDefinition(IOptionDefinition optionDefinition) { string name = optionDefinition.OptionName.GetText(); int offset = optionDefinition.GetNavigationRange().TextRange.StartOffset; IPsiSourceFile psiSourceFile = optionDefinition.GetSourceFile(); string value = ""; IOptionStringValue valueNode = optionDefinition.OptionStringValue; if (valueNode != null) { value = valueNode.GetText(); if ("\"".Equals(value.Substring(0, 1))) { value = value.Substring(1, value.Length - 1); } if ("\"".Equals(value.Substring(value.Length - 1, 1))) { value = value.Substring(0, value.Length - 1); } } mySymbols.Add(new PsiOptionSymbol(name, offset, value, psiSourceFile)); }
private void AddClassesOption(IOptionDefinition optionDefinition, int endOffset, int startOffset, string optionValueText, int startInGenerated = 0) { var classes = optionDefinition.GetPsiServices().CacheManager.GetDeclarationsCache(optionDefinition.GetPsiModule(), false, true).GetTypeElementsByCLRName(optionValueText); if (classes.Count == 0) { string optionName = GetOptionNameWithoutQuotes(optionDefinition); if (OptionDeclaredElements.UnnecessaryOptionNames.Contains(optionName)) { return; } } foreach (var typeElement in classes) { var cls = typeElement as IClass; if (cls != null) { AddStaticClassOption(endOffset, startOffset, optionValueText, startInGenerated); return; } } AddClassOption(endOffset, startOffset, optionValueText, startInGenerated); }
public bool IsConflict(IOptionDefinition optionDefinition) { return(Symbol.IsConflict(optionDefinition.Symbol)); }
public bool Equals(IOptionDefinition other) { return(Id.Equals(other.Id)); }
protected CommandOption SetOption(CommandLineApplication command, IOptionDefinition optionDefinition) { return(command.Option(optionDefinition.Template, optionDefinition.Description, optionDefinition.OptionType)); }
private void AddMethodOption(IOptionDefinition optionDefinition, int endOffset, int startOffset, string optionValueText) { int dotIndex = optionValueText.LastIndexOf('.'); var className = optionValueText.Substring(0, dotIndex); var classes = optionDefinition.GetPsiServices().CacheManager.GetDeclarationsCache(optionDefinition.GetPsiModule(), false, true).GetTypeElementsByCLRName(className); if (classes.Count == 0) { string optionName = GetOptionNameWithoutQuotes(optionDefinition); if (OptionDeclaredElements.UnnecessaryOptionNames.Contains(optionName)) { return; } } var map = GeneratedRangeMapFactory.CreateGeneratedRangeMap(myFile); map.Add(new TreeTextRange<Generated>(new TreeOffset(8), new TreeOffset(optionValueText.Length + 8)), new TreeTextRange<Original>(new TreeOffset(startOffset), new TreeOffset(endOffset))); myGeneratedMethodBody.Append(new GenerationResults(CSharpLanguage.Instance, "var a = " + optionValueText + ";\n", map)); }
private void AddClassesOption(IOptionDefinition optionDefinition, int endOffset, int startOffset, string optionValueText, int startInGenerated = 0) { var classes = optionDefinition.GetPsiServices().CacheManager.GetDeclarationsCache(optionDefinition.GetPsiModule(), false, true).GetTypeElementsByCLRName(optionValueText); if(classes.Count == 0) { string optionName = GetOptionNameWithoutQuotes(optionDefinition); if(OptionDeclaredElements.UnnecessaryOptionNames.Contains(optionName)) { return; } } foreach (var typeElement in classes) { var cls = typeElement as IClass; if(cls != null){ AddStaticClassOption(endOffset, startOffset, optionValueText, startInGenerated); return; } } AddClassOption(endOffset, startOffset, optionValueText, startInGenerated); }
private static string GetOptionNameWithoutQuotes(IOptionDefinition optionDefinition) { string optionName = optionDefinition.OptionName.GetText(); if ((optionName.Length > 0) && ("\"".Equals(optionName.Substring(0, 1)))) { optionName = optionName.Substring(1, optionName.Length - 1); } if ((optionName.Length > 0) && ("\"".Equals(optionName.Substring(optionName.Length - 1, 1)))) { optionName = optionName.Substring(0, optionName.Length - 1); } return optionName; }