Files
kaizen/external/cflags
iris 00cc9309cb Squashed 'external/ircolib/' changes from ce3cd726c..de6e324bd
de6e324bd separate emu thread
10d3daf86 Roms List improvements
95d202f37 Let's make the rom list process on a separate thread so the emulator doesnt take ages to load.
fc306967f Wow the ROM Header was just completely busted. Game list view works now
bad1691ee fuck this shit
2b59e5f46 game list in progress
d26417b83 remappable inputs in progress
ac4af8106 input
e72abc240 update readme
430139dc9 Qt6 frontend
3080d4d45 Fix this small bug too
08cd13b85 Cop0 unused functions do not actually pose a threat (as per manual). They don't do anything, so shall we.
61bb4fb44 make idle loop detection a little more specific with where the load goes
b037de4c3 SAZDFsdff
12e81e73e need to figure out why n64-systemtest loops indefinitely at some address that appears to be valid (i think it's me not invalidating the cache properly)
204f0e13b idle skipping seems to work!
cb8bb634a sdkfjlasdf
58e5c89c1 Fix compilation issue on my machine (no idea)
24fb2898e attempting more serious idle skipping
214719577 Place rsp.Step inside cached interpreter. Gains about 3 more fps
bb97dcc23 mmmmm
920b77d38 wjkhasdfjhkasdf
430ccdab4 it's a start...
4f42a673a Cached interpreter plays Mario 64. Start looking into RSP as well
c9a030787 idle skipping works!
5fbda03ce new idea
366637aba Idle skipping... maybe?
609fa2fb0 Cache instructions implemented but broken lmao. Commented out for now
e140a6d12 - Stop using inheritance for CPU, instead use composition. - Introduce KAIZEN_JIT_ENABLED optional define instead of relying on __aarch64__ and the like. - More cache work
68e613057 prep cache impl
811b4d809 fix clang format
fda755f7d idk
d5024ebbf small MI refactor in preparation of (eventually) implementing the RDRAM interface properly
694b45341 Merge commit '206dcdedf195fb320913584180edb12c7731e396' as 'external/SDL'
206dcdedf Squashed 'external/SDL/' content from commit 4d17b99d0a
4d16e1cb4 need to update sdl
848b19920 Fix compilation error
db61b5299 Merge commit 'e94a94559f28e49678fbcf72199a5258137b0fe9' as 'external/imgui'
e94a94559 Squashed 'external/imgui/' content from commit 02e9b8cac
52edb3757 need to update imgui
c1a705e86 Emulate weird JALR behaviour
4b4c32f4b Fix exception for "unusable COP1" in 4 instructions i missed accidentally (again)
df5828142 Bug putting 0s in the log everywhere
f8b580048 Make isviewer a sink to file
8241e9735 Fix exception for "unusable COP1" in 4 instructions i missed accidentally
b29715f20 small changes
d9a620bc1 make use of my new small utility library
0d1aa938e Add 'external/ircolib/' from commit 'ce3cd726c8df8388d554abf8bb55d55020eb4450'
e64eb40b3 Fuck git

git-subtree-dir: external/ircolib
git-subtree-split: de6e324bde
2026-06-15 11:56:38 +02:00
..

cflags

Command line flag parsing library in C

Heavily inspired by Go's flag package https://golang.org/pkg/flag/

Building

cflags is a header only library. To use it, simply copy cflags.h or cflags.hpp to your project, or add it to your include path.

You may also install it using cmake, like so:

cmake path/to/source
sudo make install

This will install both CMake and pkg-config configuration files.

Argument Parsing Logic

  • The first argument is stored in program
  • The following arguments are parsed left to right
  • If an argument does not start with -, it is placed in the additional arguments list stored in args/argv
  • If the special -- argument appears, all following arguments are treated as positional
    e.g. -c 4 -- --name hello would parse the -c, but place --name and hello into args/argv
  • Arguments starting with -- are long name flags, e.g. --example
    • The list of flags is searched for one with long_name equal to the argument name (after the --), e.g. long_name == example
    • If a flag is not found with that name, an error is printed and parse() returns false
  • Arguments starting with just - are short name flags, e.g. -xvf
    • These can be grouped together, so they are searched one at a time from left to right, e.g. x, v, then f
    • If any of these fail to match a flag, an error is printed and parse() returns false
  • Once a flag is found, it attempts to find a value
    • Arguments with long names can also come in the forms --name, --name=value, or --name value
    • Arguemnts with short names can come in the forms -n, or -n value
      • Note: Only the last short flag of a group can have a value, e.g. -xvf file will work, but -xfv file will fail
    • If the flag is of type [c]string, int, or float then a value is required, and if one is not found an error is printed and parse() returns false
      • Arguments of type bool can have a value, e.g. --debug=false, but one is not required
    • Each time a flag is encountered, the count member is incremented
    • The value for a flag is overwritten each time the flag is processed, the last argument parsed wins, e.g. -c 4 -c 10 will result in -c being 10
      • If you want to capture each argument separately, use add_*_callback instead

Usage (C)

#include <cflags.h>

void process_string(const char * str)
{
    printf("processing %s\n", str);
}

void process_bool(bool b)
{
    printf("processing %d\n", b);
}

void process_int(int i)
{
    printf("processing %d\n", i);
}

void process_float(float f)
{
    printf("processing %f\n", f);
}

int main(int argc, char** argv)
{
    // Create a cflags object
    cflags_t * flags = cflags_init();

    // Add a bool flag, which will be callable with -d or --debug
    // The value will be true if it exists, and can bet set to false
    // by saying -d false or --debug=false
    bool debug = false;
    cflags_add_bool(flags, 'd', "debug", &debug, "enable debug mode");

    // Add a similar help flag, which will be callable with just --help
    bool help = false;
    cflags_add_bool(flags, '\0', "help", &help, "print this text and exit");

    // Add a string flag
    const char * string = NULL;
    cflags_add_string(flags, 's', "string", &string, "enter a string");

    // Add an int flag
    int count = 0;
    cflags_add_int(flags, 'c', "count", &count, "enter a number");

    // Add a float flag
    float amount = 0.f;
    cflags_add_float(flags, 'a', "amount", &amount, "enter a float");

    // Add a string callback flag. This will call the supplied function with the value
    // when it is parsed
    cflags_add_string_callback(flags, 'f', "file", &process_string, "process a file");

    cflags_add_bool_callback(flags, 'q', "bool-flag", &process_bool, "process a bool");

    cflags_add_int_callback(flags, 'w', "int-flag", &process_int, "process a int");

    cflags_add_float_callback(flags, 'e', "float-flag", &process_float, "process a float");

    // Add a flag that can be called multiple times
    cflags_flag_t * verbose = cflags_add_bool(flags, 'v', "verbose", NULL, "enables verbose output, repeat up to 4 times for more verbosity");

    // Parse the command arguments
    if (!cflags_parse(flags, argc, argv) || help || flags->argc == 0) {
        cflags_print_usage(flags,
            "[OPTION]... [ARG]...", 
            "Tests the cflags library.", 
            "Additional information about this library can be found by at:\n"
            "  https://github.com/WhoBrokeTheBuild/cflags");
    }

    printf("debug: %d\n", debug);
    printf("string: %s\n", string);
    printf("count: %d\n", count);
    printf("amount: %f\n", amount);

    // Print the number of times verbose was added
    printf("verbosity: %d\n", verbose->count);

    // Print any additional arguments, in the order they were parsed
    for (int i = 1; i < flags->argc; ++i) {
        printf("positional arg %d: %s\n", i, flags->argv[i]);
    }

    // Cleanup
    cflags_free(flags);

    return 0;
}

Usage (C++)

#include <cflags.hpp>

void process_string(std::string str)
{
    printf("processing %s\n", str.c_str());
}

void process_cstring(const char * str)
{
    printf("processing %s\n", str);
}

void process_bool(bool b)
{
    printf("processing %d\n", b);
}

void process_int(int i)
{
    printf("processing %d\n", i);
}

void process_float(float f)
{
    printf("processing %f\n", f);
}

int main(int argc, char * argv[])
{
    // Create a cflags object
    cflags::cflags flags;

    // Add a bool flag, which will be callable with -d or --debug
    // The value will be true if it exists, and can bet set to false
    // by saying -d false or --debug=false
    bool debug = false;
    flags.add_bool('d', "debug", &debug, "enable debug mode");

    // Add a similar help flag, which will be callable with just --help
    bool help = false;
    flags.add_bool('\0', "help", &help, "print this text and exit");

    // Add a string flag
    std::string string;
    flags.add_string('s', "string", &string, "enter a string");

    // Add a cstring flag
    const char * cstring = NULL;
    flags.add_cstring('\0', "cstring", &cstring, "enter a string (cstring)");

    // Add an int flag
    int count = 0;
    flags.add_int('c', "count", &count, "enter a number");

    // Add a float flag
    float amount = 0.f;
    flags.add_float('a', "amount", &amount, "enter a float");

    // Add a string callback flag. This will call the supplied function with the value
    // when it is parsed
    flags.add_string_callback('f', "file", &process_string, "process a file");

    flags.add_cstring_callback('\0', "cfile", &process_cstring, "process a file (cstring)");

    flags.add_bool_callback('q', "bool-flag", &process_bool, "process a bool");

    flags.add_int_callback('w', "int-flag", &process_int, "process a int");

    flags.add_float_callback('e', "float-flag", &process_float, "process a float");

    // You can also use lambdas
    flags.add_string_callback('l', "lambda",
        [](std::string value) {
            printf("Hello %s\n", value.c_str());
        },
        "use a lambda function"
    );

    // Add a flag that can be called multiple times
    auto verbose = flags.add_bool('v', "verbose", NULL, "enables verbose output, repeat up to 4 times for more verbosity");

    // Parse the command arguments
    if (!flags.parse(argc, argv) || help || flags.argc == 0) {
        flags.print_usage(
            "[OPTION]... [ARG]...", 
            "Tests the cflags library.", 
            "Additional information about this library can be found by at:\n"
            "  https://github.com/WhoBrokeTheBuild/cflags");
    }

    printf("debug: %d\n", debug);
    printf("string: %s\n", string.c_str());
    printf("cstring: %s\n", cstring);
    printf("count: %d\n", count);
    printf("amount: %f\n", amount);

    // Print the number of times verbose was added
    printf("verbosity: %d\n", verbose->count);

    // Print any additional arguments, in the order they were parsed
    for (auto& arg : flags.args) {
        printf("positional arg %s\n", arg.data());
    }

    // For backwards compatability, the additional arguments are also exposed in argc/argv
    for (int i = 0; i < flags.argc; ++i) {
        printf("positional arg %d: %s\n", i, flags.argv[i]);
    }

    return 0;
}

Quirks

1. Only the last short-name argument in a group may have a value.

For example:

flags.add_string('f', "file", parse_filename, "parse a filename");
flags.add_bool('d', "debug", &debug, "enable debug mode");

// These will work
test -df test.txt
test -df test.txt -f test2.txt

// And these will fail
test -fd test.txt
test -ff test.txt test2.txt

2. Any char * strings point to the original memory from argv

Call the following code as program -s hello

int main(int argc, char ** argv)
{
    for (int i = 1; i < argc; ++i) {
        printf("argv[%d] @ %p: %s\n", i, argv[i], argv[i]);
    }

    cflags_t * flags = cflags_init();

    const char * string = NULL;
    cflags_add_string(flags, 's', "string", &string, "enter a string");

    cflags_parse(flags, argc, argv);

    // The address pointed to by `string` is the same memory pointed to by argv
    printf("string @ %p: %s\n", string, string);
    
    cflags_free(flags);

    return 0;
}

Example output

argv[1] @ 0000024F6BDD665F: -s
argv[2] @ 0000024F6BDD6662: hello
string @ 0000024F6BDD6662: hello

When parsing arguments in the form --name=value, the memory pointed to by argv is altered and the = is replaced by a \0.

When using the C++ version, arguments as std::string do not point at argv as their memory gets copied.