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
  • Running
  • Compiling
  • Dependencies
  • Writing a DCli script

Was this helpful?

Writing your first CLI app

Let's start by going over the basics of writing the classic Hello World program.

Create a directory to work in:

mkdir dcli_scripts
cd dcli_scripts
mkdir dcli_scripts
cd dcli_scripts
mkdir dcli_scripts
cd dcli_scripts

Using your preferred editor create a file called 'hello.dart' with following content:

void main() {
    print('Hello World.');
}

If you are using vscode you can simply type code hello.dart to edit the file.

Running

Now let's run your script:

dart hello.dart
> Hello World.

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.

dart compile exe hello.dart
Generated: hello.exe
./hello.exe
> Hello World

ll hello.exe
-rwxrwxr-x 1 5,875,400 Sep  5 14:30 hello.exe*

You now have a completely self-contained executable which you can copy to any binary compatible machine.

The exe is 8MB 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 set up dependency management so we can use the DCli API and any other Dart package.

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 your 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:

name: hello_world
description: My first app that does bugger all.

dependencies:
  dcli: ^7.0.0  # update the version no. to the latest; https://pub.dev/packages/dcli

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 'dart pub get' to download the required dependencies:

dart pub get
Resolving dependencies... 
Got dependencies!

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:

import 'package:dcli/dcli.dart';

void main() {
    print("Now let's do someting useful.");

    var username =  ask( 'username:');
    print('username: $username');

    var password = ask( 'password:', hidden: true);
    print('password: $password');

    // create a directory
    if (!exists('tmp')) {
        createDir('tmp');
    }

    // Truncate any existing content
    // of the file 'tmp/text.txt' and write
    // 'Hello world' to the file.
    'tmp/text.txt'.write('Hello world');

    // append 'My second line' to the file 'tmp/text.txt'.
    'tmp/text.txt'.append('My second line');

    // and another append to the same file.
    'tmp/text.txt'.append('My third line');

    // now copy the file tmp/text.txt to second.txt
    copy('tmp/text.txt', 'tmp/second.txt', overwrite: true);

    // lets dump the file we just created to the console
    read('tmp/second.txt').forEach((line) => print(line));

    //let's prove that both files exist by running
    // a recursive find.
    find('*.txt').forEach((file) => print('Found $file'));

    // Now let's tail the file using the OS tail command.
    // DCli uses Dart extensions on the String class allowing us to
    // treat a string
    // as an OS command and `.run` that command as 
    // a child process.
    // Stdout and stderr output are written
    // directly to the console.
    'tail tmp/text.txt'.run;

    //Let's do a word count capturing stdout,
    // stderr will will be swallowed.
    'wc tmp/second.txt'.forEach((line) => print('Captured $line'));

    if (confirm( "Should I delete 'tmp'? (y/n):")) {
        // Now let's clean up
        delete('tmp/text.txt');
        delete('tmp/second.txt');
        deleteDir('tmp');
    }

}

Now run our script.

dart hello.dart
Now let's do someting useful.
username: auser
username: auser
password: *********
password: apassword
Hello world
My second line
My third line
Found /tmp/fred/tmp/second.txt
Found /tmp/fred/tmp/text.txt
Hello world
My second line
My third line
Captured  3  8 41 tmp/second.txt
Should I delete 'tmp'? (y/n): (y/n): y

You are now officially a DCli guru.

Go forth young man (or gal) and create.

PreviousInstalling on WindowsNextAdd DCli to your project

Last updated 10 days ago

Was this helpful?

The allow you to run your app without compiling and without prefixing it with dart.

Search for third party package. You can only use packages labeled 'DART | NATIVE'

Check for the latest version no. of DCli.

DCli tools
pub.dev
pub.dev