private (bool Exists, List <string> Values) GetValues(UriString uri) { if (uri.Query.TryGetValue(CommandArgumentQueryStringKeys.Position, out var p)) { var position = int.Parse((string)p); var elementAtOrDefault = _commandLine.AnonymousValues().ElementAtOrDefault(position); var exists = !(elementAtOrDefault is null); return ( exists, exists ? new List <string> { elementAtOrDefault } : new List <string>() ); } else { var names = uri.Path.Decoded.ToString().Split('/'); var id = new Identifier(names.Select(SoftString.Create)); return (_commandLine.Contains(id) ? (true, _commandLine[id].ToList()) : (false, new List <string>())); } }
//[NotNull, ItemNotNull] //public static IEnumerable<string> ArgumentValues([NotNull] this ICommandLine commandLine, int? position, Identifier id) //{ // if (commandLine == null) throw new ArgumentNullException(nameof(commandLine)); // return // position.HasValue // ? commandLine.AnonymousValues().Skip(position.Value).Take(1) // : commandLine[id]; //} //[ContractAnnotation("values: notnull")] public static bool TryGetArgumentValues([NotNull] this ICommandLine commandLine, Identifier id, int?position, [CanBeNull, ItemNotNull] out IList <string> values) { if (commandLine == null) { throw new ArgumentNullException(nameof(commandLine)); } if (commandLine.Contains(id)) { values = commandLine[id].ToList(); return(true); } if (position.HasValue && position.Value <= commandLine.AnonymousValues().Count() - 1) { values = new[] { commandLine.AnonymousValues().ElementAtOrDefault(position.Value) }; return(true); } values = default; return(false); }
public static Identifier CommandId([NotNull] this ICommandLine commandLine) { if (commandLine == null) { throw new ArgumentNullException(nameof(commandLine)); } // Command-name is the first anonymous argument. return (commandLine .AnonymousValues() .FirstOrDefault() ?? throw DynamicException.Factory.CreateDynamicException( $"CommandNameNotFound{nameof(Exception)}", $"Command line '{commandLine}' does not contain a command name." )); }