Projekt dgrep

Aus Info-Theke
Zur Navigation springen Zur Suche springen

Zielsetzung[Bearbeiten]

Es wir ein Kommandozeilenprogramm erstellt, das einen String, definiert mit einem regulären Ausdruck, in Dateien, evt. in 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").

Beschreibung[Bearbeiten]

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")

Kenntnisse[Bearbeiten]

  • Umgang mit Verzeichnissen und Dateien
  • Rekursiver Methodenaufruf
  • Unittests

Projekt erstellen[Bearbeiten]

  • IdeaIC starten
  • Menü: File - New - <Project>
  • 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
  • Finish-Button

Damit ist das Projekt names dgrep erstellt.

Projekt erkunden[Bearbeiten]

  • Linke Spalte: Projects anklicken
  • Es wird ein Ordner namens dgrep sichtbar
  • auf das Symbol ">" klicken, dann klappt das Verzeichnis aus
  • Im Unterverzeichnis bin liegt die Datei mit dem Hauptprogramm, dgrep.dart.
  • Herunterladen der Datei dgrep.v1.zip und diese im Projektverzeichnis (c:\dev\dgrep oder ~/dev/dgrep) entpacken.

dgrep.dart[Bearbeiten]

Wir tragen ein:

import 'package:dgrep/search_engine.dart';

void main(List<String> arguments) {
  SearchEngine.execute(arguments);
}
  • package:dgrep/search_engine.dart Wir importieren die Klasse SearchEngine.
  • SearchEngine.execute(arguments); Die Klasse SearchEngine besitzt eine statische Methode execute(), die als Parameter die Liste der Programmargument erhält und die Suche erledigt.

pubspec.yaml[Bearbeiten]

Wir verwenden zwei externe Pakete, path für 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:

name: dgrep
description: A program for searching regular expressions in files.
version: 0.1.0

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

Die Quellcodedateien[Bearbeiten]

Durch das obige Entpacken der Zip-Datei gibt es folgende Dateien im Verzeichnis lib:

Zu jeder obigen Datei gehört eine Datei, die die Unittests enthält. Per Konvention wird einfach ein _test an den Namen angehängt.

Programm compilieren[Bearbeiten]

Linux[Bearbeiten]

  • Terminal öffnen
gdev dsuche
dcompile dsuche
</code>
Danach gibt es die Datei <code>/usr/local/bin/dsuche</code>.
Dieses Programm kann in der Eingabeaufforderung gestartet werden.
<pre>dgrep -r main "~/dev/*.dart" -V2

Windows[Bearbeiten]

  • Eingabeaufforderung starten
  • gdev dgrep
  • dcompile dgrep

Danach gibt es die Datei c:\dev\bin\dgrep.exe Dieses Programm kann in der Eingabeaufforderung gestartet werden.

dgrep -r "main" "c:\dev\*.dart" -V2