From ee1688268d0653a60be050309e1b3796dfd86ed1 Mon Sep 17 00:00:00 2001 From: milarin Date: Fri, 7 Apr 2023 15:22:20 +0200 Subject: [PATCH] improved command line argument handling --- main.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 7b897fe..5731654 100644 --- a/main.go +++ b/main.go @@ -12,10 +12,7 @@ import ( "github.com/fatih/color" ) -var ( - FlagOutputFile = flag.String("o", "output.pdf", "output file name") - ColorRed = color.New(color.FgRed) -) +var ColorRed = color.New(color.FgRed) func main() { flag.Parse() @@ -25,12 +22,17 @@ func main() { os.Exit(1) } - inputFilePath := flag.Arg(0) - if inputFilePath == "" { - ColorRed.Fprintln(os.Stderr, "please provide an input file path") + if len(flag.Args()) == 0 { + ColorRed.Fprintln(os.Stderr, "please provide at least one input file path") os.Exit(1) } + for _, inputFilePath := range flag.Args() { + Convert(inputFilePath) + } +} + +func Convert(inputFilePath string) { inputFile, err := os.Open(inputFilePath) if err != nil { ColorRed.Fprintf(os.Stderr, "input file path could not be read: %s\n", err.Error()) @@ -77,7 +79,8 @@ func main() { tempFile.Close() } - args := append([]string{"--output", *FlagOutputFile}, tempFiles...) + outputFilePath := fmt.Sprintf("%s.pdf", filepath.Base(inputFilePath)[:len(inputFilePath)-len(filepath.Ext(inputFilePath))]) + args := append([]string{"--output", outputFilePath}, tempFiles...) img2pdf := exec.Command("img2pdf", args...) if err := img2pdf.Start(); err != nil { @@ -89,4 +92,6 @@ func main() { ColorRed.Fprintf(os.Stderr, "img2pdf returned an error: %s\n", err.Error()) os.Exit(1) } + + fmt.Println(outputFilePath) }