public CommandLineArgsConfigurationSource(IEnumerable <ArgumentDefinition> argumentDefinitions, IEnumerable <string> args) { ArgumentDefinitions = argumentDefinitions.ToList(); if (ArgumentDefinition.HasDoubleDefinition(argumentDefinitions, out string firstDoubleDefinition)) { throw new System.ArgumentException($"The argument key '{firstDoubleDefinition}' has been defined twice.", nameof(argumentDefinitions)); } Args = args.ToList(); }
/// <summary> /// Creates a new instance of <see cref="CommandLineArgsConfigurationProvider"/> /// </summary> public CommandLineArgsConfigurationProvider(CommandLineArgsConfigurationSource source) { _source = source.Clone(); _indexedArgumentDefinitions = source.ArgumentDefinitions .Where(ArgumentDefinition.IsNamedForNamedArguments) .SelectMany(definition => ArgumentDefinition.GetAllNames(definition) .CatMaybes() .Select(name => (Key: name, Definition: definition))) .ToDictionary(p => p.Key, p => p.Definition, StringComparer.Ordinal); // index is case sensitive _valueDefinitions = source.ArgumentDefinitions .OfType <ArgumentDefinition.RequiredValueDefinition>() .Select(p => p.ConfigurationKey) .ToList(); }
/// <summary> /// Checks whether an instance of <see cref="ArgumentDefinition"/> is of a type /// (see <see cref="Type"/>), determining that <see cref="LongName"/> or <see cref="ShortName"/> /// is neither <c>null</c> nor empty. /// </summary> /// <param name="definition">The argument definition to check.</param> /// <returns> /// <c>true</c> if <paramref name="definition"/> is of a type that requires an argument name. /// </returns> public static bool IsNamedForNamedArguments(ArgumentDefinition definition) => definition is SwitchOptionDefinition || definition is ValueOptionDefinition;
/// <summary> /// Returns <see cref="LongName"/> and/or <see cref="ShortName"/> /// as sequence oft strings, depending on whether they are set. /// </summary> /// <param name="definition">The argument definition.</param> /// <returns>A sequence of strings containing all names set in the provided definition.</returns> public static IEnumerable <Maybe <string> > GetAllNames(ArgumentDefinition definition) { return(definition.ShortName.AsEnumerable().Concat(definition.LongName.AsEnumerable())); }