Summary - CLI
- Dart course
- Advanced
- Lesson 60/83
- 5 min
Combine args, ArgParser and stdin to write a CLI tool.
Summary - CLI
In this group we learned to build CLI tools with Dart. args and ArgParser parse command-line arguments; stdin/stdout provide interactive input/output; cli_util offers configuration and terminal helpers; dcli runs shell commands.
In this summary we will write a small program that brings args and stdout together. The program parses a name option with ArgParser and prints the result with stdout.writeln. This is a small but working example of a real CLI tool.
Practice CLI summary
Add import 'package:args/args.dart'; and import 'dart:io';. In main create final parser = ArgParser()..addOption('name', abbr: 'n');. Parse with final results = parser.parse(['--name=Ayşe']);. Print with stdout.writeln('Summary: ${results['name']}').
Starter code
// import the args package and dart:io
void main() {
// parse --name with ArgParser and print with stdout
}Sample solution
import 'package:args/args.dart';
import 'dart:io';
void main() {
final parser = ArgParser()..addOption('name', abbr: 'n');
final results = parser.parse(['--name=Ayşe']);
stdout.writeln('Summary: ${results["name"]}');
}Expected output
Summary: Ayşe
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which package parses CLI arguments?
- jaspr
- sqflite
- drift
- args Correct answer
Explanation: the args package parses CLI arguments.
2 Match the CLI tools
| Term | Meaning |
|---|---|
args |
Argument parsing |
ArgParser |
Parser |
stdin |
Input |
dcli |
Shell commands |
3 Creating an ArgParser
final parser = ____();
final parser = ArgParser();
Explanation: ArgParser() creates a parser.