dcli
  • Introduction
  • What does DCli do?
  • Install DCli
    • Installing on Windows
  • Writing your first CLI app
  • Add DCli to your project
  • pub.dev
  • github
  • Dart basics
    • Dart lambda functions
    • Function Arguments
    • Futures
    • stdin/stdout/stderr a primer
  • Tour
    • Overview
    • Using DCli functions
    • User input
      • Ask Validators
    • Displaying information
    • Managing Files And Directories
    • Environment variables
    • Calling apps
    • Redirecting output
    • Command Line Arguments
    • Paths
    • Glob Expansion
    • Piping
    • Locking
    • Fetch
    • The evils of CD
    • Assets/Resources
    • Cross Platform
      • Posix
      • Windows
      • Docker
        • Detecting Docker
        • Add DCli to a Docker Container
        • Example DCli app in Docker
  • Elevated Privileges
    • Sudo
  • Performance
  • Dependency Management
    • Dependency Management
    • Pubspec Managment
  • DCli Tools
    • DCli tools
    • Use a shebang #!
    • DCli Compile
    • DCli Clean
    • DCli Create
    • DCli Doctor
    • DCli Install
    • DCli Run
    • DCli Warmup
    • DCli Pack
    • Upgrade DCli
  • Internal Workings
    • Internal Workings
    • waitForEx
  • Contributing
    • Creating a release
    • Running Unit tests
    • Implemention support for a shell
    • Templates
  • References
  • Examples
    • Projects
    • Code
      • hello world.
      • dcompress
      • dpath
      • dmysql
      • dshell
      • dwhich
      • dipaddr
      • gnome launcher
  • Articles
    • build CLI apps in dart - part 1
    • build CLI apps in dart - part 2
    • Dealing with permissions
    • 3rd Party console packages
  • Dart on Linux - the perfect CLI tooling
  • Improving your build environment
    • Existing tooling
    • Building with Dart
    • A home for your build tools
  • Olivier Revial - CLI apps made easy
  • Video: package of the week
Powered by GitBook
On this page
  • DCli's API covers a number of areas:
  • User input
  • Displaying information
  • Manage files and directories
  • Read/Write files
  • Call command line apps
  • Explore you environment

Was this helpful?

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.

  • 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 provides a number of functions to facilitate user input.

  • ask

  • confirm

  • menu

import 'package:dcli/dcli.dart';
void main(){
   var username = ask('Username:', validator: Ask.required);
   if (confirm('Are you over 18')) {
       print(orange('Welcome to the club'));
   }
   
   var selected = menu('Select your poison', options: ['beer', 'wine', 'spirits']
      defaultOption: 'beer');
   print(green('You choice was: $selected'));
}

DCli provides 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

print(orange("I'm an important message"));
printerr(red('Oops, something went wrong here'));

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

import 'package:dcli/dcli.dart';
void main() {
    if (!exists('/keep')) {
        createDir('/keep');
    }
    
    var images = find('*.png', root: '/images');
    for (var image in images) {
        if (image.startsWith('nsfw')) {
            copy(image, '/keep');
        }
    }
}

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

//  write lines to myfile.ini
var settings = 'myfile.ini';
settings.write('[section1]');
settings.append('color=red');
settings.append('color=blue');

// fix the spelling in myfile.ini
replace(settings, 'color', 'colour');

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.

var user = ask('username');
var password = ask('password', hidden:true);
'mysql -u $user --password=$password customerdb -e "select 1"'.run;

Run a mysql command and store the results in a list (users).

var users = 'mysql customerdb -e "select fname, sname from user"'
    .toList(skipLines: 1);
/// now parse each user and print their name
for (var user in users) {
    var parts = user.split(',');
    print('Firstname: ${parts[0]} Surname: ${parts[1]});
}
/// Run grep and print each line that it finds
'grep error /var/lib/syslog'.forEach((line) => print(line));

Explore you environment

DCli makes it easy to explore your environment with direct manipulation of environment variables.

var user = env['USER'];
env['JAVA_HOME'] = '/usr/lib/java';

PATH management tools:

Env().appendToPath('/home/me/bin');
Env().addToPATHIfAbsent('/home/me/bin');
Env().removeFromPATH('/home/me/bin');

And the ability to explore the Dart environment.

/// Get details of the current dart script that is running.
DartScript.current;
DartSdk().pathToDartExe;
PubCache().pathTo
PreviousIntroductionNextInstall DCli

Last updated 1 year ago

Was this helpful?

Dart has a large ecosystem of packages that you can use to extend the DCli such as the excellent package that lets you easily manipulate file paths.

You can explore the complete API .

paths
here