Shellminator V3.0.1
Simple Terminal
|
Now, let’s talk about position-independent arguments.
Have you ever compiled a program using GCC from the command line? If so, you might already be familiar with this concept! Position-independent arguments always come in key-value pairs.
In the past, argument names were often a single letter preceded by a dash, like -h
for help. However, modern applications allow more human-friendly argument names that can be full words, as long as they don’t contain spaces. These longer names are usually prefixed with two dashes (--
).
For example:
-h
--help
The argument value follows the argument name, separated by at least one space.
Let's say we need to implement a command called random
. As the name suggests, its job is to generate a random number.
In the syntax used by GCC, the order of function parameters is fixed and cannot be changed, as swapping them could alter the result. In contrast, higher-level languages like Python and Matlab allow parameters to be specified using name-value pairs. This makes it clear which argument is assigned to which value, reducing potential confusion—although this approach requires a bit more typing.
Additionally, modern programming languages often implement random
in two ways:
0
as the default lower limit.We'll build a command that supports both approaches.
Each argument needs a short name (required) and an optional long name.
To keep things consistent, either use short names for all arguments or enable long names support for all arguments.
For simplicity:
-l
and a long name --low
.-h
and a long name --high
. Here’s how we define these arguments:
We need to check four scenarios:
high
argument is provided → ✅ No problem! We assume the lower limit is 0. low
argument is provided → ❌ This doesn't make sense—we should inform the user that something went wrong. Previously, we used parseInt()
to check if an argument was parsed correctly. However, if you call it multiple times, your command might run hundreds of times slower than necessary!
Instead, once parseInt()
, parseFloat()
, or parseString()
has been successfully called, the argument object remembers its parsed value.
To quickly check if an argument was successfully parsed, you can cast the argument object to a bool
.
Example:
To make things a bit more visually appealing, we can also add colors to error messages! This helps draw the user’s attention to the important part of the message—which might even be the solution itself.
The shell.format()
method works seamlessly with the caller
argument, so you can mix and match them without any issues!
Here are a few suggested commands you might want to try in the demo above:
âś… Basic Usage
random?
– Displays a help message explaining how to use the command. random -h 10
– Generates a random number between 0 and 10. random --high 10
– Does the same as the previous command. ✅ Custom Range
random -h 10 -l 5
– Generates a random number between 5 and 10. random -h 10 --low 5
– Same as above. random -l 5 -h 10
– Again, generates a number between 5 and 10. This shows that argument order doesn’t matter! ❌ Error Cases
random
– Error: No arguments provided. random -l 5
– Error: Only the lower limit is given; missing the upper limit. random -l tz5 -h 10
– This is an interesting case! 🤔 You won't get an error, but the lower limit won't be parsed because it's not a valid number. The code will treat it as if only the upper limit was provided, meaning the lower limit defaults to 0.
Give them a try and see what happens!