Command Line Subcommands in Scilab

Here’s the translation of the Go code to Scilab, with explanations in Markdown format suitable for Hugo:

Our program demonstrates how to implement command-line subcommands in Scilab. While Scilab doesn’t have built-in support for subcommands like Go’s flag package, we can simulate this behavior using function arguments and conditional statements.

function main()
    // Check if at least one argument is provided
    if argn(2) < 1 then
        disp("expected 'foo' or 'bar' subcommands")
        exit(1)
    end

    // Get the subcommand (first argument)
    subcommand = argv(1)

    // Check which subcommand is invoked
    select subcommand
    case "foo" then
        fooSubcommand(argv(2:$))
    case "bar" then
        barSubcommand(argv(2:$))
    else
        disp("expected 'foo' or 'bar' subcommands")
        exit(1)
    end
endfunction

function fooSubcommand(args)
    // Parse arguments for foo subcommand
    enable = %f
    name = ""
    for i = 1:length(args)
        if args(i) == "-enable" then
            enable = %t
        elseif strindex(args(i), "-name=") == 1 then
            name = part(args(i), 7:length(args(i)))
        end
    end

    // Display results
    disp("subcommand 'foo'")
    disp("  enable: " + string(enable))
    disp("  name: " + name)
    disp("  tail: " + strcat(args(find(~or(args == ["-enable", "-name=" + name]))), " "))
endfunction

function barSubcommand(args)
    // Parse arguments for bar subcommand
    level = 0
    for i = 1:length(args)
        if strindex(args(i), "-level=") == 1 then
            level = strtod(part(args(i), 8:length(args(i))))
        end
    end

    // Display results
    disp("subcommand 'bar'")
    disp("  level: " + string(level))
    disp("  tail: " + strcat(args(find(args ~= "-level=" + string(level))), " "))
endfunction

// Call the main function
main()

To use this program, save it as command_line_subcommands.sce and run it from the Scilab console or command line.

First, invoke the foo subcommand:

$ scilab-cli -f command_line_subcommands.sce foo -enable -name=joe a1 a2
subcommand 'foo'
  enable: T
  name: joe
  tail: a1 a2

Now try bar:

$ scilab-cli -f command_line_subcommands.sce bar -level=8 a1
subcommand 'bar'
  level: 8
  tail: a1

Note that in Scilab, we don’t have the same level of command-line argument parsing as in the original Go example. The implementation here is a simplified version that demonstrates the concept of subcommands.

In this Scilab version:

  1. We define a main function that checks the first argument to determine which subcommand to run.
  2. Each subcommand is implemented as a separate function (fooSubcommand and barSubcommand).
  3. We manually parse the arguments for each subcommand, simulating the behavior of flag parsing.
  4. The program displays the parsed arguments and any additional (tail) arguments.

This implementation provides a basic structure for handling subcommands in Scilab, although it lacks some of the robustness and error checking of the original Go version.