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