You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

namedarguments.md 2.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ---
  2. uid: Guides.Commands.NamedArguments
  3. title: Named Arguments
  4. ---
  5. # Named Arguments
  6. By default, arguments for commands are parsed positionally, meaning
  7. that the order matters. But sometimes you may want to define a command
  8. with many optional parameters, and it'd be easier for end-users
  9. to only specify what they want to set, instead of needing them
  10. to specify everything by hand.
  11. ## Setting up Named Arguments
  12. In order to be able to specify different arguments by name, you have
  13. to create a new class that contains all of the optional values that
  14. the command will use, and apply an instance of
  15. [NamedArgumentTypeAttribute] on it.
  16. ### Example - Creating a Named Arguments Type
  17. ```cs
  18. [NamedArgumentType]
  19. public class NamableArguments
  20. {
  21. public string First { get; set; }
  22. public string Second { get; set; }
  23. public string Third { get; set; }
  24. public string Fourth { get; set; }
  25. }
  26. ```
  27. ## Usage in a Command
  28. The command where you want to use these values can be declared like so:
  29. ```cs
  30. [Command("act")]
  31. public async Task Act(int requiredArg, NamableArguments namedArgs)
  32. ```
  33. The command can now be invoked as
  34. `.act 42 first: Hello fourth: "A string with spaces must be wrapped in quotes" second: World`.
  35. A TypeReader for the named arguments container type is
  36. automatically registered.
  37. It's important that any other arguments that would be required
  38. are placed before the container type.
  39. > [!IMPORTANT]
  40. > A single command can have only __one__ parameter of a
  41. > type annotated with [NamedArgumentTypeAttribute], and it
  42. > **MUST** be the last parameter in the list.
  43. > A command parameter of such an annotated type
  44. > is automatically treated as if that parameter
  45. > has [RemainderAttribute](xref:Discord.Commands.RemainderAttribute)
  46. > applied.
  47. ## Complex Types
  48. The TypeReader for Named Argument Types will look for a TypeReader
  49. of every property type, meaning any other command parameter type
  50. will work just the same.
  51. You can also read multiple values into a single property
  52. by making that property an `IEnumerable<T>`. So for example, if your
  53. Named Argument Type has the following field,
  54. ```cs
  55. public IEnumerable<int> Numbers { get; set; }
  56. ```
  57. then the command can be invoked as
  58. `.cmd numbers: "1, 2, 4, 8, 16, 32"`
  59. ## Additional Notes
  60. The use of [`[OverrideTypeReader]`](xref:Discord.Commands.OverrideTypeReaderAttribute)
  61. is also supported on the properties of a Named Argument Type.
  62. [NamedArgumentTypeAttribute]: xref:Discord.Commands.NamedArgumentTypeAttribute