Skip to content

Commit cba89f5

Browse files
author
Florian Heinze
committed
BUGFIX: make cobra ignore flags for tasks
1 parent 512026c commit cba89f5

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ and other nifty feature ;)
2525
// Execute adds all child commands to the root command and sets flags appropriately.
2626
// This is called by main.main(). It only needs to happen once to the rootCmd.
2727
func Execute(version, commit string) {
28+
// The following lines remove -h and --help flags form the usages
29+
// RootCmd.InitDefaultHelpFlag()
30+
// RootCmd.Flags().MarkHidden("help")
31+
// This was initially needed because we DisableFlagParsing for the tasks.
32+
// Now we still DisableFlagParsing but also evaluate the args. If -h or --help
33+
// is the only arg we show the help of the command
34+
2835
// IMPORTANT: the order of tasks should resemble the order in the dev.sh
2936
cobra.EnableCommandSorting = false
3037
RootCmd.AddCommand(buildSectionCommand("your tasks"))

cmd/task.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"main/utils"
88
"os"
99
"path/filepath"
10+
"strings"
1011
"syscall"
1112
)
1213

@@ -17,8 +18,23 @@ func buildTaskCommand(task utils.DevScriptTask, devScriptPath string) *cobra.Com
1718
Short: color.Gray.Text(task.Short),
1819
Long: task.Long,
1920
Args: cobra.ArbitraryArgs,
21+
// If this is true all flags will be passed to the command as arguments.
22+
DisableFlagParsing: true,
23+
// Will disable the addition of [flags] to the usage
24+
// As we currently do not parse comments with a specific syntax to provide
25+
// useful information. The only flags we support are -h and --help they
26+
// are always part of the usage template.
27+
DisableFlagsInUseLine: true,
2028
Run: func(cmd *cobra.Command, args []string) {
21-
execDevScriptWithArguments(devScriptPath, append([]string{task.Usage}, args...))
29+
// We DisableFlagParsing but we still want to use the -h and --help flag.
30+
// This is why we evaluate the args manually and show the help of the
31+
// command if needed.
32+
argsAsString := strings.Join(args, " ")
33+
if argsAsString == "--help" || argsAsString == "-h" {
34+
cmd.Help()
35+
} else {
36+
execDevScriptWithArguments(devScriptPath, append([]string{task.Usage}, args...))
37+
}
2238
},
2339
}
2440
return cmd

dev.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ set -e
1111

1212
######### TASKS #########
1313

14-
# runs foo
15-
function foo() {
16-
echo "test"
17-
}
18-
19-
# use main aus binary
14+
# exposes ./main binary globally
2015
function switch-dev-binary() {
2116
echo "-----------> creating build"
2217
build
@@ -40,11 +35,12 @@ function restore-dev-binary() {
4035
echo "no /usr/local/bin/dev_back found. Nothing to restore!"
4136
fi
4237
}
43-
38+
# build
4439
function build() {
4540
go build main
4641
}
4742

43+
# install dev dependencies
4844
function setup() {
4945
# As the setup typically is more complex we recommend using a separate
5046
# file `dev_setup.sh`

0 commit comments

Comments
 (0)