-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default command #823
Comments
I solved adding a cmd, _, err := rootCmd.Find(os.Args[1:])
if err != nil || cmd == nil {
// Not found
args := append([]string{"Item1"}, os.Args[1:]...)
rootCmd.SetArgs(args)
} |
@rmasci , @GustavoKatel , you might find #725 useful. |
Thank You!!
…On Tue, Mar 5, 2019 at 8:40 AM Gustavo Sampaio ***@***.***> wrote:
I solved adding a find operation and changing the default args before
executing the root command.
Something like this:
cmd, _, err := rootCmd.Find(os.Args[1:])if err != nil || cmd == nil {
// Not found
args := append([]string{"Item1"}, os.Args[1:]...)
rootCmd.SetArgs(args)
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#823 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AA0UPYMaFEgiKrq5nXrCig8YCTPkb0Izks5vTnPrgaJpZM4a05Ov>
.
|
@jharshman, since there is no intuitive one-liner solution to this question, I think it might be worth keeping this issue open. Although the solution in #725 works, it is not clean and it is not properly integrated in the codebase. Therefore, I think this can be kept open until some equivalent to |
just a quick bug fix (should be checking if func Execute() {
cmd, _, err := rootCmd.Find(os.Args[1:])
// default cmd if no cmd is given
if err != nil || cmd.Args == nil {
args := append([]string{"<your-default-command>"}, os.Args[1:]...)
rootCmd.SetArgs(args)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} |
I still get an error - now its always the default |
Ideally, this would be more tightly integrated into the API so that help text will still give you information about e.g. the other commands that are possible instead of dumping the help text for the subcommand (the default behaviour when using one of these fixes without supplying a subcommand) and make it more clear that it's running a de-facto subcommand. Perhaps
Not sure this is optimal, but some way to make it less confusing would be nice. Edit: func subCommands() (commandNames []string) {
for _, command := range cmd.Commands() {
commandNames = append(commandNames, append(command.Aliases, command.Name())...)
}
return
}
func setDefaultCommandIfNonePresent() {
if len(os.Args) > 1 {
potentialCommand := os.Args[1]
for _, command := range subCommands() {
if command == potentialCommand {
return
}
}
os.Args = append([]string{os.Args[0], "<default subcommand>"}, os.Args[1:]...)
}
}
func main() {
setDefaultCommandIfNonePresent()
if err := cmd.Execute(); err != nil {
zap.S().Error(err)
os.Exit(1)
}
} The difference here is that it checks if |
This issue is being marked as stale due to a long period of inactivity |
try this var rootCmd = &cobra.Command{
Use: "root",
}
func Execute() {
cmd, _, err := rootCmd.Find(os.Args[1:])
// default cmd if no cmd is given
if err == nil && cmd.Use == rootCmd.Use {
args := append([]string{defaultCmd.Use}, os.Args[1:]...)
rootCmd.SetArgs(args)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
|
better to keep default help behaviour with this code: cmd, _, err := rootCmd.Find(os.Args[1:])
// default cmd if no cmd is given
if err == nil && cmd.Use == rootCmd.Use && cmd.Flags().Parse(os.Args[1:]) != pflag.ErrHelp {
args := append([]string{defaultCmd.Use}, os.Args[1:]...)
rootCmd.SetArgs(args)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
} |
Latest solve for this is the following: main.go
root.go
|
I'm trying to clean up the backlog and wanted to check on a proposed solution. Walking through the code it just seems like this is a bug unique to the root command due to how it tries to Execute only from that node. It tries to find a subcommand and if it doesn't find one it errors. It seems like the solution would be just to treat that as a bug. Would it be an acceptable solution to simply fix the code to allow setting the same
In this case the root command behaves the same way as the child command. |
I think the change is really narrowly scoped if we approach it this way. This section of code finds the command being invoked, returns a 'not found' error causing the execution to stop. Since the rootCmd has a RunE though it should have been identified via |
Hi, I fixed this using var rootCmd = &cobra.Command{
Use: "myapp",
Short: "MyApp",
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
childCmd.Run(cmd, args) // <-- Here I'm invoking the default command
},
} This is the better approach or what can I do? this works, but I don't know side effects (if exists) |
If I understand cobra correctly, then this won't run any of the pre and post hooks of your subcommand. If you don't use them, then your example should be fine. |
Prior this commit, the bootc-image-builder container image had a custom entrypoint that hardcoded the use of the build subcommand. This meant that if a user wanted to use a different subcommand, they had to overwrite the entrypoint. This commit changes the cobra code in bib to fallback to build if no subcommand was given. This is slighly ugly, but it allows us to remove the custom entrypoint, streamlining the use of subcommands. Let's see an example of calling the version subcommand: Before: podman run --rm -it --entrypoint=/usr/bin/bootc-image-builder \ quay.io/centos-bootc/bootc-image-builder:latest version After: sudo podman run --rm -it \ quay.io/centos-bootc/bootc-image-builder:latest version Kudos to https://github.com/IKukhta for his code from spf13/cobra#823 (comment)
Prior this commit, the bootc-image-builder container image had a custom entrypoint that hardcoded the use of the build subcommand. This meant that if a user wanted to use a different subcommand, they had to overwrite the entrypoint. This commit changes the cobra code in bib to fallback to build if no subcommand was given. This is slighly ugly, but it allows us to remove the custom entrypoint, streamlining the use of subcommands. Let's see an example of calling the version subcommand: Before: podman run --rm -it --entrypoint=/usr/bin/bootc-image-builder \ quay.io/centos-bootc/bootc-image-builder:latest version After: sudo podman run --rm -it \ quay.io/centos-bootc/bootc-image-builder:latest version Kudos to https://github.com/IKukhta for his code from spf13/cobra#823 (comment)
This commit makes unknown commands a proper error type so that the caller can check for this specific error type without having to do string comparisons. This is useful in the context of spf13#823
This commit makes unknown commands a proper error type so that the caller can check for this specific error type without having to do string comparisons. This is useful in the context of spf13#823 Signed-off-by: Michael Vogt <mvogt@redhat.com> Signed-off-by: Michael Vogt <michael.vogt@gmail.com>
Prior this commit, the bootc-image-builder container image had a custom entrypoint that hardcoded the use of the build subcommand. This meant that if a user wanted to use a different subcommand, they had to overwrite the entrypoint. This commit changes the cobra code in bib to fallback to build if no subcommand was given. This is slighly ugly, but it allows us to remove the custom entrypoint, streamlining the use of subcommands. Let's see an example of calling the version subcommand: Before: podman run --rm -it --entrypoint=/usr/bin/bootc-image-builder \ quay.io/centos-bootc/bootc-image-builder:latest version After: sudo podman run --rm -it \ quay.io/centos-bootc/bootc-image-builder:latest version Kudos to https://github.com/IKukhta for his code from spf13/cobra#823 (comment)
Prior this commit, the bootc-image-builder container image had a custom entrypoint that hardcoded the use of the build subcommand. This meant that if a user wanted to use a different subcommand, they had to overwrite the entrypoint. This commit changes the cobra code in bib to fallback to build if no subcommand was given. This is slighly ugly, but it allows us to remove the custom entrypoint, streamlining the use of subcommands. Let's see an example of calling the version subcommand: Before: podman run --rm -it --entrypoint=/usr/bin/bootc-image-builder \ quay.io/centos-bootc/bootc-image-builder:latest version After: sudo podman run --rm -it \ quay.io/centos-bootc/bootc-image-builder:latest version Kudos to https://github.com/IKukhta for his code from spf13/cobra#823 (comment)
Prior this commit, the bootc-image-builder container image had a custom entrypoint that hardcoded the use of the build subcommand. This meant that if a user wanted to use a different subcommand, they had to overwrite the entrypoint. This commit changes the cobra code in bib to fallback to build if no subcommand was given. This is slighly ugly, but it allows us to remove the custom entrypoint, streamlining the use of subcommands. Let's see an example of calling the version subcommand: Before: podman run --rm -it --entrypoint=/usr/bin/bootc-image-builder \ quay.io/centos-bootc/bootc-image-builder:latest version After: sudo podman run --rm -it \ quay.io/centos-bootc/bootc-image-builder:latest version Kudos to https://github.com/IKukhta for his code from spf13/cobra#823 (comment)
Why is it so difficult to have a default command? So if I am making a program that does 4 things:
MyCommand Item1 filename
MyCommand Item2 -a 1 -b 2 filename
MyCommand Item3 -c 1 -d 2 -e 3 filename
MyCommand Item4 -a1 -e 3 filename
So if a user just types, 'MyCommand filename', it runs as if they typed 'MyCommand item1 filename' by default? If command item1 errors out then throw an error, but try item1 if no command specified.
The text was updated successfully, but these errors were encountered: