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
This commit is contained in:
18
main.go
18
main.go
@@ -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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user