argtable / argtable3

A single-file, ANSI C, command-line parsing library that parses GNU-style command-line options.
http://www.argtable.org
Other
372 stars 65 forks source link

Metadata for each argument #21

Closed computerquip-streamlabs closed 5 years ago

computerquip-streamlabs commented 6 years ago

Might be useful told hold an enum value in arg_hdr to have the ability to cast back from void*.

computerquip-streamlabs commented 6 years ago

I implemented this in my code. It's kinda gross but looks like this essentially:

enum arg_type {
    ARG_LITERAL,
    ARG_STRING,
    ARG_INTEGER,
    ARG_END
};

...

    void *arg_table[] = {
        help,
        dump_args,
        base_url,
        app_dir,
        temp_dir,
        pids,
        end
    };

    /* We need type information to dump parameters generically */
    enum arg_type arg_table_types[] = {
        ARG_LITERAL,
        ARG_LITERAL,
        ARG_STRING,
        ARG_STRING,
        ARG_STRING,
        ARG_INTEGER,
        ARG_END
    };

    if (dump_args->count > 0) {
        for (int i = 0; i < arg_table_sz; ++i) {
            const arg_type type = arg_table_types[i];

            switch (type) {
            case ARG_LITERAL:
                print_literal_arg((struct arg_lit*)arg_table[i]);
                break;
            case ARG_STRING:
                print_string_arg((struct arg_str*)arg_table[i]);
                break;
            case ARG_INTEGER:
                print_integer_arg((struct arg_int*)arg_table[i]);
                break;
            case ARG_END:
                print_end_arg((struct arg_end*)arg_table[i]);
                break;
            }
        }
    }

this let's me generically print each argument and their values.

tomghuang commented 5 years ago

You can try the information in const char* arg_hdr::datatype.