Fix build flag parsing

This commit is contained in:
adnano 2021-04-20 16:12:52 -04:00
parent 0109a33b11
commit 133b371e4c
2 changed files with 11 additions and 12 deletions

View file

@ -30,7 +30,7 @@ kiln - a simple static site generator
Specifies the configuration file to use. Defaults to "config.toml". Specifies the configuration file to use. Defaults to "config.toml".
*-t* _task_ *-t* _task_
Specifies the task to run. Defaults to "all", which runs all tasks. Specifies the task to run. If unspecified, all tasks will be run.
# OVERVIEW # OVERVIEW

13
main.go
View file

@ -45,9 +45,10 @@ func build() {
config string config string
) )
flag.StringVar(&task, "t", "all", "the task to run") flags := flag.NewFlagSet("kiln build", flag.ExitOnError)
flag.StringVar(&config, "c", "config.toml", "the configuration file to use") flags.StringVar(&task, "t", "", "the task to run")
flag.Parse() flags.StringVar(&config, "c", "config.toml", "the configuration file to use")
flags.Parse(os.Args[2:])
// Load config // Load config
cfg, err := LoadConfig(config) cfg, err := LoadConfig(config)
@ -64,17 +65,15 @@ func build() {
} }
func run(cfg *Config, taskName string) error { func run(cfg *Config, taskName string) error {
switch taskName { if taskName == "" {
case "all":
return runAll(cfg) return runAll(cfg)
default: }
task, ok := cfg.Tasks[taskName] task, ok := cfg.Tasks[taskName]
if !ok { if !ok {
return fmt.Errorf("run task %q: no such task", taskName) return fmt.Errorf("run task %q: no such task", taskName)
} }
return runTask(cfg, task) return runTask(cfg, task)
} }
}
func runAll(cfg *Config) error { func runAll(cfg *Config) error {
for _, task := range cfg.Tasks { for _, task := range cfg.Tasks {