コード例 #1
0
 public OptionInfo(string text, int startOffset, int endOffset, IOptionDefinition optionDefinition)
 {
     myText             = text;
     myStartOffset      = startOffset;
     myEndoffset        = endOffset;
     myOptionDefinition = optionDefinition;
 }
コード例 #2
0
        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());
        }
コード例 #3
0
        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);
                    }
                }
            }
        }
コード例 #4
0
ファイル: OptionToken.cs プロジェクト: AxeDotNet/Axe.Cli
        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));
        }
コード例 #5
0
ファイル: OptionToken.cs プロジェクト: AxeDotNet/Axe.Cli
        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;
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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());
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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;
        }
コード例 #11
0
        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));
        }
コード例 #12
0
 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));
 }
コード例 #13
0
        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));
        }
コード例 #14
0
        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);
        }
コード例 #15
0
 public OptionInfo(string text, int startOffset, int endOffset, IOptionDefinition optionDefinition)
 {
   myText = text;
   myStartOffset = startOffset;
   myEndoffset = endOffset;
   myOptionDefinition = optionDefinition;
 }
コード例 #16
0
 public bool IsConflict(IOptionDefinition optionDefinition)
 {
     return(Symbol.IsConflict(optionDefinition.Symbol));
 }
コード例 #17
0
 public bool Equals(IOptionDefinition other)
 {
     return(Id.Equals(other.Id));
 }
コード例 #18
0
 protected CommandOption SetOption(CommandLineApplication command, IOptionDefinition optionDefinition)
 {
     return(command.Option(optionDefinition.Template,
                           optionDefinition.Description,
                           optionDefinition.OptionType));
 }
コード例 #19
0
    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);
          }
        }
      }
    }
コード例 #20
0
    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));
    }
コード例 #21
0
    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);
    }
コード例 #22
0
 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;
 }