1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 13:30:26 +00:00
mycorrhiza/flag.go

59 lines
1.2 KiB
Go
Raw Normal View History

2020-10-25 15:06:51 +00:00
package main
import (
_ "embed"
2020-10-25 15:06:51 +00:00
"flag"
2021-03-09 14:27:14 +00:00
"fmt"
"github.com/bouncepaw/mycorrhiza/cfg"
2020-10-25 15:06:51 +00:00
"log"
2021-03-09 14:27:14 +00:00
"os"
2020-10-25 15:06:51 +00:00
"path/filepath"
)
2021-05-11 08:33:00 +00:00
// CLI options are read and parsed here.
//go:embed assets/config.ini
var defaultConfig []byte
2021-03-09 14:27:14 +00:00
var printExampleConfig bool
2020-10-25 15:06:51 +00:00
func init() {
flag.StringVar(&cfg.ConfigFilePath, "config-path", "", "Path to a configuration file. Leave empty if you don't want to use it.")
2021-03-09 14:27:14 +00:00
flag.BoolVar(&printExampleConfig, "print-example-config", false, "If true, print an example configuration file contents and exit. You can save the output to a file and base your own configuration on it.")
2021-05-11 08:33:00 +00:00
flag.Usage = printHelp
}
// printHelp prints the help message.
2021-05-11 08:33:00 +00:00
func printHelp() {
_, err := fmt.Fprintf(
flag.CommandLine.Output(),
"Usage of %s:\n",
2021-05-11 08:33:00 +00:00
os.Args[0],
)
if err != nil {
log.Fatal(err)
2021-03-09 14:27:14 +00:00
}
2021-05-11 08:33:00 +00:00
flag.PrintDefaults()
2020-10-25 15:06:51 +00:00
}
2021-05-11 08:33:00 +00:00
// parseCliArgs parses CLI options and sets several important global variables. Call it early.
2020-10-25 15:06:51 +00:00
func parseCliArgs() {
flag.Parse()
args := flag.Args()
2021-03-09 14:27:14 +00:00
if printExampleConfig {
os.Stdout.Write(defaultConfig)
2021-03-09 14:27:14 +00:00
os.Exit(0)
}
2020-10-25 15:06:51 +00:00
if len(args) == 0 {
log.Fatal("Error: pass a wiki directory")
}
wikiDir, err := filepath.Abs(args[0])
cfg.WikiDir = wikiDir
2020-10-25 15:06:51 +00:00
if err != nil {
log.Fatal(err)
}
}