Skip to content

Commit 804ab40

Browse files
MacherelRaknysh
andauthored
Added default values handling for custom CLI flags (#1379)
Co-authored-by: Andriy Knysh <[email protected]>
1 parent aee2b7d commit 804ab40

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

atmos.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,10 @@ docs:
402402
show_outputs: true
403403
sort_by: "name"
404404
hide_empty: false
405-
indent_level: 2
405+
indent_level: 2
406406

407407
version:
408408
check:
409409
enabled: true
410410
timeout: 1000 # ms
411411
frequency: 1h
412-

cmd/cmd_utils.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"strconv"
910
"strings"
1011
"time"
1112

@@ -91,16 +92,28 @@ func processCustomCommands(
9192
// Process and add flags to the command
9293
for _, flag := range commandConfig.Flags {
9394
if flag.Type == "bool" {
95+
defaultVal := false
96+
if flag.Default != "" {
97+
// Accept "true"/"false" as string for bool default
98+
parsed, err := strconv.ParseBool(flag.Default)
99+
if err == nil {
100+
defaultVal = parsed
101+
}
102+
}
94103
if flag.Shorthand != "" {
95-
customCommand.PersistentFlags().BoolP(flag.Name, flag.Shorthand, false, flag.Usage)
104+
customCommand.PersistentFlags().BoolP(flag.Name, flag.Shorthand, defaultVal, flag.Usage)
96105
} else {
97-
customCommand.PersistentFlags().Bool(flag.Name, false, flag.Usage)
106+
customCommand.PersistentFlags().Bool(flag.Name, defaultVal, flag.Usage)
98107
}
99108
} else {
109+
defaultVal := ""
110+
if flag.Default != "" {
111+
defaultVal = flag.Default
112+
}
100113
if flag.Shorthand != "" {
101-
customCommand.PersistentFlags().StringP(flag.Name, flag.Shorthand, "", flag.Usage)
114+
customCommand.PersistentFlags().StringP(flag.Name, flag.Shorthand, defaultVal, flag.Usage)
102115
} else {
103-
customCommand.PersistentFlags().String(flag.Name, "", flag.Usage)
116+
customCommand.PersistentFlags().String(flag.Name, defaultVal, flag.Usage)
104117
}
105118
}
106119

pkg/schema/schema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ type CommandFlag struct {
622622
Description string `yaml:"description" json:"description" mapstructure:"description"`
623623
Usage string `yaml:"usage" json:"usage" mapstructure:"usage"`
624624
Required bool `yaml:"required" json:"required" mapstructure:"required"`
625+
Default string `yaml:"default" json:"default" mapstructure:"default"`
625626
}
626627

627628
type CommandEnv struct {

0 commit comments

Comments
 (0)