Dart Lesson 39 – Command Line Tools | Dataplexa

Building Command-Line Tools in Dart

In this lesson, you will learn how to build command-line interface (CLI) tools using Dart. CLI tools are widely used for automation, scripting, data processing, and DevOps workflows.

Dart is not only for Flutter apps — it is also a powerful language for building fast, maintainable command-line applications.


What Is a CLI Tool?

A command-line tool is a program that runs in a terminal and interacts with users through commands and arguments instead of graphical interfaces.

Examples of CLI tools:

  • Git
  • Docker
  • npm
  • Dart CLI utilities

Creating a Basic Dart CLI Program

Every Dart program starts from the main() function.

void main() {
  print('Welcome to Dart CLI');
}

Run it using:

dart run main.dart


Reading Command-Line Arguments

Dart provides access to command-line arguments using List<String> args.

void main(List<String> args) {
  print(args);
}

Run with arguments:

dart run main.dart hello world

Output:

[hello, world]


Using Arguments in Logic

Let’s build a simple greeting tool.

void main(List<String> args) {
  if (args.isEmpty) {
    print('Please provide your name');
    return;
  }

  print('Hello, ${args[0]}!');
}

This allows dynamic user input through the terminal.


Reading User Input from Terminal

Sometimes you want interactive input instead of arguments.

import 'dart:io';

void main() {
  stdout.write('Enter your age: ');
  final input = stdin.readLineSync();

  print('Your age is $input');
}

Parsing Numeric Input

Real-world CLI tools often work with numbers.

import 'dart:io';

void main() {
  stdout.write('Enter a number: ');
  final input = stdin.readLineSync();

  final number = int.tryParse(input ?? '');

  if (number == null) {
    print('Invalid number');
  } else {
    print('Square: ${number * number}');
  }
}

Creating a Simple Calculator CLI

Let’s build a calculator that takes two numbers from arguments.

void main(List<String> args) {
  if (args.length < 2) {
    print('Usage: dart run calc.dart num1 num2');
    return;
  }

  final a = int.parse(args[0]);
  final b = int.parse(args[1]);

  print('Sum: ${a + b}');
  print('Difference: ${a - b}');
  print('Product: ${a * b}');
}

Working with Files in CLI Tools

CLI tools often read or write files.

import 'dart:io';

void main() {
  final file = File('data.txt');
  file.writeAsStringSync('Hello from Dart CLI');

  print('File written successfully');
}

Error Handling in CLI Programs

Always handle runtime errors to avoid crashes.

void main(List<String> args) {
  try {
    final number = int.parse(args[0]);
    print(number * 10);
  } catch (e) {
    print('Error: Please provide a valid number');
  }
}

Real-World Use Cases

  • Automation scripts
  • Log analyzers
  • Data processing tools
  • Build and deployment scripts

📝 Practice Exercises

Exercise 1

Create a CLI tool that converts Celsius to Fahrenheit.

Exercise 2

Build a CLI tool that counts words in a text file.

Exercise 3

Create a calculator that supports addition and multiplication.


What’s Next?

In the next lesson, you will learn how to test Dart applications and ensure your CLI tools work correctly and reliably.