doc.odin 5.9 KB

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