Command Line Subcommands in AngelScript

Here’s the translation of the Go code to AngelScript, formatted in Markdown suitable for Hugo:

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

#include <string>
#include <vector>
#include <iostream>

void printUsage() {
    std::cout << "expected 'foo' or 'bar' subcommands" << std::endl;
}

int main(int argc, string[] args) {
    if (argc < 2) {
        printUsage();
        return 1;
    }

    string subcommand = args[1];

    if (subcommand == "foo") {
        bool enable = false;
        string name = "";
        vector<string> tail;

        // Parse foo subcommand arguments
        for (int i = 2; i < argc; i++) {
            if (args[i] == "-enable") {
                enable = true;
            } else if (args[i].substr(0, 6) == "-name=") {
                name = args[i].substr(6);
            } else {
                tail.push_back(args[i]);
            }
        }

        std::cout << "subcommand 'foo'" << std::endl;
        std::cout << "  enable: " << (enable ? "true" : "false") << std::endl;
        std::cout << "  name: " << name << std::endl;
        std::cout << "  tail: ";
        for (const auto& arg : tail) {
            std::cout << arg << " ";
        }
        std::cout << std::endl;
    } else if (subcommand == "bar") {
        int level = 0;
        vector<string> tail;

        // Parse bar subcommand arguments
        for (int i = 2; i < argc; i++) {
            if (args[i].substr(0, 7) == "-level=") {
                level = std::stoi(args[i].substr(7));
            } else {
                tail.push_back(args[i]);
            }
        }

        std::cout << "subcommand 'bar'" << std::endl;
        std::cout << "  level: " << level << std::endl;
        std::cout << "  tail: ";
        for (const auto& arg : tail) {
            std::cout << arg << " ";
        }
        std::cout << std::endl;
    } else {
        printUsage();
        return 1;
    }

    return 0;
}

This program demonstrates how to implement subcommands in AngelScript. The subcommand is expected as the first argument to the program.

To run the program, compile it and then use the following commands:

$ ./program foo -enable -name=joe a1 a2
subcommand 'foo'
  enable: true
  name: joe
  tail: a1 a2

$ ./program bar -level=8 a1
subcommand 'bar'
  level: 8
  tail: a1

$ ./program bar -enable a1
expected 'foo' or 'bar' subcommands

In this example, we’ve implemented two subcommands: ‘foo’ and ‘bar’. Each subcommand has its own set of flags and arguments.

The ‘foo’ subcommand supports an ‘-enable’ flag and a ‘-name’ option, while the ‘bar’ subcommand supports a ‘-level’ option. Both subcommands can also accept additional positional arguments, which are collected in the ’tail’ vector.

Note that AngelScript doesn’t have built-in command-line parsing libraries like Go’s flag package, so we’ve implemented a simple parsing logic manually. In a real-world application, you might want to use a more robust command-line parsing library if available for AngelScript.

This approach allows you to create command-line tools with multiple subcommands, each with its own set of options and behaviors, similar to tools like git or docker.