Beispiel #1
0
        private void BindOptions(
            ICommand instance,
            IReadOnlyList <CommandOptionInput> optionInputs,
            IReadOnlyDictionary <string, string> environmentVariables)
        {
            // All inputs must be bound
            var remainingOptionInputs = optionInputs.ToList();

            // All required options must be set
            var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();

            // Environment variables
            foreach (var(name, value) in environmentVariables)
            {
                var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(name));
                if (option == null)
                {
                    continue;
                }

                var values = option.IsScalar
                    ? new[] { value }
                    : value.Split(Path.PathSeparator);

                option.BindOn(instance, values);
                unsetRequiredOptions.Remove(option);
            }

            // Direct input
            foreach (var option in Options)
            {
                var inputs = optionInputs
                             .Where(i => option.MatchesNameOrShortName(i.Alias))
                             .ToArray();

                // Skip if the inputs weren't provided for this option
                if (!inputs.Any())
                {
                    continue;
                }

                var inputValues = inputs.SelectMany(i => i.Values).ToArray();
                option.BindOn(instance, inputValues);

                remainingOptionInputs.RemoveRange(inputs);

                // Required option implies that the value has to be set and also be non-empty
                if (inputValues.Any())
                {
                    unsetRequiredOptions.Remove(option);
                }
            }

            // Ensure all inputs were bound
            if (remainingOptionInputs.Any())
            {
                throw CliFxException.UnrecognizedOptionsProvided(remainingOptionInputs);
            }

            // Ensure all required options were set
            if (unsetRequiredOptions.Any())
            {
                throw CliFxException.RequiredOptionsNotSet(unsetRequiredOptions);
            }
        }
Beispiel #2
0
        private void InjectOptions(
            ICommand command,
            IReadOnlyList <CommandOptionInput> optionInputs,
            IReadOnlyDictionary <string, string> environmentVariables)
        {
            // All inputs must be bound
            var remainingOptionInputs = optionInputs.ToList();

            // All required options must be set
            var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();

            // Environment variables
            foreach (var(name, value) in environmentVariables)
            {
                var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(name));

                if (option != null)
                {
                    var values = option.IsScalar
                        ? new[] { value }
                        : value.Split(Path.PathSeparator);

                    option.Inject(command, values);
                    unsetRequiredOptions.Remove(option);
                }
            }

            // TODO: refactor this part? I wrote this while sick
            // Direct input
            foreach (var option in Options)
            {
                var inputs = optionInputs
                             .Where(i => option.MatchesNameOrShortName(i.Alias))
                             .ToArray();

                if (inputs.Any())
                {
                    var inputValues = inputs.SelectMany(i => i.Values).ToArray();
                    option.Inject(command, inputValues);

                    foreach (var input in inputs)
                    {
                        remainingOptionInputs.Remove(input);
                    }

                    if (inputValues.Any())
                    {
                        unsetRequiredOptions.Remove(option);
                    }
                }
            }

            // Ensure all required options were set
            if (unsetRequiredOptions.Any())
            {
                throw CliFxException.RequiredOptionsNotSet(unsetRequiredOptions);
            }

            // Ensure all inputs were bound
            if (remainingOptionInputs.Any())
            {
                throw CliFxException.UnrecognizedOptionsProvided(remainingOptionInputs);
            }
        }