Search…
Writing your first CLI app
Let's start by going over the basic by writing the classic hello world program.
Create a directory to work in:
Linux
Windows
OSx
1
mkdir dcli_scripts
2
cd dcli_scripts
Copied!
1
mkdir dcli_scripts
2
cd dcli_scripts
Copied!
1
mkdir dcli_scripts
2
cd dcli_scripts
Copied!
Using your preferred editor create a file called 'hello.dart' with following content:
1
void main() {
2
print('Hello World.');
3
}
Copied!
If you are using vscode you can simply type code hello.dart to edit the file.

Running

Now lets run your script:
1
dart hello.dart
2
> Hello World.
Copied!
When we run our script using the dart command, dart performs JIT compilation of our script which slows down the startup time a little but makes for fast test iteration.
In vscode you should see 'Run | Debug' just above main(). Click Debug to start your app.

Compiling

You can compile your script to a native executable so that it launches and runs much faster.
The DCli tools allow you to run you app without compiling and without prefixing it with dart.
1
dart compile exe hello.dart
2
Generated: hello.exe
3
./hello.exe
4
> Hello World
5
6
ll hello.exe
7
-rwxrwxr-x 1 5,875,400 Sep 5 14:30 hello.exe*
Copied!
You now have a completely self contained executable which you can copy to any binary compatible machine.
The exe is 5MB in size and does NOT require Dart to be installed.

Dependencies

So far we haven't actually used the DCli API in our hello.dart program. Let's now setup dependency management so we can use the DCli API and any other Dart package.
Search pub.dev for third party package. You can only use packages labelled 'DART | NATIVE'
Dart uses a special file called pubspec.yaml to control the set of packages accessible to your application.
Dart's pubspec.yaml is equivalent to a makefile, pom.xml, gradle.build or package.json, in that it defines the set of dependencies for you application.
To use the DCli API or any other Dart package you need to add the dependency to your pubspec.yaml.
Create and edit your first 'pubspec.yaml' file using your preferred editor:
Check pub.dev for the latest version no. of DCli.
1
name: hello_world
2
description: My first app that does bugger all.
3
4
dependencies:
5
dcli: ^0.51.0
Copied!
The pubspec.yaml lives at the top of your projects directory tree. We refer to this directory as your package root.
Whenever you change your 'pubspec.yaml' you must run 'pub get' to download the required dependencies:
1
pub get
2
Resolving dependencies...
3
Got dependencies!
Copied!

Writing a DCli script

Now that we have added DCli to our pubspec.yaml we can modify hello.dart to make calls to the DCli API.
Edit your hello.dart script as follows:
1
import 'package:dcli/dcli.dart';
2
3
void main() {
4
print("Now let's do someting useful.");
5
6
var username = ask( 'username:');
7
print('username: $username');
8
9
var password = ask( 'password:', hidden: true);
10
print('password: $password');
11
12
// create a directory
13
if (!exists('tmp')) {
14
createDir('tmp');
15
}
16
17
// Truncate any existing content
18
// of the file 'tmp/text.txt' and write
19
// 'Hello world' to the file.
20
'tmp/text.txt'.write('Hello world');
21
22
// append 'My second line' to the file 'tmp/text.txt'.
23
'tmp/text.txt'.append('My second line');
24
25
// and another append to the same file.
26
'tmp/text.txt'.append('My third line');
27
28
// now copy the file tmp/text.txt to second.txt
29
copy('tmp/text.txt', 'tmp/second.txt', overwrite: true);
30
31
// lets dump the file we just created to the console
32
read('tmp/second.txt').forEach((line) => print(line));
33
34
// lets prove that both files exist by running
35
// a recursive find.
36
find('*.txt').forEach((file) => print('Found $file'));
37
38
// Now lets tail the file using the OS tail command.
39
// Again a dart extensions we treat a string
40
// as an OS command and run that command as
41
// a child process.
42
// Stdout and stderr output are written
43
// directly to the console.
44
'tail tmp/text.txt'.run;
45
46
// Lets do a word count capturing stdout,
47
// stderr will will be swallowed.
48
'wc tmp/second.txt'.forEach((line) => print('Captured $line'));
49
50
if (confirm( "Should I delete 'tmp'? (y/n):")) {
51
// Now lets clean up
52
delete('tmp/text.txt');
53
delete('tmp/second.txt');
54
deleteDir('tmp');
55
}
56
57
}
Copied!
Now run our script.
1
dart hello.dart
2
Now lets do someting useful.
3
username: auser
4
username: auser
5
password: *********
6
password: apassword
7
Hello world
8
My second line
9
My third line
10
Found /tmp/fred/tmp/second.txt
11
Found /tmp/fred/tmp/text.txt
12
Hello world
13
My second line
14
My third line
15
Captured 3 8 41 tmp/second.txt
16
Should I delete 'tmp'? (y/n): (y/n): y
Copied!
You are now officially a DCli guru.
Go forth young man (or gal) and create.
Last modified 1mo ago