Shellminator V3.0.1
Simple Terminal
|
Every command-line system eventually faces the challenge of parameterizing commands —at some point, simple standalone commands aren’t enough.
Let’s take the sum
command from our previous example. I bet you’ve wondered:
Is there an easier way to parse those two numbers?
Good news—yes, there is! But before we jump in, let’s go over a few key concepts.
In general, there are three types of arguments:
What’s cool is that these argument types can be mixed. For example, you might require two positional arguments at the start and allow a few optional ones at the end. From an implementation standpoint, optional and non-positional arguments can be handled similarly since their placement is flexible.
Positional arguments are useful when:
Let’s set up positional arguments for our sum
function.
First, you need to include the Commander-Arguments package:
For each argument, we create an Argument
object. The constructor determines the type of argument.
The basic syntax for a positional argument is:
args
→ The argument list received in the command callback. place
→ The index of the argument (starting from 0
, left to right). For example, given the command:
10
. 20
. After configuring the argument, we need to extract its value. The Commander library supports three data types:
parseInt()
) parseFloat()
) parseString()
) Since we need two integers, we’ll use parseInt()
. The parsing functions return true
if successful, or false
if the argument could not be converted.
Now that we’ve successfully parsed the argument, we need to retrieve its value. Here’s where C++ magic comes into play—don’t worry, it’s simpler than it sounds!
To get the argument value, cast the argument object to the desired type:
⚠️ Important: The cast type must match the parsing method you used. Since we used parseInt()
, we cast the argument to int
.
This method makes it easy to work with positional arguments and helps keep command processing efficient. Stay tuned—we’ll explore non-positional and optional arguments next!