Book Image

DART Essentials

Book Image

DART Essentials

Overview of this book

Table of Contents (16 chapters)
Dart Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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')
 ...