Added 'disable color' flag ('-d') to disable color printing. Also replaced os.Args with flag.Args, to retrieve command-line arguments after stripping out flags

master
Aadhavan Srinivasan 2 months ago
parent 098f7fe01b
commit 0b67cb317e

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"flag"
"fmt" "fmt"
"os" "os"
"os/user" "os/user"
@ -60,6 +61,9 @@ func printFile(fileName string) {
} }
func main() { func main() {
disableColorFlag := flag.Bool("d", false, "Disable color")
flag.Parse()
// Check if config exists. If it doesn't, generate the config files. // Check if config exists. If it doesn't, generate the config files.
var configPath string // Location of config files, depends on OS var configPath string // Location of config files, depends on OS
if runningOnWindows() { if runningOnWindows() {
@ -76,17 +80,19 @@ func main() {
} }
// Check if user has provided a file name // Check if user has provided a file name
if len(os.Args) != 2 { if len(flag.Args()) < 1 {
printErrAndExit("Invalid number of arguments") printErrAndExit("No File specified")
} }
fileName := os.Args[1] fileName := flag.Args()[0]
// Check if file exists.
mustExist(fileName) mustExist(fileName)
extension := filepath.Ext(fileName) extension := filepath.Ext(fileName)
configExists, configFilename := getConfig(configPath, extension) configExists, configFilename := getConfig(configPath, extension)
// If the given file has no corresponding config, print it out // If the given file has no corresponding config, or if the 'disable color'
// and exit. // flag is set, print the file out and exit.
if configExists == false { if configExists == false || *disableColorFlag {
printFile(fileName) printFile(fileName)
return return
} }

Loading…
Cancel
Save