Command Line Flags in AngelScript

Our first program demonstrates how to use command-line flags in AngelScript. Here’s the full source code:

void main()
{
    // AngelScript doesn't have a built-in flag parsing library, so we'll
    // implement a basic version ourselves.
    array<string> args = getCommandLineArgs();

    string word = "foo";
    int numb = 42;
    bool fork = false;
    string svar = "bar";

    // Parse command-line arguments
    for (uint i = 1; i < args.length(); i++)
    {
        string arg = args[i];
        if (arg.substr(0, 7) == "--word=")
            word = arg.substr(7);
        else if (arg.substr(0, 7) == "--numb=")
            numb = parseInt(arg.substr(7));
        else if (arg == "--fork")
            fork = true;
        else if (arg.substr(0, 7) == "--svar=")
            svar = arg.substr(7);
    }

    // Print the parsed options
    print("word: " + word);
    print("numb: " + numb);
    print("fork: " + (fork ? "true" : "false"));
    print("svar: " + svar);

    // Print any remaining arguments
    print("tail: " + join(args.slice(1), " "));
}

To experiment with the command-line flags program, you’ll need to compile it and then run the resulting binary directly.

Try out the built program by first giving it values for all flags:

$ ./command-line-flags --word=opt --numb=7 --fork --svar=flag
word: opt
numb: 7
fork: true
svar: flag
tail: 

Note that if you omit flags, they automatically take their default values:

$ ./command-line-flags --word=opt
word: opt
numb: 42
fork: false
svar: bar
tail: 

Trailing positional arguments can be provided after any flags:

$ ./command-line-flags --word=opt a1 a2 a3
word: opt
numb: 42
fork: false
svar: bar
tail: a1 a2 a3

Unlike the Go version, this basic implementation doesn’t automatically generate help text or handle errors for undefined flags. You would need to implement these features manually if required.

Note that AngelScript doesn’t have built-in support for command-line flag parsing like Go’s flag package. This example provides a basic implementation that mimics some of the functionality, but it’s not as robust or feature-complete as the Go version. In a real-world scenario, you might want to use or create a more comprehensive command-line parsing library for AngelScript.