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

Command Line Arguments

A CLI app is only so useful, unless you can pass arguments to your app.

Use dcli create --template=full myproject to create an example cli app that demonstrates best practices when parsing arguments.

Like many languages Dart allows you to pass arguments to your main method..

void main(List<String> args)
{
    print('Found ${arg.length} arguments.');

    print('The arguments are:');

    for (int i = 0; i < args.lenght; i++) {
        print('arg[$i]=${args[i]}');
    }
}

If you DCli script is called test.dart:

dart test.dart one two three
> Found 3 arguments.
> The arguments are:
> arg[0] = one
> arg[1] = two
> arg[2] = three

You can also stop your app and return an exit code using the exit method.

import 'dart:io';

void main(List<String> args)
{
    print('Found ${arg.length} arguments.');

    /// stops the progam so no further lines will be executed.
    /// The progam outputs an exit code of 1.
    exit(1);

    print('The arguments are:');

    for (int i = 0; i < args.lenght; i++) {
        print('arg[$i]=${args[i]}');
    }
}

ArgParser

For simple command argument processing you can process the args argument yourself.

If you want to do more complex argument processing then its better to get some help.

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

/// This is a full implementation of the linux cli 'which' app.
/// The which command searches the PATH for the passed exe.
void main(List<String> args) {

  /// create the parser and add a --verbose option
  var parser = ArgParser();
  parser..addFlag('verbose', abbr: 'v', defaultsTo: false, negatable: false);

  /// parse the passed in command line arguments.
  var results = parser.parse(args);

  /// get the value of the passed in verbose flag.
  var verbose = results['verbose'] as bool;

  /// The 'rest' of the results are any additional arguments
  /// we only expect one which is the name of the exe we are looking for.
  if (results.rest.length != 1) {
    print(red('You must pass the name of the executable to search for.'));
    print(green('Usage:'));
    print(green('   which ${parser.usage}<exe>'));
    exit(1);
  }

  /// name of the command we will search for.
  var command = results.rest[0];
  var home = env['HOME'];

  List<String> paths = Env().path;

  for (var path in paths) {
    if (path.startsWith('~')) {
      path = path.replaceAll('~', home);
    }
    if (verbose) {
      print('Searching: ${canonicalize(path)}');
    }
    if (exists(join(path, command))) {
      print(red('Found at: ${canonicalize(join(path, command))}'));
    }
  }
}

To use the above application:

dart which.dart ls
Found at: /usr/bin/ls
Found at: /bin/ls
PreviousRedirecting outputNextPaths

Last updated 2 years ago

Was this helpful?

The Dart team has very kindly put together the package which provides advanced argument parsing. The DCli API includes the args package so you do NOT need to add it to your pubspec.yaml dependencies.

You can read all about using the package on but here is a little example of what you can do:

args
args
pub.dev