dcli
- Dart course
- Advanced
- Lesson 59/83
- 5 min
Learn advanced CLI with dcli.
The dcli package
dcli is a powerful CLI framework for Dart. It provides shell command execution, file operations, environment variables and more. It is imported with import 'package:dcli/dcli.dart';.
Example: 'ls'.run; runs a shell command. env['HOME'] reads an environment variable. touch('file.txt') creates a file. dcli is the Dart equivalent of bash scripts; it makes writing system administration tools in Dart easy.
Running shell commands
With dcli a shell command runs as 'command'.run. This sends a Dart string to the shell. Example: 'echo hello'.run; prints 'hello' to the screen.
An environment variable is read with env['VAR']. Example: final home = env['HOME']. This gives the user's home directory. The power of dcli is that you can mix shell commands and Dart code seamlessly; you write system scripts as readable Dart code.
Practice Print dcli info
Add import 'package:dcli/dcli.dart';. Define a function runShell with a 'ls'.run call and an env['HOME'] access inside. In main add print('dcli: CLI framework').
Starter code
// import the dcli package
// define runShell (shell command and env variable)
void main() {
// print dcli info
}Sample solution
import 'package:dcli/dcli.dart';
void runShell() {
'ls'.run;
final home = env['HOME'];
}
void main() {
print('dcli: CLI framework');
}Expected output
dcli: CLI framework
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 The dcli import
import 'package:____/dcli/dcli.dart';
import 'package:dcli/dcli/dcli.dart';
Explanation: dcli is imported via package:dcli/dcli.dart.
2 Match the dcli parts
| Term | Meaning |
|---|---|
dcli |
CLI framework |
'ls'.run |
Shell command |
env |
Environment vars |
touch |
Creates a file |
3 What does dcli run?
- Web server
- Database
- Shell commands Correct answer
- Mobile app
Explanation: dcli runs shell commands.