diff --git a/go.mod b/go.mod index 857cb8a..b3cfbba 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.15 require ( github.com/markbates/pkger v0.17.1 - github.com/spf13/pflag v1.0.5 + github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c github.com/tdewolff/canvas v0.0.0-20201021153214-d9228b138ea8 golang.org/x/text v0.3.3 // indirect ) diff --git a/go.sum b/go.sum index cbf8ae7..6ca0226 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qq github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c h1:zqmyTlQyufRC65JnImJ6H1Sf7BDj8bG31EV919NVEQc= +github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/marianne.go b/marianne.go index ed8e3b1..15c92b6 100644 --- a/marianne.go +++ b/marianne.go @@ -289,42 +289,16 @@ func writeImages(c *canvas.Canvas, zp, formats string) { } -var traductions = map[string]string{ - "default": "par défaut", - "strings": " ", - "string": " ", - "ints": " ", - // "bad flag syntax:": "mauvaise syntaxe du paramètre :", - // "unknown flag:": "paramètre inconnu :", - // "unknown shorthand flag:": "paramètre court inconnu :", - // " in ": " dans ", - // "flag needs an argument:": "Le paramètre nécessite d'un argument :", -} - // Affiche l'aide d'utilisation // C'est un peut plus compliqué que ce que ça devrait être // car on doit remplacer "default" avec "par défaut" // voir : https://github.com/golang/go/issues/42124 func Aide() { - - fmt.Fprintf(os.Stderr, "marianne (version: %s)\n\n", version) - fmt.Fprintf(os.Stderr, "Ce programme génère le logo de l'institution.\nParamètres disponibles:\n\n") - - // Remplace "default" avec "par défaut" dans `flag.PrintDefaults` - var buf = new(bytes.Buffer) - // on redirige la sortie vers buf et on affiche les flags dedans - flag.CommandLine.SetOutput(buf) - defer flag.CommandLine.SetOutput(os.Stderr) + var out = flag.CommandLine.Output() + fmt.Fprintf(out, "marianne (version: %s)\n\n", version) + fmt.Fprintf(out, "Ce programme génère le logo de l'institution.\nParamètres disponibles:\n\n") flag.PrintDefaults() - // le message avant les ajustements - msg := string(buf.Bytes()) - // traduction en français - for from, to := range traductions { - msg = strings.ReplaceAll(msg, from, to) - } - // on affiche finalement le message d'aide - fmt.Fprintf(os.Stderr, msg) - fmt.Fprintf(os.Stderr, "\n") + fmt.Fprintf(out, "\n") } func main() { @@ -342,13 +316,21 @@ func main() { flag.BoolVarP(&aide, "aide", "h", false, "Imprime ce message d'aide.") // Message d'aide flag.CommandLine.SortFlags = false + flag.CommandLine.SetOutput(FrenchTranslator{flag.CommandLine.Output()}) flag.Usage = Aide + flag.CommandLine.Init("marianne", flag.ContinueOnError) + // récupère les flags - flag.Parse() + err = flag.CommandLine.Parse(os.Args[1:]) // affiche l'aide si demandé - if aide { + if aide || err != nil { flag.Usage() - os.Exit(0) + if err != nil { + fmt.Fprintln(flag.CommandLine.Output(), "ERREUR : ", err) + os.Exit(2) + } else { + os.Exit(0) + } } // au moins une des versions doit être présente (avec marges par défaut) if !sansMarges { diff --git a/translate.go b/translate.go new file mode 100644 index 0000000..54fb0a4 --- /dev/null +++ b/translate.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "io" + "strings" +) + +type trad struct { + from, to string +} + +var traductions = []trad{ + {"default", "par défaut"}, + {"strings", " "}, + {"string", " "}, + {"ints", " "}, + {"bad flag syntax:", "mauvaise syntaxe du paramètre :"}, + {"unknown flag:", "paramètre inconnu :"}, + {"unknown shorthand flag:", "paramètre court inconnu :"}, + {"flag needs an argument:", "Le paramètre nécessite d'un argument :"}, + {" in ", " dans "}, +} + +type FrenchTranslator struct { + w io.Writer +} + +func (fw FrenchTranslator) Write(p []byte) (n int, err error) { + out := string(p) + + for _, tr := range traductions { + out = strings.ReplaceAll(out, tr.from, tr.to) + } + + fmt.Fprintf(fw.w, out) + return len(p), nil +}