improved command line argument handling
This commit is contained in:
parent
03e6d79f3a
commit
c2a36c3d31
20
main.go
20
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,9 @@ func main() {
|
||||
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...)
|
||||
|
||||
if err := img2pdf.Start(); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user