Shellminator V3.0.1
Simple Terminal
|
Commander isn’t just for handling commands—it can also manage system variables. The idea is inspired by Linux environment variables, but there are a few key differences.
There are many parameters in a system that are useful to query dynamically. System variables can also be used directly as arguments for commands. In Commander, we can define three types of system variables:
The advantage of int and float types is that they can be modified from the terminal with simple commands. However, modifying string variables at runtime is risky, as improper handling can cause memory overflow or bus faults. Because of this, it’s best to avoid modifying string variables dynamically.
System variables are stored in a system variable tree, just like commands are stored in a tree. Here’s how we define them:
The setup is simple:
systemVariableInt
, systemVariableFloat
, or systemVariableString
macros. ⚠️ Important: The variable name in your C code will be the same name used in the terminal.
BATTERY
variable in C can be accessed as $BATTERY
in the terminal. It’s highly recommended (but not required) to use uppercase names for system variables, as this improves readability and follows common conventions.
Once we’ve defined our system variables, we need to attach them to the Commander object using the attachVariables()
method:
System variables aren’t tied to a specific object, but rather to the Commander class itself. This means that if you have multiple Commander objects in your system, calling attachVariables()
on any one of them makes the variables accessible to all Commander instances.
This design helps optimize resource usage and allows the Argument class to access system variables linked to the Commander class.
To see system variables in action, let’s create a simple echo command that prints out whatever it receives as input:
This echo
command simply prints back what it receives as input. However, there’s a twist:
Command | Output | Explanation |
---|---|---|
echo VERSION | VERSION | Just prints the input as-is. |
echo $VERSION | V1.0.2a | $VERSION gets replaced with the system variable's value. |
echo MILLIS | MILLIS | Prints the input as-is. |
echo $MILLIS | (some number) | Prints the current value of MILLIS , which increases over time. |
echo $Millis | $Millis | System variables are case-sensitive, so this doesn’t match MILLIS . |
echo "$MILLIS" | $MILLIS | Since it’s inside quotes, it’s treated as a string, not a variable. |
$
prefix to be recognized and replaced. $MILLIS
works, but $Millis
doesn’t). "$MILLIS"
stays as-is). $MILLIS
change over time, so running the command multiple times gives different values.