Exemplo n.º 1
0
        private void SetupAssignmentsFromShortOverrides(IReadOnlyDictionary <string, ITemplateParameter> paramNamesNeedingAssignment)
        {
            foreach (KeyValuePair <string, string> canonicalAndShort in _shortNameOverrides.Where(x => paramNamesNeedingAssignment.ContainsKey(x.Key)))
            {
                string canonical     = canonicalAndShort.Key;
                string shortOverride = canonicalAndShort.Value;

                if (shortOverride == string.Empty)
                {
                    // it was explicitly empty string in the host file. If it wasn't specified, it'll be null
                    // this means there should be no short version
                    continue;
                }

                if (CommandAliasAssigner.TryAssignAliasesForParameter((x) => _takenAliases.Contains(x), canonical, null, shortOverride, out IReadOnlyList <string> assignedAliases))
                {
                    string shortParam = assignedAliases.FirstOrDefault(x => x.StartsWith("-") && !x.StartsWith("--"));
                    if (!string.IsNullOrEmpty(shortParam))
                    {
                        _shortAssignments.Add(canonical, shortParam);
                        _takenAliases.Add(shortParam);
                    }
                }
                else
                {
                    _invalidParams.Add(canonical);
                }
            }
        }
Exemplo n.º 2
0
        private void SetupAssignmentsWithoutOverrides(IReadOnlyDictionary <string, ITemplateParameter> paramNamesNeedingAssignment)
        {
            foreach (ITemplateParameter parameterInfo in paramNamesNeedingAssignment.Values)
            {
                if (_longAssignments.ContainsKey(parameterInfo.Name) && _shortAssignments.ContainsKey(parameterInfo.Name))
                {
                    // already fully assigned
                    continue;
                }

                _longNameOverrides.TryGetValue(parameterInfo.Name, out string longOverride);
                _shortNameOverrides.TryGetValue(parameterInfo.Name, out string shortOverride);

                if (CommandAliasAssigner.TryAssignAliasesForParameter((x) => _takenAliases.Contains(x), parameterInfo.Name, longOverride, shortOverride, out IReadOnlyList <string> assignedAliases))
                {
                    if (shortOverride != string.Empty)
                    {
                        // explicit empty string in the host file means there should be no short name.
                        // but thats not the case here.
                        if (!_shortAssignments.ContainsKey(parameterInfo.Name))
                        {
                            // still needs a short version
                            string shortParam = assignedAliases.FirstOrDefault(x => x.StartsWith("-") && !x.StartsWith("--"));
                            if (!string.IsNullOrEmpty(shortParam))
                            {
                                _shortAssignments.Add(parameterInfo.Name, shortParam);
                                _takenAliases.Add(shortParam);
                            }
                        }
                    }

                    if (!_longAssignments.ContainsKey(parameterInfo.Name))
                    {
                        // still needs a long version
                        string longParam = assignedAliases.FirstOrDefault(x => x.StartsWith("--"));
                        if (!string.IsNullOrEmpty(longParam))
                        {
                            _longAssignments.Add(parameterInfo.Name, longParam);
                            _takenAliases.Add(longParam);
                        }
                    }
                }
                else
                {
                    _invalidParams.Add(parameterInfo.Name);
                }
            }
        }
Exemplo n.º 3
0
 private void SetupAssignmentsFromLongOverrides(IReadOnlyDictionary <string, ITemplateParameter> paramNamesNeedingAssignment)
 {
     foreach (KeyValuePair <string, string> canonicalAndLong in _longNameOverrides.Where(x => paramNamesNeedingAssignment.ContainsKey(x.Key)))
     {
         string canonical    = canonicalAndLong.Key;
         string longOverride = canonicalAndLong.Value;
         if (CommandAliasAssigner.TryAssignAliasesForParameter((x) => _takenAliases.Contains(x), canonical, longOverride, null, out IReadOnlyList <string> assignedAliases))
         {
             // only deal with the long here, ignore the short for now
             string longParam = assignedAliases.FirstOrDefault(x => x.StartsWith("--"));
             if (!string.IsNullOrEmpty(longParam))
             {
                 _longAssignments.Add(canonical, longParam);
                 _takenAliases.Add(longParam);
             }
         }
         else
         {
             _invalidParams.Add(canonical);
         }
     }
 }
        // Creates a command setup with the args for "new", plus args for the input template parameters.
        public static Command CreateNewCommandWithArgsForTemplate(string commandName, string templateName,
                                                                  IReadOnlyList <ITemplateParameter> parameterDefinitions,
                                                                  IDictionary <string, string> longNameOverrides,
                                                                  IDictionary <string, string> shortNameOverrides,
                                                                  out IReadOnlyDictionary <string, IReadOnlyList <string> > templateParamMap)
        {
            IList <Option> paramOptionList = new List <Option>();

            Option[]         allBuiltInArgs = ArrayExtensions.CombineArrays(NewCommandVisibleArgs, NewCommandHiddenArgs, NewCommandReservedArgs, DebuggingCommandArgs);
            HashSet <string> takenAliases   = VariantsForOptions(allBuiltInArgs);
            HashSet <string> invalidParams  = new HashSet <string>();
            Dictionary <string, IReadOnlyList <string> > canonicalToVariantMap = new Dictionary <string, IReadOnlyList <string> >();

            foreach (ITemplateParameter parameter in parameterDefinitions.Where(x => x.Priority != TemplateParameterPriority.Implicit))
            {
                string canonical = parameter.Name;
                longNameOverrides.TryGetValue(canonical, out string longOverride);
                shortNameOverrides.TryGetValue(canonical, out string shortOverride);

                if (CommandAliasAssigner.TryAssignAliasesForParameter((x) => takenAliases.Contains(x), canonical, longOverride, shortOverride, out IReadOnlyList <string> assignedAliases))
                {
                    Option option;

                    if (string.Equals(parameter.DataType, "choice", StringComparison.OrdinalIgnoreCase))
                    {
                        IList <string> choices = parameter.Choices.Keys.ToList();
                        option = Create.Option(string.Join("|", assignedAliases), parameter.Documentation,
                                               Accept.ExactlyOneArgument()
                                               //.WithSuggestionsFrom(parameter.Choices.Keys.ToArray())
                                               .With(defaultValue: () => parameter.DefaultValue));
                    }
                    else if (string.Equals(parameter.DataType, "bool", StringComparison.OrdinalIgnoreCase))
                    {
                        option = Create.Option(string.Join("|", assignedAliases), parameter.Documentation,
                                               Accept.ZeroOrOneArgument()
                                               .WithSuggestionsFrom(new[] { "true", "false" }));
                    }
                    else
                    {
                        option = Create.Option(string.Join("|", assignedAliases), parameter.Documentation,
                                               Accept.ExactlyOneArgument());
                    }

                    paramOptionList.Add(option);                                    // add the option
                    canonicalToVariantMap.Add(canonical, assignedAliases.ToList()); // map the template canonical name to its aliases.
                    takenAliases.UnionWith(assignedAliases);                        // add the aliases to the taken aliases
                }
                else
                {
                    invalidParams.Add(canonical);
                }
            }

            if (invalidParams.Count > 0)
            {
                string unusableDisplayList = string.Join(", ", invalidParams);
                throw new Exception($"Template is malformed. The following parameter names are invalid: {unusableDisplayList}");
            }

            templateParamMap = canonicalToVariantMap;
            return(GetNewCommandForTemplate(commandName, templateName, NewCommandVisibleArgs, NewCommandHiddenArgs, DebuggingCommandArgs, paramOptionList.ToArray()));
        }