Search…
Using DCli functions

Using DCli functions

For complete API documentation refer to: pub.dev
Lets start by looking at the some of the built in functions that DCli supports.
DCli exposes a range of built-in functions that are exposed as Dart global functions.
These functions are the core of how DCli provides a very Bash like feel to writing DCli scripts.
These functions make strong use of named arguments with intelligent defaults so mostly you can use the minimal form of the function.
Take note, there are no Futures or awaits here. Each function runs synchronously.
1
import 'package:dcli/dcli.dart';
2
3
void main() {
4
// Use the global DCli Settings to enable debug output.
5
Settings().setVerbose(enabled: true);
6
7
// Print the current working directory
8
print('PWD: ${pwd}');
9
10
// Create a directory and if necessary
11
// its parent directories.
12
var pathToImages = 'tools/images';
13
createDir(pathToImages, recursive: true);
14
15
16
var pathToGoodJpg = join(pathToImages, 'good.jpg');
17
// create a file (its empty)
18
touch(pathToGoodJpg, create: true);
19
20
// update the last modified time on an existing file
21
touch(pathToGoodJpg);
22
23
print('Showing all files');
24
25
// print out all files in the current directory.
26
// [file] is just a [String]
27
find('*.*', recursive: false).forEach((file) => print(file));
28
29
// take a nap for a couple of seconds.
30
sleep(2);
31
32
print('Find file matching *.jpg');
33
// Find all files that end with .jpg
34
// in the current directory and any subdirectories
35
for (var file in find('*.jpg', workingDirectory: pathToImages).toList()) {
36
print(file);
37
}
38
39
var pathToBadJpg = join(pathToImages, "bad.jpg");
40
// Move/rename a file
41
move(pathToGoodJpg, pathToBadJpg);
42
43
// check if a file exists.
44
if (exists(pathToBadJpg)) {
45
print("bad.jpg exists");
46
}
47
48
// Delete a file asking the user first.
49
delete(pathToBadJpg, ask: true);
50
51
}
Copied!
As you can see we have achieved much of the power of Bash without any of the ugly grammar, and what's more we only used one type of quote!
Last modified 1mo ago
Copy link