improved command line argument handling
This commit is contained in:
parent
03e6d79f3a
commit
ee1688268d
21
main.go
21
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user