2021-10-30 10:19:16 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* IMPORTS
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
import (
|
2021-11-03 18:43:13 +01:00
|
|
|
"fmt"
|
|
|
|
|
2021-10-30 10:19:16 +02:00
|
|
|
"github.com/akamensky/argparse"
|
|
|
|
|
|
|
|
"ads/internal/types"
|
2021-11-03 18:43:13 +01:00
|
|
|
"ads/pkg/re"
|
2021-10-30 10:19:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* GLOBAL VARIABLES
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
var Parser *argparse.Parser
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* LOCAL VARIABLES / CONSTANTS
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
var optionsQuiet = argparse.Options{
|
|
|
|
Help: "Blendet alle Konsole-Messages aus.",
|
|
|
|
Required: false,
|
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:07:04 +01:00
|
|
|
var optionsChecks = argparse.Options{
|
|
|
|
Help: "Ob vor und nach Ausführung von Algorithmen Checks auf Inputs/Outputs ausgeführt werden sollen.",
|
|
|
|
Required: false,
|
|
|
|
// NOTE: no `Boolean` option available!
|
|
|
|
Default: "false",
|
|
|
|
}
|
|
|
|
|
2021-10-30 10:19:16 +02:00
|
|
|
var optionsDebug = argparse.Options{
|
|
|
|
Help: "Blendet die Debugging-Befehle ein.",
|
|
|
|
Required: false,
|
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:05:46 +01:00
|
|
|
var optionsInteractive = argparse.Options{
|
|
|
|
Help: "Startet die App im interaktiven Modus.",
|
|
|
|
Required: false,
|
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
|
2021-10-30 10:19:16 +02:00
|
|
|
var optionsColour = argparse.Options{
|
2021-11-02 09:07:04 +01:00
|
|
|
Help: "Ob Logging färblich angezeigt wird.",
|
2021-10-30 10:19:16 +02:00
|
|
|
Required: false,
|
|
|
|
// NOTE: no `Boolean` option available!
|
2021-11-04 07:57:22 +01:00
|
|
|
Default: "false",
|
2021-10-30 10:19:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var optionsConfigFile = argparse.Options{
|
|
|
|
Help: "Pfad zur Configdatei (nur für run Endpunkt).",
|
|
|
|
Required: false,
|
|
|
|
Default: "code/config.yml",
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHODS parse cli
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
2021-11-03 18:43:13 +01:00
|
|
|
// Parst cli flags.
|
2021-10-30 10:19:16 +02:00
|
|
|
func ParseCli(args []string) (*types.CliArguments, error) {
|
|
|
|
var err error
|
|
|
|
Parser = argparse.NewParser("cli parser", "Liest Optionen + Flags von Kommandozeile.")
|
|
|
|
arguments := types.CliArguments{
|
2021-11-05 08:51:11 +01:00
|
|
|
ModeHelp: Parser.NewCommand("help", "Hilfsanleitung anzeigen"),
|
|
|
|
ModeVersion: Parser.NewCommand("version", "Version anzeigen."),
|
|
|
|
ModeRun: Parser.NewCommand("run", "Algorithmen ausführen."),
|
2021-11-03 16:05:46 +01:00
|
|
|
Quiet: Parser.Flag("q", "quiet", &optionsQuiet),
|
|
|
|
Debug: Parser.Flag("", "debug", &optionsDebug),
|
|
|
|
Interactive: Parser.Flag("", "it", &optionsInteractive),
|
|
|
|
Checks: Parser.String("", "checks", &optionsChecks),
|
|
|
|
Colour: Parser.String("", "colour", &optionsColour),
|
|
|
|
ConfigFile: Parser.String("", "config", &optionsConfigFile),
|
2021-10-30 10:19:16 +02:00
|
|
|
}
|
|
|
|
err = Parser.Parse(args)
|
|
|
|
return &arguments, err
|
|
|
|
}
|
2021-11-03 18:43:13 +01:00
|
|
|
|
|
|
|
// Prüft, ob der Parser nur deshalb fehlschlägt, weil ein Command fehlt.
|
|
|
|
func ParseCliCommandMissing(err error) bool {
|
|
|
|
// FIXME: unschöne Lösung. Leider ist Error-Typ im Package versteckt
|
|
|
|
return re.Matches(`(?i)(command required)`, fmt.Sprintf("%v", err))
|
|
|
|
}
|