Command Line Subcommands in Modelica
Here’s the translation of the Go code to Modelica, formatted in Markdown suitable for Hugo:
Our first program demonstrates how to create command-line subcommands with their own set of flags. This is similar to tools like git where different subcommands (e.g., git commit, git push) have their own specific flags.
model CommandLineSubcommands
import Modelica.Utilities.Strings;
import Modelica.Utilities.System;
function parseArguments
input String[:] args;
output String subcommand;
output Boolean fooEnable;
output String fooName;
output Integer barLevel;
algorithm
if size(args, 1) < 2 then
System.error("expected 'foo' or 'bar' subcommands");
end if;
subcommand := args[2];
if subcommand == "foo" then
fooEnable := false;
fooName := "";
for i in 3:size(args, 1) loop
if args[i] == "-enable" then
fooEnable := true;
elseif Strings.startsWith(args[i], "-name=") then
fooName := Strings.substring(args[i], 7, Strings.length(args[i]));
end if;
end for;
elseif subcommand == "bar" then
barLevel := 0;
for i in 3:size(args, 1) loop
if Strings.startsWith(args[i], "-level=") then
barLevel := Integer(Strings.substring(args[i], 8, Strings.length(args[i])));
end if;
end for;
else
System.error("expected 'foo' or 'bar' subcommands");
end if;
end parseArguments;
function main
input String[:] args;
protected
String subcommand;
Boolean fooEnable;
String fooName;
Integer barLevel;
algorithm
(subcommand, fooEnable, fooName, barLevel) := parseArguments(args);
if subcommand == "foo" then
Modelica.Utilities.Streams.print("subcommand 'foo'");
Modelica.Utilities.Streams.print(" enable: " + String(fooEnable));
Modelica.Utilities.Streams.print(" name: " + fooName);
elseif subcommand == "bar" then
Modelica.Utilities.Streams.print("subcommand 'bar'");
Modelica.Utilities.Streams.print(" level: " + String(barLevel));
end if;
end main;
equation
when initial() then
main(Modelica.Utilities.System.getArguments());
end when;
end CommandLineSubcommands;In this Modelica implementation, we’ve created a model CommandLineSubcommands that simulates the behavior of command-line subcommands. The parseArguments function processes the command-line arguments, and the main function handles the logic for different subcommands.
To use this model:
- Save the code in a file named
CommandLineSubcommands.mo. - Compile and run the model using a Modelica simulation environment.
For example, to simulate the “foo” subcommand:
$ modelica CommandLineSubcommands.mo foo -enable -name=joe
subcommand 'foo'
enable: true
name: joeAnd for the “bar” subcommand:
$ modelica CommandLineSubcommands.mo bar -level=8
subcommand 'bar'
level: 8Note that Modelica doesn’t have built-in support for command-line argument parsing like Go’s flag package. This implementation provides a basic simulation of that functionality using string parsing and Modelica’s utility functions.
In Modelica, we typically work with models and simulations rather than standalone command-line programs. This example adapts the concept to fit within Modelica’s modeling paradigm.