Skip to content

ArgParseInit

Description

Create a parser. The name and about pointers are borrowed – they must outlive the parser (string literals are the typical case). The -h / --help flag is registered lazily on the first ArgParseRun / ArgParseHelp call.

Two call shapes, like StrInit / VecInitT: ArgParseInit(name, about) – uses the surrounding Scope’s allocator. ArgParseInit(name, about, alloc) – explicit allocator.

Success

Yields an initialized parser. The specs Vec is empty; add specs with ArgParseAddPositional / etc.

Failure

Cannot fail at construction; first allocator OOM surfaces from later spec-Vec growth.

Usage example (Cross-references)

Usage examples (Cross-references)
            Zstr hostname = NULL;
    
            ArgParse ap = ArgParseInit("resolve", "look up a hostname via /etc/hosts and DNS");
            ArgPositional(&ap, "hostname", &hostname, "name to resolve");
            Zstr upstream_spec = NULL;
    
            ArgParse ap = ArgParseInit("beam", "small reverse-proxy");
            ArgRequired(&ap, "-l", "--listen", &listen_spec, "host:port to listen on");
            ArgRequired(&ap, "-u", "--upstream", &upstream_spec, "upstream host:port");
    static bool test_required_long_space(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        Zstr listen = NULL;
    static bool test_required_long_equals(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        Zstr listen = NULL;
    static bool test_required_short(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        Zstr listen = NULL;
    static bool test_optional_default_preserved(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        u32 timeout = 30;
    static bool test_optional_overrides_default(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        u32 timeout = 30;
    static bool test_flag_presence(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        bool verbose = false;
    static bool test_flag_absence(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        bool verbose = false;
    static bool test_count_repeated(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        u32 verbose = 0;
    static bool test_count_bundled(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        u32 verbose = 0;
    static bool test_positional_order(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("cp", NULL, &a);
    
        Zstr src = NULL;
    static bool test_positional_with_interleaved_flag(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("cp", NULL, &a);
    
        Zstr src     = NULL;
    static bool test_type_inferred_u32(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        u32 n = 0;
    static bool test_type_inferred_i64_negative(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        i64 v = 0;
    static bool test_type_inferred_f64(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        f64 ratio = 1.0;
    static bool test_type_inferred_str(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        Str name = StrInit(&a);
    static bool test_missing_required(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        Zstr listen = NULL;
    static bool test_missing_positional(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("cp", NULL, &a);
    
        Zstr src = NULL;
    static bool test_unknown_option(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        bool v = false;
    static bool test_invalid_value_for_type(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        u32 n = 0;
    static bool test_u8_overflow(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        u8 v = 0;
    static bool test_too_many_positionals(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", NULL, &a);
    
        Zstr x = NULL;
    static bool test_double_dash_separator(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("cat", NULL, &a);
    
        Zstr file = NULL;
    static bool test_help_returns_help_code(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        ArgParse         p = ArgParseInit("prog", "test prog", &a);
    
        Zstr required = NULL;
Last updated on