Beispiel #1
0
        /// <summary>
        /// Fills out this argument with the remainder of the provided command
        /// line.
        /// </summary>
        /// <param name="restOfLine">Remainder of the command-line tokens.</param>
        /// <returns>true on success; false otherwise.</returns>
        public bool TrySetRestOfLine(IEnumerable <string> restOfLine)
        {
            Debug.Assert(restOfLine != null);
            Debug.Assert(!SeenValue);

            SeenValue = true;

            if (Argument.IsCollection)
            {
                foreach (var arg in restOfLine)
                {
                    CollectionValues.Add(arg);
                }
            }
            else if (IsObjectPresent(DestinationObject))
            {
                var restOfLineAsList = restOfLine.ToList();
                if (!TrySetValue(DestinationObject,
                                 CreateCommandLine(restOfLineAsList),
                                 string.Join(" ", restOfLineAsList)))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Parses the provided value string using this object's value type.
        /// </summary>
        /// <param name="setParseState">Current parse state for containing arg set.</param>
        /// <param name="value">The string to parse.</param>
        /// <param name="parsedValue">On success, receives the parsed value.
        /// </param>
        /// <returns>True on success; false otherwise.</returns>
        public bool TryParseAndStore(ArgumentSetParser setParseState, string value, out object parsedValue)
        {
            // Check for disallowed duplicate arguments.
            if (SeenValue && !Argument.AllowMultiple)
            {
                ReportDuplicateArgumentValue(value);

                parsedValue = null;
                return(false);
            }

            // Check for conflicting arguments that have already been specified.
            bool conflictsDetected = false;

            foreach (var arg in Argument.ConflictingArgs.Where(setParseState.HasSeenValueFor))
            {
                ReportConflictingArgument(value, arg);
                conflictsDetected = true;
            }

            if (conflictsDetected)
            {
                parsedValue = null;
                return(false);
            }

            // Note that we've now seen a value for this argument so we can
            // catch disallowed duplicates later.
            SeenValue = true;

            // Parse the string version of the value.
            if (!ParseValue(value, out object newValue))
            {
                parsedValue = null;
                return(false);
            }

            if (!TryValidateValue(newValue, new ArgumentValidationContext(ParseContext.FileSystemReader)))
            {
                parsedValue = null;
                return(false);
            }

            if (Argument.IsCollection)
            {
                var newValues = Argument.CollectionArgumentType.ToEnumerable(newValue).Cast <object>();

                // Check for disallowed duplicate values in this argument. Note that the
                // duplication could either be between existing values and the new values,
                // or within the new value collection itself.
                if (Argument.Unique)
                {
                    var allValues = newValues.Concat(CollectionValues.Cast <object>()).ToList();

                    // TODO: Allow providing alternate notion of equality for uniqueness.
                    if (allValues.Count != allValues.Distinct().Count())
                    {
                        ReportDuplicateArgumentValue(value);

                        parsedValue = null;
                        return(false);
                    }
                }

                // Add the value to the collection.
                foreach (var newValueItem in newValues)
                {
                    CollectionValues.Add(newValueItem);
                }
            }
            else if (IsObjectPresent(DestinationObject))
            {
                if (!TrySetValue(DestinationObject, newValue, value))
                {
                    parsedValue = null;
                    return(false);
                }
            }

            parsedValue = newValue;
            return(true);
        }