doc.odin 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. package flags implements a command-line argument parser.
  3. It works by using Odin's run-time type information to determine where and how
  4. to store data on a struct provided by the user. Type conversion is handled
  5. automatically and errors are reported with useful messages.
  6. Command-Line Syntax:
  7. Arguments are treated differently on how they're formatted. The format is
  8. similar to the Odin binary's way of handling compiler flags.
  9. ```
  10. type handling
  11. ------------ ------------------------
  12. <positional> depends on struct layout
  13. -<flag> set a bool true
  14. -<flag:option> set flag to option
  15. -<flag=option> set flag to option, alternative syntax
  16. -<map>:<key>=<value> set map[key] to value
  17. ```
  18. Struct Tags:
  19. Users of the `encoding/json` package may be familiar with using tags to
  20. annotate struct metadata. The same technique is used here to annotate where
  21. arguments should go and which are required.
  22. Under the `args` tag:
  23. - `name=S`, alias a struct field to `S`
  24. - `pos=N`, place positional argument `N` into this field
  25. - `hidden`, hide this field from the usage documentation
  26. - `required`, cause verification to fail if this argument is not set
  27. There is also the `usage` tag, which is a plain string to be printed alongside
  28. the flag in the usage output.
  29. Supported Field Datatypes:
  30. - all `bool`s
  31. - all `int`s
  32. - all `float`s
  33. - `string`, `cstring`
  34. - `rune`
  35. - `dynamic` arrays with element types of the above
  36. - `map[string]`s with value types of the above
  37. Validation:
  38. The parser will ensure `required` arguments are set. This is on by default.
  39. Strict:
  40. The parser will return on the first error and stop parsing. This is on by
  41. default. Otherwise, all arguments that can be parsed, will be, and only the
  42. last error is returned.
  43. Help:
  44. By default, `-h` and `-help` are reserved flags which raise their own error
  45. type when set, allowing the program to handle the request differently from
  46. other errors.
  47. Example:
  48. ```odin
  49. Options :: struct {
  50. file: string `args:"pos=0,required" usage:"input file"`,
  51. out: string `args:"pos=1" usage:"output file"`,
  52. retry_count: uint `args:"name=retries" usage:"times to retry process"`,
  53. debug: bool `args:"hidden" usage:"print debug info"`,
  54. collection: map[string]string `usage:"path aliases"`,
  55. }
  56. opt: Options
  57. flags.parse(&opt, {
  58. "main.odin",
  59. "-retries:3",
  60. "-collection:core=./core",
  61. "-debug",
  62. }, validate_args = true, strict = true)
  63. ```
  64. */
  65. package flags