Search…
What does DCli do?
DCli has a singular focus:
make it easy to build command line apps using the Dart programming language.
DCli has the following aims:
  • make building CLI apps as easy as walking.
  • fully utilise the expressiveness of Dart.
  • works seamlessly with the core Dart libraries.
  • provide a cross platform API for Windows, OSx and Linux.
  • create a completely synchronously API (no futures and no async).
  • call any CLI app.
  • make debugging CLI apps easy.
  • generate error messages that make it easy to resolve problems.
  • provide quality documentation and examples.

DCli's API covers a number of areas:

User input

Asking the user to input data into a CLI application should be simple. DCli provide a number of functions to facilitate user input.
  • ask
  • confirm
  • menu
1
import 'package:dcli/dcli.dart';
2
void main(){
3
var username = ask('Username:', validator: Ask.required);
4
if (confirm('Are you over 18')) {
5
print(orange('Welcome to the club'));
6
}
7
8
var selected = menu('Select your poision', options: ['beer', 'wine', 'spirits']
9
defaultOption: 'beer');
10
print(green('You choice was: $selected'));
11
}
Copied!
DCli provide an extensive API designed specifically for building command line apps.

Displaying information

Out of the box dart provides the basic 'print' statement which DCli extends to provide common features.
  • print
  • printerr - prints to stderr.
  • colour coding
  • cursor management
  • clear screen/clear line
1
print(orange("I'm an important message"));
2
printerr(red('Oops, something went wrong here'));
Copied!

Manage files and directories

A fundamental task of most CLI applications is the management of files and directories. DCli provides a large set of tools for file management such as:
  • find
  • which
  • copy
  • copyTree
  • move
  • moveTree
  • delete
  • deleteDir
  • touch
  • exists
  • isWritable/isReadable/isExecutable
  • isFile
  • isLink
  • isDirectory
  • lastModified
DCli ships with the excellent paths package that lets you easily manipulate file paths.
1
import 'package:dcli/dcli.dart';
2
void main() {
3
if (!exists('/keep')) {
4
createDir('/keep');
5
}
6
7
var images = find('*.png', root: '/images');
8
for (var image in images) {
9
if (image.startsWith('nsfw')) {
10
copy(image, '/keep');
11
}
12
}
13
}
Copied!

Read/Write files

You are often going to need to read/write and modify text and binary files.
  • read
  • write
  • truncate
  • append
  • replace
  • tail
  • cat
You still have full access to the full set of Dart APIs
1
// write lines to myfile.ini
2
var settings = 'myfile.ini';
3
settings.write('[section1]');
4
settings.append('color=red');
5
settings.append('color=blue');
6
7
// fix the spelling in myfile.ini
8
replace(settings, 'color', 'colour');
Copied!

Call command line apps

A core feature of DCli is the ability to call other CLI apps and process their output.
  • run
  • start
  • toList
  • forEach
  • firstLine
  • lastLine
  • | (pipe)
  • stream
Run mysql with all output being displayed to the console.
1
var user = ask('username');
2
var password = ask('password', hidden:true);
3
'mysql -u $user --password=$password customerdb -e "select 1"'.run;
Copied!
Run a mysql command and store the results in a list (users).
1
var users = 'mysql customerdb -e "select fname, sname from user"'
2
.toList(skipLines: 1);
3
/// now parse each user and print their name
4
for (var user in users) {
5
var parts = user.split(',');
6
print('Firstname: ${parts[0]} Surname: ${parts[1]});
7
}
8
/// Run grep and print each line that it finds
9
'grep error /var/lib/syslog'.forEach((line) => print(line));
Copied!

Explore you environment

DCli makes it easy to explore your environment with direct manipulation of environment variables.
1
var user = env['USER'];
2
env['JAVA_HOME'] = '/usr/lib/java';
Copied!
PATH management tools:
1
Env().appendToPath('/home/me/bin');
2
Env().addToPATHIfAbsent('/home/me/bin');
3
Env().removeFromPATH('/home/me/bin');
Copied!
And the ability to explore the Dart environment.
1
/// Get details of the current dart script that is running.
2
Script.current;
3
DartSdk().pathToDartExe;
4
PubCache().pathTo
Copied!
You can explore the complete api here.
Last modified 1mo ago