improved command line argument handling

This commit is contained in:
milarin 2023-04-07 15:22:20 +02:00
parent 03e6d79f3a
commit c2a36c3d31

20
main.go
View File

@ -12,10 +12,7 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
var ( var ColorRed = color.New(color.FgRed)
FlagOutputFile = flag.String("o", "output.pdf", "output file name")
ColorRed = color.New(color.FgRed)
)
func main() { func main() {
flag.Parse() flag.Parse()
@ -25,12 +22,17 @@ func main() {
os.Exit(1) os.Exit(1)
} }
inputFilePath := flag.Arg(0) if len(flag.Args()) == 0 {
if inputFilePath == "" { ColorRed.Fprintln(os.Stderr, "please provide at least one input file path")
ColorRed.Fprintln(os.Stderr, "please provide an input file path")
os.Exit(1) os.Exit(1)
} }
for _, inputFilePath := range flag.Args() {
Convert(inputFilePath)
}
}
func Convert(inputFilePath string) {
inputFile, err := os.Open(inputFilePath) inputFile, err := os.Open(inputFilePath)
if err != nil { if err != nil {
ColorRed.Fprintf(os.Stderr, "input file path could not be read: %s\n", err.Error()) ColorRed.Fprintf(os.Stderr, "input file path could not be read: %s\n", err.Error())
@ -77,7 +79,9 @@ func main() {
tempFile.Close() tempFile.Close()
} }
args := append([]string{"--output", *FlagOutputFile}, tempFiles...) outputFilePath := fmt.Sprintf("%s.pdf", filepath.Base(inputFilePath)[:len(inputFilePath)-len(filepath.Ext(inputFilePath))])
fmt.Println(outputFilePath)
args := append([]string{"--output", outputFilePath}, tempFiles...)
img2pdf := exec.Command("img2pdf", args...) img2pdf := exec.Command("img2pdf", args...)
if err := img2pdf.Start(); err != nil { if err := img2pdf.Start(); err != nil {