import 'dart:io'; List initializers = []; List finalizers = []; /// enablePrefixMatching allows for automatic prefix matching. /// Automatic prefix matching can be a dangerous thing to automatically enable in CLI tools. var enablePrefixMatching = false; /// enableCommandSorting controls sorting of the slice of commands, which is turned on by default. var enableCommandSorting = true; /// enableCaseInsensitive allows case-insensitve command names. (case sensitive by default) var enableCaseInsensitive = false; /// enableTraverseRunHooks executes persistent pre-run and post-run hooks from all parents. /// By default this is disabled, which means only the first run hook to be found is executed. var enableTraverseRunHooks = false; /// mousetrapHelpText enables an information splash screen on Windows /// if the CLI is started from explorer.exe. /// To disable the mousetrap, just set this variable to blank string (""). // Works only on Microsoft Windows. String mousetrapHelpText = """This is a command line tool. You need to open cmd.exe and run it from there. """; /// mousetrapDisplayDuration controls how long the mousetrapHelpText message is displayed on Windows /// if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed. /// To disable the mousetrap, just set mousetrapHelpText to a blank string (""). Duration mousetrapDisplayDuration = Duration(seconds: 5); /// onInitialize sets the passed functions to be run when each command's /// execute method is called. void onInitialize(List y) { initializers.addAll(y); } /// onFinalize sets the passed functions to be run when each command's /// execute method is terminated. void onFinalize(List y) { finalizers.addAll(y); } /// ld compares two strings and returns the levenshtein distance between them. int ld(String s, String t, bool ignoreCase) { if (ignoreCase) { s.toLowerCase(); t.toLowerCase(); } List> d = List.generate( s.length + 1, (_) => List.filled(t.length + 1, 0), ); for (int i = 0; i < d.length; i++) { d[i][0] = i; } for (int j = 0; j < d[0].length; j++) { d[0][j] = j; } for (int j = 1; j <= t.length; j++) { for (int i = 1; i <= s.length; i++) { if (s[i - 1] == t[j - 1]) { d[i][j] = d[i - 1][j - 1]; } else { int min = d[i - 1][j - 1]; if (d[i][j - 1] < min) { min = d[i][j - 1]; } if (d[i - 1][j - 1] < min) { min = d[i - 1][j - 1]; } d[i][j] = min + 1; } } } return d[s.length][t.length]; } /// checkError print the msg with the prefix 'Error:' and exits with error code 1. If the msg is null, it does nothing. void checkError(Object? msg) { if (msg != null) { stderr.write("Error: $msg"); exit(1); } } /// writeStringAndCheck writes a string into a buffer, and checks if the error is not null. void writeStringAndCheck(StringBuffer b, String s) { try { b.write(s); } catch (e) { checkError(e); } }