Dart Lesson 20 – Dart CLI Basics | Dataplexa

Dart CLI Basics

In this lesson, you will learn how to use the Dart Command Line Interface (CLI). The Dart CLI allows you to create projects, run programs, manage packages, and build real-world command-line applications.

Professional developers rely heavily on the CLI for automation, scripting, backend tools, and project workflows.


What Is the Dart CLI?

The Dart CLI is a set of commands provided by the Dart SDK that lets you:

  • Create Dart projects
  • Run Dart programs
  • Manage dependencies
  • Build executable tools

It works on Windows, macOS, and Linux.


Checking Dart Installation

Before using the CLI, make sure Dart is installed correctly.

dart --version

Example output:

Dart SDK version: 3.3.0 (stable)

Creating a New Dart Project

Use the following command to create a new Dart project:

dart create my_cli_app

This command creates a complete project structure with example files.


Project Structure Explained

After creation, the folder looks like this:

my_cli_app/
 ├── bin/
 │   └── my_cli_app.dart
 ├── lib/
 ├── pubspec.yaml
 └── README.md
  • bin/ → Entry point of the application
  • lib/ → Reusable libraries
  • pubspec.yaml → Dependencies & metadata

Running a Dart Program

Navigate into the project directory and run:

dart run

Or run a specific file:

dart run bin/my_cli_app.dart

Writing a Simple CLI Program

Let’s modify bin/my_cli_app.dart:

void main(List<String> arguments) {
  print('Arguments: $arguments');
}

Run it with arguments:

dart run bin/my_cli_app.dart hello world

Output:

Arguments: [hello, world]

Reading User Input

You can interact with users via the terminal using stdin.

import 'dart:io';

void main() {
  stdout.write('Enter your name: ');
  String? name = stdin.readLineSync();
  print('Hello, $name!');
}

Using CLI Tools from pub.dev

Many Dart packages provide CLI tools. Example: args package for argument parsing.

dart pub add args

Then import it in your program.


Creating Executable Dart Scripts

You can turn Dart programs into executable tools.

dart compile exe bin/my_cli_app.dart

This creates a standalone executable for distribution.


Real-World Use Cases

  • Automation scripts
  • Build tools
  • Data processing pipelines
  • Backend utilities
  • DevOps helpers

📝 Practice Exercises


Exercise 1

Check your Dart version using the CLI.

Exercise 2

Create a new Dart project and run it.

Exercise 3

Write a program that takes two numbers as CLI arguments and prints their sum.


✅ Practice Answers


Answer 1

dart --version

Answer 2

dart create demo_app
cd demo_app
dart run

Answer 3

void main(List<String> args) {
  int a = int.parse(args[0]);
  int b = int.parse(args[1]);
  print(a + b);
}

What’s Next?

In the next lesson, you will begin Dart OOP Basics, where you’ll learn how object-oriented programming works in Dart.