Book Image

DART Essentials

Book Image

DART Essentials

Overview of this book

This book is targeted at expert programmers in JavaScript who want to learn Dart quickly. Some previous experience with OOP programming in other languages and a good knowledge of JavaScript are assumed.
Table of Contents (11 chapters)
10
Index

Parsing CLI arguments with the args package


As we're going to write a WebSockets server, we might want to control it with some options passed from the command line, for example, a port number where the WebSockets server is listening or a command to stop the server when running in the background.

Let's create a new project with the Console Application template, add the args package to its dependencies, and start writing the WebSockets sever by defining its CLI options.

We'll create an instance of ArgParser and define possible options with the addOption() and addFlag()methods. Flags are options that can only have true/false values:

// bin/server.dart
import 'package:args/args.dart';
import 'dart:io';

main(List<String> args) async {
  ArgParser parser = new ArgParser();
  
  // This is probably self-explanatory.
  parser
    ..addOption('port', abbr: 'p', defaultsTo: '8888')
    ..addOption('pid-file', defaultsTo: 'var/websockets.pid',
        help: 'Path for a file with process ID')
 ...