Reading command line arguments

Dart:
Reading command line arguments

How to:

Dart provides a straightforward approach to access command line arguments via the List<String> args in the main method. Below is a simple example demonstrating how to read and utilize command line arguments.

// main.dart
void main(List<String> args) {
  print('Command Line Arguments:');
  for (var i = 0; i < args.length; i++) {
    print('${i + 1}: ${args[i]}');
  }
}

To run this Dart program and pass command line arguments, use the Dart CLI like so:

dart run main.dart Hello World!

Expected output:

Command Line Arguments:
1: Hello
2: World!

Using a Popular Third-Party Library: args

While Dart’s built-in capabilities for handling command line arguments are robust for many applications, the args package provides a refined way to define and parse command line arguments for more complex needs.

First, add the args package to your pubspec.yaml:

dependencies:
  args: ^2.0.0

Then, use it in your program as follows:

// Using the 'args' package
import 'package:args/args.dart';

void main(List<String> arguments) {
  final parser = ArgParser()..addOption('name', abbr: 'n');
  final argResults = parser.parse(arguments);

  if (argResults.wasParsed('name')) {
    print('Hello, ${argResults['name']}!');
  } else {
    print('No name provided.');
  }
}

Run the program with a named argument:

dart run main.dart --name=John

Expected output:

Hello, John!

This simple introduction to parsing command line arguments, both natively and with the args library, showcases how Dart can handle user inputs right from the console, opening a pathway to creating more interactive and dynamic CLI applications.