Projekt dgrep - flutter-tutorial-de/dart-programming GitHub Wiki
Es wir ein Kommandozeilenprogramm erstellt, das einen String, definiert mit einem regulären Ausdruck, in Dateien oder gar mehreren Dateibäumen, sucht.
Mit zahlreichen Optionen kann die Suche eingegrenzt und die Ausgabe spezifiziert werden.
Das Programm hat eine Erklärfunktion (Aufruf mit Option "--help" oder "-h").
Usage: dgrep [<options>] <pattern> [<file1> [<file2> ...]] Searches <pattern> in <file1> <file2>... <fileN>: a directory or a shell file pattern like '*.txt' or both like /home/*.txt <option>: -c, --count Show only the count of lines with hits (per file) -i, --ignore-case The search is case insensitive -v, --invert-match Show lines that do not contain the pattern --process-binaries Normally binary files will not be processed -h, --help Show the command description -l, --list Show the filename only, not the matching lines -r, --recursive Searches in all sub directories too -w, --word The pattern describes a word: a hit will be delimited by word boundaries -x, --excluded A regular expression for files to skip, e.g. ".*.(bak|sic)" -X, --excluded-dirs A regular expression for directory to skip, e.g. "test|.git|.config -f, --format Describes the format of a found line The format is a string with placeholders. Example: The name of the file is "/doc/data.txt", the search pattern is "(\w+).* (\d+\.\d\d) " the found line is "apples: 20 kg 20.00 EUR" %f% full name of the file, e.g. "/doc/data.txt" %p% the path of the file, e.g. "/doc" %n% the node, e.g. "data.txt" %F% the filename, e.g. "data" %e% the extension, e.g. ".txt" %#% the line number %h% the hit, e.g. ": 20 " %c% the count of hit lines. Only meaningful together with option "--count" %l% the line with the hit, e.g. "apples: 20 kg 20.00 EUR" %1% the first group, e.g. "apple" %2% the 2.nd group, e.g. "20.00" ... %9% the 9.th group, e.g. "" (not found) A meaningful format of the above example: "%f%: product: %1% price: %2%" -F, --format-context Describes the format of a context line (see above-context...) The format is a string with placeholders like the option --format. -a, --above-context That number of lines above the hit line will be showed (defaults to "0") -C, --context That number of lines above and below the hit line will be showed (defaults to "0") -b, --below-context That number of lines below the hit line will be showed (defaults to "0") --break-lines Stops the search in a file if that number of matching lines is reached --exit-lines Stops the search if that number of matching lines is reached (over all files) --exit-files Stops the search if that number of files with matching lines is reached -V, --verbose-level 0: no additional info 1: summary 2: detail 3: loop 4: fine (defaults to "0")
- Umgang mit Verzeichnissen und Dateien
- Rekursiver Methodenaufruf
- Unittests
- IdeaIC starten
- Menü:
File
-New
- - Linke Spalte:
Dart
- Dart-SDK:
- Windows: c:\dart-sdk
- Linux: ~/dart-sdk
- Es erscheint eine Listbox, dort
Console-Application a command-line application sample
anklicken. -
Next
-Button - Project name:
dgrep
- Project loction:
- Linux:
~/dev/dgrep
- Windows:
c:\dev\dgrep
- Linux:
-
Finish
-Button
dgrep
erstellt.
- Linke Spalte:
Projects
anklicken. - Es wird ein Ordner namens
dgrep
sichtbar. - Auf das Symbol ">" klicken, daraufhin klappt das Verzeichnis aus.
- Im Unterverzeichnis
bin
liegt die Datei mit dem Hauptprogrammdgrep.dart
. - Herunterladen der Datei dgrep.v1.zip und diese im Projektverzeichnis (
c:\dev\dgrep
oder~/dev/dgrep
) entpacken.
Wir tragen ein:
void main(List<String> arguments) { SearchEngine.execute(arguments); }
-
package:dgrep/search_engine.dart
Wir importieren die KlasseSearchEngine
. -
SearchEngine.execute(arguments);
Die KlasseSearchEngine
besitzt eine statische Methodeexecute()
, die als Parameter die Liste der Programmargumente enthält und die Suche erledigt.
Wir verwenden zwei externe Pakete, nämlich path
für die Behandlung von Dateinamen und args
für die Auswertung von Programmargumenten.
Dazu kommt das externe Paket für Unittests test
und die Unterstützung für die Quellcodeanalyse pedantic
.
Die Datei pubspec.yaml
muss deshalb so aussehen:
environment: sdk: '>=2.10.0 <3.0.0' #dependencies: args: ^1.6.0 path: ^1.7.0 dev_dependencies: pedantic: ^1.9.0 test: ^1.15.7
Durch das obige Entpacken der Zip-Datei gibt es folgende Dateien im Verzeichnis lib
:
- Die Datei helper.dart: Kleine Hilfsfunktionen, die sonst nirgends hin passen.
- Die Datei search_engine.dart: Die Klasse
SearchEngine
, die die Suche organisiert.
_test
an den Namen angehängt.
- Die Datei search_engine_test.dart: Testet die Klasse
SearchEngine
. - Die Datei helper_test.dart: Testet die Funktionen.
- Terminal öffnen
gdev dgrep dcompile dgrepDanach gibt es die Datei
/usr/local/bin/dgrep
.
Dieses Programm kann in der Eingabeaufforderung gestartet werden.
dgrep -r main "~/dev/*.dart" -V2
- Eingabeaufforderung starten
gdev dgrep
dcompile dgrep
c:\dev\bin\dgrep.exe
Dieses Programm kann in der Eingabeaufforderung gestartet werden.
dgrep -r "main" "c:\dev\*.dart" -V2