core/flags

flags

Types

11

Custom_Flag_Checker

Custom_Flag_Checker :: proc(model: rawptr, name: string, value: any, args_tag: string) -> (error: string)Source

Check a flag after parsing, during the validation stage.

  • model: A raw pointer to the data structure provided to parse.
  • name: The name of the flag being checked.
  • value: An any type that contains the value to be checked.
  • args_tag: The args tag from within the struct.
  • error: An error message, or an empty string if no error occurred.

Custom_Type_Setter

Custom_Type_Setter :: proc(data: rawptr, data_type: typeid, unparsed_value: string, args_tag: string) -> (error: string, handled: bool, alloc_error: runtime.Allocator_Error)Source

Handle setting custom data types.

  • data: A raw pointer to the field where the data will go.
  • data_type: Type information on the underlying field.
  • unparsed_value: The unparsed string that the flag is being set to.
  • args_tag: The args tag from the struct's field.
  • error: An error message, or an empty string if no error occurred.
  • handled: A boolean indicating if the setter handles this type.
  • alloc_error: If an allocation error occurred, return it here.

Parse_Error_Reason

Parse_Error_Reason :: enum int { None = 0, // An extra positional argument was given, and there is no `overflow` field. Extra_Positional = 1, // The underlying type does not support the string value it is being set to. Bad_Value = 2, // No flag was given by the user. No_Flag = 3, // No value was given by the user. No_Value = 4, // The flag on the struct is missing. Missing_Flag = 5, // The type itself isn't supported. Unsupported_Type = 6, }Source

Parser

Parser :: struct { // `fields_set` tracks which arguments have been set. // It uses their struct field index. fields_set: bit_array.Bit_Array, // `filled_pos` tracks which arguments have been filled into positional // spots, much like how `fmt` treats them. filled_pos: bit_array.Bit_Array, }Source

Used to group state together.

Parsing_Style

Parsing_Style :: enum int { // Odin-style: `-flag`, `-flag:option`, `-map:key=value` Odin = 0, // UNIX-style: `-flag` or `--flag`, `--flag=argument`, `--flag argument (manifold-argument)` Unix = 1, }Source

Constants

18

IMPORTING_NET

IMPORTING_NET :: _ = #config(ODIN_CORE_FLAGS_USE_NET, ODIN_OS == .Windows || ODIN_OS == .Linux || ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD)Source

Override support for parsing net types.

NO_CORE_NAMED_TYPES

NO_CORE_NAMED_TYPES :: _ = #config(ODIN_CORE_FLAGS_NO_CORE_NAMED_TYPES, false)Source

Set to true to compile with support for core named types disabled, as a fallback in the event your platform does not support one of the types, or you have no need for them and want a smaller binary.

Procedures

27

get_subtag

get_subtag :: proc(tag: string, id: string) -> (value: string, ok: bool)Source

Get the value for a subtag.

This is useful if you need to parse through the args tag for a struct field on a custom type setter or custom flag checker.

Example:

import "core:flags"
import "core:fmt"

get_subtag_example :: proc() {
	args_tag := "precision=3,signed"

	precision, has_precision := flags.get_subtag(args_tag, "precision")
	signed, is_signed := flags.get_subtag(args_tag, "signed")

	fmt.printfln("precision = %q, %t", precision, has_precision)
	fmt.printfln("signed = %q, %t", signed, is_signed)
}

Output:

precision = "3", true
signed = "", true

parse

parse :: proc( model: ^T, args: []string, style: Parsing_Style, validate_args: bool, strict: bool, allocator: mem.Allocator = context.allocator, loc: _ = #caller_location, ) -> (error: Error)Source

Parse a slice of command-line arguments into an annotated struct.

Allocates Using Provided Allocator

By default, this proc will only allocate memory outside of its lifetime if it has to append to a dynamic array, set a map value, or set a cstring.

The program is expected to free any allocations on model as a result of parsing.

  • model: A pointer to an annotated struct with flag definitions.
  • args: A slice of strings, usually os.args[1:].
  • style: The argument parsing style.
  • validate_args: If true, will ensure that all required arguments are set if no errors occurred.
  • strict: If true, will return on first error. Otherwise, parsing continues.
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • error: A union of errors; parsing, file open, a help request, or validation.

parse_or_exit

parse_or_exit :: proc(model: ^T, program_args: []string, style: Parsing_Style, allocator: mem.Allocator = context.allocator, loc = #caller_location)Source

Parse any arguments into an annotated struct or exit if there was an error.

Allocates Using Provided Allocator

This is a convenience wrapper over parse and print_errors.

  • model: A pointer to an annotated struct.
  • program_args: A slice of strings, usually os.args.
  • style: The argument parsing style.
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)

write_usage

write_usage :: proc(out: io.Writer, data_type: typeid, program: string, style: Parsing_Style)Source

Write out the documentation for the command-line arguments to a stream.

  • out: The stream to write to.
  • data_type: The typeid of the data structure to describe.
  • program: The name of the program, usually the first argument to os.args.
  • style: The argument parsing style, required to show flags in the proper style.

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.