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

Was this helpful?

  1. Tour

Using DCli functions

PreviousOverviewNextUser input

Last updated 1 year ago

Was this helpful?

Using DCli functions

For complete API documentation refer to:

Let's start by looking at 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.

import 'package:path/path.dart';
import 'package:dcli/dcli.dart';

void main() {
    // Use the global DCli Settings to enable debug output.
    Settings().setVerbose(enabled: true);

    // Print the current working directory
    print('PWD: ${pwd}');

    // Create a directory and if necessary
    // its parent directories.
    var pathToImages = 'tools/images';
    createDir(pathToImages, recursive: true);

  
    var pathToGoodJpg = join(pathToImages, 'good.jpg');
    // create a file (it's empty)
    touch(pathToGoodJpg, create: true);

    // update the last modified time on an existing file
    touch(pathToGoodJpg);

    print('Showing all files');

    // print out all files in the current directory.
    // [file] is just a [String]
    find('*.*', recursive: false).forEach((file) => print(file));

    // take a nap for a couple of seconds.
    sleep(2);

    print('Find file matching *.jpg');
    // Find all files that end with .jpg
    // in the current directory and any subdirectories
    for (var file in find('*.jpg', workingDirectory: pathToImages).toList()) {
        print(file);
    }

    var pathToBadJpg = join(pathToImages, "bad.jpg");
    // Move/rename a file
    move(pathToGoodJpg, pathToBadJpg);

    // check if a file exists.
    if (exists(pathToBadJpg)) {
        print("bad.jpg exists");
    }

    // Delete a file asking the user first.
    delete(pathToBadJpg, ask: true);

}

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!

pub.dev