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{
|
2021-11-06 11:16:34 +01:00
|
|
|
Help: "Blendet Debugging-Befehle ein.",
|
2021-10-30 10:19:16 +02:00
|
|
|
Required: false,
|
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:05:46 +01:00
|
|
|
var optionsInteractive = argparse.Options{
|
2021-11-06 11:16:34 +01:00
|
|
|
Help: "Startet das Programm im interaktiven Modus.",
|
2021-11-03 16:05:46 +01:00
|
|
|
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
|
2021-11-07 18:17:31 +01:00
|
|
|
Parser = argparse.NewParser(
|
|
|
|
"cli parser",
|
|
|
|
"\033[93;2mEin Programm zur Ausführung verschiedener Algorithmen aus dem Kurs AlgoDat I.\033[0m",
|
|
|
|
)
|
2021-10-30 10:19:16 +02:00
|
|
|
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))
|
|
|
|
}
|