add arguments parser

This commit is contained in:
rmanach 2022-04-12 11:07:30 +02:00
parent 5d50f308d9
commit a7776f01f6
2 changed files with 36 additions and 2 deletions

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/akamensky/argparse v1.3.1 h1:kP6+OyvR0fuBH6UhbE6yh/nskrDEIQgEA1SUXDPjx4g=
github.com/akamensky/argparse v1.3.1/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=

36
main.go
View File

@ -1,9 +1,41 @@
package main
import (
"github.com/akamensky/argparse"
"log"
"os"
"fmt"
)
func main() {
log.Printf("packer")
// parseArguments - get needed arguments to launch the `packer`
// returns 3 strings :
// * regex
// * outputPath
// * directory
func parseArguments() (string, string, string, error) {
parser := argparse.NewParser("packer", "packer - zip file where filenames match with a regex")
var err error
regex := parser.String("r", "regex", &argparse.Options{Required: true, Help: "filename Regex"})
outputPath := parser.String("o", "output-path", &argparse.Options{Required: true, Help: "zip output path (including zip name)"})
directory := parser.String("d", "directory", &argparse.Options{Required: false, Help: "root directory to check files", Default: "."})
parseError := parser.Parse(os.Args)
if parseError != nil {
err = fmt.Errorf("parsing error: %s", parseError)
}
return *regex, *outputPath, *directory, err
}
func main() {
regex, directory, outputPath, err := parseArguments()
if err != nil {
log.Fatal("error while parsing args : ", err)
}
log.Printf("looking for file '%s' in '%s' ...", regex, directory)
log.Printf("files zipped in %s", outputPath)
}