A command-line tool should be useful in a script as well as in a terminal. Keep normal output on stdout, diagnostics on stderr and the exit code meaningful. Avoid prompting for input when a command is expected to run unattended.
A complete local entry point
Save this as bin/batch.dart in a Dart console package:
import 'dart:io';
void main(List<String> arguments) {
if (arguments.length != 1) {
stderr.writeln('Usage: dart run bin/batch.dart <positive-count>');
exitCode = 64;
return;
}
final count = int.tryParse(arguments.single);
if (count == null || count < 1) {
stderr.writeln('Count must be a positive integer.');
exitCode = 64;
return;
}
stdout.writeln('Prepared $count labels.');
}
Run dart run bin/batch.dart 3. Then run the command with no argument and with a non-number. Confirm both the message and exit status. With several flags, use a suitable argument parser instead of inventing ambiguous string splitting.
Separate input from business behavior
The entry point translates external strings into a typed request. A separate function should perform the calculation or export so a unit test does not need to simulate a terminal. If interactive input is genuinely needed, handle end-of-input and cancellation; do not assume stdin always returns a line.
For file output, define whether an existing destination may be overwritten. Prefer an explicit output option over silently writing into the SDK or the repository root. Keep secrets out of command-line arguments that may appear in process listings.
Package the tool deliberately
Use dart run while developing. A compiled executable targets its build platform and architecture. A Windows binary is not a Linux deployment artifact. A distributable Dart package should declare executables in its pubspec and document the supported SDK/platform boundary.
Exercise
Add a --format=json option and write a deterministic result to stdout. Verify that invalid input produces only a diagnostic on stderr and a nonzero exit code. Use the
expense report project
to practise processing a real input file.