Exemple #1
0
        /// <summary>
        /// Generates a set of valid strings--parseable to this type--that
        /// contain the provided string as a strict prefix.
        /// </summary>
        /// <param name="context">Context for parsing.</param>
        /// <param name="valueToComplete">The string to complete.</param>
        /// <returns>An enumeration of a set of completion strings; if no such
        /// strings could be generated, or if the type doesn't support
        /// completion, then an empty enumeration is returned.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/>
        /// or <paramref name="valueToComplete"/> is null.</exception>
        public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (valueToComplete == null)
            {
                throw new ArgumentNullException(nameof(valueToComplete));
            }

            string[] tokens;
            if (context.ParseContext.ElementSeparators.Any())
            {
                tokens = valueToComplete.Split(
                    context.ParseContext.ElementSeparators.ToArray(),
                    StringSplitOptions.None);
            }
            else
            {
                tokens = new[] { valueToComplete };
            }

            Debug.Assert(tokens.Length >= 1);

            var currentTokenIndex = tokens.Length - 1;
            var currentToken      = tokens[currentTokenIndex];

            tokens[currentTokenIndex] = string.Empty;

            var preferredSeparator = GetPreferredElementSeparatorOrDefault(context.ParseContext) ?? string.Empty;

            return(_elementArgumentType.GetCompletions(context, currentToken)
                   .Select(completion => string.Join(preferredSeparator, tokens) + completion));
        }
        public void GetCompletions()
        {
            var type = (TupleArgumentType)ArgumentType.GetType(typeof(Tuple<bool, int, bool>));
            var c = new ArgumentCompletionContext { ParseContext = ArgumentParseContext.Default };

            type.GetCompletions(c, "Tr").Should().ContainInOrder("True");
            type.GetCompletions(c, string.Empty).Should().ContainInOrder("False", "True");
            type.GetCompletions(c, "False,3").Should().BeEmpty();
            type.GetCompletions(c, "False,3,").Should().ContainInOrder("False,3,False", "False,3,True");
        }
        public void InvalidUseOfGetCompletions()
        {
            var type = (TupleArgumentType)ArgumentType.GetType(typeof(Tuple<bool, bool>));
            var c = new ArgumentCompletionContext { ParseContext = ArgumentParseContext.Default };

            Action badContext = () => type.GetCompletions(null, "Tr");
            badContext.ShouldThrow<ArgumentNullException>();

            Action badValue = () => type.GetCompletions(c, null);
            badValue.ShouldThrow<ArgumentNullException>();
        }
        public void GetCompletions()
        {
            var type = (KeyValuePairArgumentType)ArgumentType.GetType(typeof(KeyValuePair<bool, bool>));
            var c = new ArgumentCompletionContext { ParseContext = ArgumentParseContext.Default };

            type.GetCompletions(c, "Tr").Should().ContainInOrder("True");
            type.GetCompletions(c, string.Empty).Should().ContainInOrder("False", "True");
            type.GetCompletions(c, "False=f").Should().ContainInOrder("False=False");
            type.GetCompletions(c, "33=f").Should().ContainInOrder("33=False");
            type.GetCompletions(c, "True=").Should().ContainInOrder("True=False", "True=True");
        }
        public void GetCompletions()
        {
            var type = (ArrayArgumentType)ArgumentType.GetType(typeof(bool[]));
            var c = new ArgumentCompletionContext { ParseContext = ArgumentParseContext.Default };

            type.GetCompletions(c, "Tr").Should().ContainInOrder("True");
            type.GetCompletions(c, string.Empty).Should().ContainInOrder("False", "True");
            type.GetCompletions(c, "False,f").Should().ContainInOrder("False,False");
            type.GetCompletions(c, "33,f").Should().ContainInOrder("33,False");
            type.GetCompletions(c, "True,False,").Should().ContainInOrder("True,False,False", "True,False,True");
        }
Exemple #6
0
        /// <summary>
        /// Get possible completions of the provided path prefix string.
        /// </summary>
        /// <param name="context">Context for completion.</param>
        /// <param name="pathPrefix">Path prefix to complete.</param>
        /// <returns>Possible completions.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/>
        /// is null.</exception>
        public static IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string pathPrefix)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // If the string to be completed is empty, then treat it as if it
            // were a reference to the current working directory (with the
            // intention of completing to names of files in it).
            if (string.IsNullOrEmpty(pathPrefix))
            {
                pathPrefix = "." + System.IO.Path.DirectorySeparatorChar;
            }

            try
            {
                // If the string to be completed has no valid directory part,
                // then use its root as the directory name.  If there's no
                // root either, then assume the current working directory
                // is what we should use.
                var directoryPath = System.IO.Path.GetDirectoryName(pathPrefix);
                if (string.IsNullOrEmpty(directoryPath))
                {
                    var rootPath = System.IO.Path.GetPathRoot(pathPrefix);
                    directoryPath = !string.IsNullOrEmpty(rootPath) ? rootPath : ".";
                }

                // Construct a glob-style file pattern for matching.
                var filePattern = System.IO.Path.GetFileName(pathPrefix) + "*";

                // Enumerate all matching files in the selected directory.
                return(context.ParseContext.FileSystemReader.EnumerateFileSystemEntries(directoryPath, filePattern));
            }
            catch (Exception ex) when(
                ex is IOException ||
                ex is ArgumentException ||
                ex is NotSupportedException ||
                ex is SecurityException ||
                ex is UnauthorizedAccessException)
            {
                return(new List <string>());
            }
        }
        /// <summary>
        /// Generates a set of valid strings--parseable to this type--that
        /// contain the provided string as a strict prefix.
        /// </summary>
        /// <param name="context">Context for parsing.</param>
        /// <param name="valueToComplete">The string to complete.</param>
        /// <returns>An enumeration of a set of completion strings; if no such
        /// strings could be generated, or if the type doesn't support
        /// completion, then an empty enumeration is returned.</returns>
        public override IEnumerable<string> GetCompletions(ArgumentCompletionContext context, string valueToComplete)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (valueToComplete == null)
            {
                throw new ArgumentNullException(nameof(valueToComplete));
            }

            var separatorIndex = valueToComplete.IndexOf(KeyValueSeparatorChar);
            if (separatorIndex < 0)
            {
                return _keyType.GetCompletions(context, valueToComplete);
            }

            var valueSoFar = valueToComplete.Substring(separatorIndex + 1);
            return _valueType.GetCompletions(context, valueSoFar)
                       .Select(completion => valueToComplete.Substring(0, separatorIndex + 1) + completion);
        }
        /// <summary>
        /// Generates a set of valid strings--parseable to this type--that
        /// contain the provided string as a strict prefix.
        /// </summary>
        /// <param name="context">Context for parsing.</param>
        /// <param name="valueToComplete">The string to complete.</param>
        /// <returns>An enumeration of a set of completion strings; if no such
        /// strings could be generated, or if the type doesn't support
        /// completion, then an empty enumeration is returned.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/>
        /// or <paramref name="valueToComplete"/> is null.</exception>
        public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (valueToComplete == null)
            {
                throw new ArgumentNullException(nameof(valueToComplete));
            }

            var tokens = valueToComplete.Split(ItemSeparatorChar);

            Debug.Assert(tokens.Length >= 1);

            var currentTokenIndex = tokens.Length - 1;
            var currentToken      = tokens[currentTokenIndex];

            tokens[currentTokenIndex] = string.Empty;

            return(_argTypeParameters[currentTokenIndex].GetCompletions(context, currentToken)
                   .Select(completion => string.Join(ItemSeparatorChar.ToString(), tokens) + completion));
        }
Exemple #9
0
        /// <summary>
        /// Generates a set of valid strings--parseable to this type--that
        /// contain the provided string as a strict prefix.
        /// </summary>
        /// <param name="context">Context for parsing.</param>
        /// <param name="valueToComplete">The string to complete.</param>
        /// <returns>An enumeration of a set of completion strings; if no such
        /// strings could be generated, or if the type doesn't support
        /// completion, then an empty enumeration is returned.</returns>
        public override IEnumerable<string> GetCompletions(ArgumentCompletionContext context, string valueToComplete)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (valueToComplete == null)
            {
                throw new ArgumentNullException(nameof(valueToComplete));
            }

            var tokens = valueToComplete.Split(ItemSeparatorChar);
            Debug.Assert(tokens.Length >= 1);

            var currentTokenIndex = tokens.Length - 1;
            var currentToken = tokens[currentTokenIndex];

            tokens[currentTokenIndex] = string.Empty;

            return _argTypeParameters[currentTokenIndex].GetCompletions(context, currentToken)
                       .Select(completion => string.Join(ItemSeparatorChar.ToString(), tokens) + completion);
        }
Exemple #10
0
        /// <summary>
        /// Generates a set of valid strings--parseable to this type--that
        /// contain the provided string as a strict prefix.
        /// </summary>
        /// <param name="context">Context for parsing.</param>
        /// <param name="valueToComplete">The string to complete.</param>
        /// <returns>An enumeration of a set of completion strings; if no such
        /// strings could be generated, or if the type doesn't support
        /// completion, then an empty enumeration is returned.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/>
        /// or <paramref name="valueToComplete"/> is null.</exception>
        public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (valueToComplete == null)
            {
                throw new ArgumentNullException(nameof(valueToComplete));
            }

            var separatorIndex = valueToComplete.IndexOf(KeyValueSeparatorChar);

            if (separatorIndex < 0)
            {
                return(_keyType.GetCompletions(context, valueToComplete));
            }

            var valueSoFar = valueToComplete.Substring(separatorIndex + 1);

            return(_valueType.GetCompletions(context, valueSoFar)
                   .Select(completion => valueToComplete.Substring(0, separatorIndex + 1) + completion));
        }
 protected static StringComparison GetStringComparison(ArgumentCompletionContext context) =>
 context.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
 /// <summary>
 /// Filters the provided candidate string completion list based on the
 /// provided string to complete.
 /// </summary>
 /// <param name="context">Context for completion.</param>
 /// <param name="valueToComplete">String to complete.</param>
 /// <param name="candidates">Candidate objects whose formatted strings
 /// should be selected from.</param>
 /// <returns>An enumeration of the selected strings.</returns>
 protected static IEnumerable <string> SelectCompletions(ArgumentCompletionContext context, string valueToComplete, IEnumerable <object> candidates) =>
 SelectCompletions(context, valueToComplete, candidates.Select(candidate => candidate.ToString()));
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public virtual IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
 // By default, return an empty enumeration of strings.
 Enumerable.Empty <string>();
 protected static IEnumerable <string> SelectCompletions(ArgumentCompletionContext context, string valueToComplete, IEnumerable <string> candidates) =>
 candidates.Where(name => name.StartsWith(valueToComplete, GetStringComparison(context)))
 .OrderBy(name => name, GetStringComparer(context));
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public virtual IEnumerable<string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
     (Completer ?? InnerType).GetCompletions(context, valueToComplete);
Exemple #16
0
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public override IEnumerable<string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
     SelectCompletions(context, valueToComplete, Type.GetEnumNames());
Exemple #17
0
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public virtual IEnumerable<string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
     // By default, return an empty enumeration of strings.
     Enumerable.Empty<string>();
Exemple #18
0
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
 SelectCompletions(context, valueToComplete, new object[] { false, true });
Exemple #19
0
 public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
 _commandArgType.GetCompletions(context, valueToComplete);
 public IEnumerable<string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
     new[] { "|a|", "|ABC|" };
Exemple #21
0
 public IEnumerable<string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
     new[] { "xyzzy", "fizzy" };
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
 // Only complete to long names.
 SelectCompletions(context, valueToComplete,
                   _values.Where(v => !v.Hidden && !v.Disallowed)
                   .Select(v => v.LongName));
Exemple #23
0
 /// <summary>
 /// Filters the provided candidate string completion list based on the
 /// provided string to complete.
 /// </summary>
 /// <param name="context">Context for completion.</param>
 /// <param name="valueToComplete">String to complete.</param>
 /// <param name="candidates">Candidate objects whose formatted strings
 /// should be selected from.</param>
 /// <returns>An enumeration of the selected strings.</returns>
 protected IEnumerable<string> SelectCompletions(ArgumentCompletionContext context, string valueToComplete, IEnumerable<object> candidates) =>
     SelectCompletions(context, valueToComplete, candidates.Select(candidate => candidate.ToString()));
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public override IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete)
 {
     return(_completionHandler != null
         ? _completionHandler(context, valueToComplete)
         : base.GetCompletions(context, valueToComplete));
 }
 /// <summary>
 /// Generates a set of valid strings--parseable to this type--that
 /// contain the provided string as a strict prefix.
 /// </summary>
 /// <param name="context">Context for parsing.</param>
 /// <param name="valueToComplete">The string to complete.</param>
 /// <returns>An enumeration of a set of completion strings; if no such
 /// strings could be generated, or if the type doesn't support
 /// completion, then an empty enumeration is returned.</returns>
 public virtual IEnumerable <string> GetCompletions(ArgumentCompletionContext context, string valueToComplete) =>
 (Completer ?? InnerType).GetCompletions(context, valueToComplete);
Exemple #26
0
 /// <summary>
 /// Filters the provided candidate string completion list based on the
 /// provided string to complete.
 /// </summary>
 /// <param name="context">Context for completion.</param>
 /// <param name="valueToComplete">String to complete.</param>
 /// <param name="candidates">Candidate strings to select from.</param>
 /// <returns>An enumeration of the selected strings.</returns>
 protected static IEnumerable<string> SelectCompletions(ArgumentCompletionContext context, string valueToComplete, IEnumerable<string> candidates) =>
     candidates.Where(name => name.StartsWith(valueToComplete, StringComparison.OrdinalIgnoreCase))
               .OrderBy(name => name, StringComparer.OrdinalIgnoreCase);