Async and Practical
Argument Parser Lite
Real CLI parsing uses a package, but a tiny --flag value reader is just a for-loop over an argument list. Each pair fills a Map<String, String>, then the program reads it with safe defaults.
Program
Play the program to parse a fixed argument list into a small options map.
argument_parser_lite.dart
Replay: real traced execution (multi-file project)
void main() {
var args = ['--name', 'Ada', '--count', '3'];
var options = <String, String>{};
for (var i = 0; i + 1 < args.length; i += 2) {
var key = args[i].substring(2);
options[key] = args[i + 1];
}
var name = options['name'] ?? '';
var count = int.parse(options['count'] ?? '0');
print('$name x$count');
}
args ← 4 tokens
1void main() {2 var args = ['--name', 'Ada', '--count', '3'];3 var options = <String, String>{};values this step4 tokensargsoptions ← {}
2var args = ['--name', 'Ada', '--count', '3'];3var options = <String, String>{};4for (var i = 0; i + 1 < args.length; i += 2) {values this step{}optionsloop ← parse pairs
3var options = <String, String>{};4for (var i = 0; i + 1 < args.length; i += 2) {5 var key = args[i].substring(2);values this stepparse pairsloopoptions ← {name: Ada, count: 3}
5 var key = args[i].substring(2);6 options[key] = args[i + 1];7}values this step{name: Ada, count: 3}optionsname ← Ada
7}8var name = options['name'] ?? '';9var count = int.parse(options['count'] ?? '0');values this stepAdanamecount ← 3
8var name = options['name'] ?? '';9var count = int.parse(options['count'] ?? '0');10print('$name x$count');values this step3countprint('$name x$count');
9 var count = int.parse(options['count'] ?? '0');10 print('$name x$count');11}outputAda x3values this stepAdaname3count
flag pairs
Each `--flag value` pair becomes one entry in the options map, keyed by the flag name.
substring(2)
`args[i].substring(2)` drops the leading `--` from each flag before storing it.
safe defaults
`options['count'] ?? '0'` falls back to a sensible string when the flag is missing.