Command Line Arguments in Dart
Here’s the translated Dart code and explanation in Markdown format suitable for Hugo:
Command-line arguments are a common way to parameterize execution of programs. For example, dart run hello.dart
uses run
and hello.dart
arguments to the dart
command.
To experiment with command-line arguments it’s best to run the Dart script directly.
In Dart, the main
function can optionally take a List<String>
parameter which contains the command-line arguments. The Platform.executable
property is used to get the path to the Dart executable, which we include to mimic the behavior of languages that include the program name as the first argument.
The dart:io
library is imported to use the Platform
class, which provides information about the environment in which the program is running.
Next, we’ll look at more advanced command-line processing with option parsing libraries available in Dart.