Skip to content

Commit 8933e2f

Browse files
committed
Implement command line parsing
1 parent 39c2160 commit 8933e2f

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

steamgrid.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55
import (
66
"bufio"
77
"errors"
8+
"flag"
89
"fmt"
910
"io/ioutil"
1011
"net/http"
@@ -33,16 +34,9 @@ func startApplication() {
3334
"Cover": []string{"p", ".p", "library_600x900_2x.jpg", "600", "900", "dimensions=600x900"},
3435
}
3536

36-
steamGridDBApiKey := "";
37-
38-
// Works for now, should be replaced by something better when more are added.
39-
// Maybe https://github.com/jessevdk/go-flags ?
40-
if len(os.Args) >= 3 {
41-
if os.Args[1] == "--steamgriddb" {
42-
steamGridDBApiKey = os.Args[2]
43-
fmt.Println("Support for SteamGridDB activated")
44-
}
45-
}
37+
steamGridDBApiKey := flag.String("steamgriddb", "", "Your personal SteamGridDB api key, get one here: https://www.steamgriddb.com/profile/preferences")
38+
steamDir := flag.String("steamdir", "", "Path to your steam installation")
39+
flag.Parse()
4640

4741
fmt.Println("Loading overlays...")
4842
overlays, err := LoadOverlays(filepath.Join(filepath.Dir(os.Args[0]), "overlays by category"), artStyles)
@@ -56,7 +50,7 @@ func startApplication() {
5650
}
5751

5852
fmt.Println("Looking for Steam directory...\nIf SteamGrid doesn´t find the directory automatically, launch it with an argument linking to the Steam directory.")
59-
installationDir, err := GetSteamInstallation()
53+
installationDir, err := GetSteamInstallation(*steamDir)
6054
if err != nil {
6155
errorAndExit(err)
6256
}
@@ -130,10 +124,10 @@ func startApplication() {
130124
// Download if missing.
131125
///////////////////////
132126
if game.ImageSource == "" {
133-
from, err := DownloadImage(gridDir, game, artStyle, artStyleExtensions, steamGridDBApiKey)
127+
from, err := DownloadImage(gridDir, game, artStyle, artStyleExtensions, *steamGridDBApiKey)
134128
if err != nil && err.Error() == "401" {
135129
// Wrong api key
136-
steamGridDBApiKey = ""
130+
*steamGridDBApiKey = ""
137131
fmt.Println("Api key rejected, disabling SteamGridDB.")
138132
} else if err != nil {
139133
fmt.Println(err.Error())

users.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,13 @@ func GetProfile(user User) (string, error) {
113113
// GetSteamInstallation Returns the Steam installation directory in Windows. Should work for
114114
// internationalized systems, 32 and 64 bits and users that moved their
115115
// ProgramFiles folder. If a folder is given by program parameter, uses that.
116-
func GetSteamInstallation() (path string, err error) {
117-
if len(os.Args) == 2 {
118-
argDir := os.Args[1]
119-
_, err := os.Stat(argDir)
116+
func GetSteamInstallation(steamDir string) (path string, err error) {
117+
if steamDir != "" {
118+
_, err := os.Stat(steamDir)
120119
if err == nil {
121-
return argDir, nil
120+
return steamDir, nil
122121
}
123-
return "", errors.New("Argument must be a valid Steam directory, or empty for auto detection. Got: " + argDir)
122+
return "", errors.New("Argument must be a valid Steam directory, or empty for auto detection. Got: " + steamDir)
124123
}
125124

126125
currentUser, err := user.Current()

0 commit comments

Comments
 (0)