doc.odin 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 program. Type conversion is handled
  5. automatically and errors are reported with useful messages.
  6. Command-Line Syntax:
  7. Arguments are treated differently depending on how they're formatted.
  8. The format is similar to the Odin binary's way of handling compiler flags.
  9. type handling
  10. ------------ ------------------------
  11. <positional> depends on struct layout
  12. -<flag> set a bool true
  13. -<flag:option> set flag to option
  14. -<flag=option> set flag to option, alternative syntax
  15. -<map>:<key>=<value> set map[key] to value
  16. Struct Tags:
  17. Users of the `core:encoding/json` package may be familiar with using tags to
  18. annotate struct metadata. The same technique is used here to annotate where
  19. arguments should go and which are required.
  20. Under the `args` tag, there are the following subtags:
  21. - `name=S`: set `S` as the flag's name.
  22. - `pos=N`: place positional argument `N` into this flag.
  23. - `hidden`: hide this flag from the usage documentation.
  24. - `required`: cause verification to fail if this argument is not set.
  25. - `variadic`: take all remaining arguments when set, UNIX-style only.
  26. - `file`: for `os.Handle` types, file open mode.
  27. - `perms`: for `os.Handle` types, file open permissions.
  28. - `indistinct`: allow the setting of distinct types by their base type.
  29. `required` may be given a range specifier in the following formats:
  30. min
  31. <max
  32. min<max
  33. `max` is not inclusive in this range, as noted by the less-than `<` sign, so if
  34. you want to require 3 and only 3 arguments in a dynamic array, you would
  35. specify `required=3<4`.
  36. `variadic` may be given a number (`variadic=N`) above 1 to limit how many extra
  37. arguments it consumes.
  38. `file` determines the file open mode for an `os.Handle`.
  39. It accepts a string of flags that can be mixed together:
  40. - r: read
  41. - w: write
  42. - c: create, create the file if it doesn't exist
  43. - a: append, add any new writes to the end of the file
  44. - t: truncate, erase the file on open
  45. `perms` determines the file open permissions for an `os.Handle`.
  46. The permissions are represented by three numbers in octal format. The first
  47. number is the owner, the second is the group, and the third is other. Read is
  48. represented by 4, write by 2, and execute by 1.
  49. These numbers are added together to get combined permissions. For example, 644
  50. represents read/write for the owner, read for the group, and read for other.
  51. Note that this may only have effect on UNIX-like platforms. By default, `perms`
  52. is set to 444 when only reading and 644 when writing.
  53. `indistinct` tells the parser that it's okay to treat distinct types as their
  54. underlying base type. Normally, the parser will hand those types off to the
  55. custom type setter (more about that later) if one is available, if it doesn't
  56. know how to handle the type.
  57. Usage Tag:
  58. There is also the `usage` tag, which is a plain string to be printed alongside
  59. the flag in the usage output. If `usage` contains a newline, it will be
  60. properly aligned when printed.
  61. All surrounding whitespace is trimmed when formatting with multiple lines.
  62. Supported Flag Data Types:
  63. - all booleans
  64. - all integers
  65. - all floats
  66. - all enums
  67. - all complex numbers
  68. - all quaternions
  69. - all bit_sets
  70. - `string` and `cstring`
  71. - `rune`
  72. - `os.Handle`
  73. - `time.Time`
  74. - `datetime.DateTime`
  75. - `net.Host_Or_Endpoint`,
  76. - additional custom types, see Custom Types below
  77. - `dynamic` arrays with element types of the above
  78. - `map[string]`s or `map[cstring]`s with value types of the above
  79. Validation:
  80. The parser will ensure `required` arguments are set, if no errors occurred
  81. during parsing. This is on by default.
  82. Additionally, you may call `register_flag_checker` to set your own argument
  83. validation procedure that will be called after the default checker.
  84. Strict:
  85. The parser will return on the first error and stop parsing. This is on by
  86. default. Otherwise, all arguments that can be parsed, will be, and only the
  87. last error is returned.
  88. Error Messages:
  89. All error message strings are allocated using the context's `temp_allocator`,
  90. so if you need them to persist, make sure to clone the underlying `message`.
  91. Help:
  92. By default, `-h` and `-help` are reserved flags which raise their own error
  93. type when set, allowing the program to handle the request differently from
  94. other errors.
  95. Custom Types:
  96. You may specify your own type setter for program-specific structs and other
  97. named types. Call `register_type_setter` with an appropriate proc before
  98. calling any of the parsing procs.
  99. A compliant `Custom_Type_Setter` must return three values:
  100. - an error message if one occurred,
  101. - a boolean indicating if the proc handles the type, and
  102. - an `Allocator_Error` if any occurred.
  103. If the setter does not handle the type, simply return without setting any of
  104. the values.
  105. UNIX-style:
  106. This package also supports parsing arguments in a limited flavor of UNIX.
  107. Odin and UNIX style are mutually exclusive, and which one to be used is chosen
  108. at parse time.
  109. --flag
  110. --flag=argument
  111. --flag argument
  112. --flag argument repeating-argument
  113. `-flag` may also be substituted for `--flag`.
  114. Do note that map flags are not currently supported in this parsing style.
  115. For a complete example, see: [[ core/flags/example; https://github.com/odin-lang/Odin/blob/master/core/flags/example/example.odin ]].
  116. */
  117. package flags