Skip to content
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

Command chaining support #665

Closed
willmitchell opened this issue Apr 6, 2018 · 3 comments
Closed

Command chaining support #665

willmitchell opened this issue Apr 6, 2018 · 3 comments

Comments

@willmitchell
Copy link

Hi there, and thanks for Cobra!

Is it possible to chain commands? Imagine a super simple 'accumulator' type of program that supports 3 commands as shown below:

$ go run main.go 
Demo program related to Cobra command chaining

Usage:
  cobra_chain_ex [command]

Available Commands:
  add         Add 1 to the accumulator
  help        Help about any command
  sub         Subtract 1 from the accumulator
  total       Print the value of the accumulator

I would like to be able to chain commands like so:

$ go run main.go add add add sub total
add called
$ 

Note that only the first command is executed. Is there a way to make Cobra execute all of the commands in left to right order?

Code is here.

Thanks!

@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@ghostsquad
Copy link

@willmitchell you could write a variadic calc command. Each argument describes an action that is done in order, so the available actions for this example is:

add
sub

This is similar to lfold in various languages. https://medium.com/zendesk-engineering/foldable-go-d74fb9cf2fc9

go run main.go calc add add sub
2
go run main.go calc --start 5 add add add add
9

@johnSchnake
Copy link
Collaborator

Sorry this has been open for so long with little attention. As part of #1600 I'm going to closing as duplicate of #726 which is requesting the same feature effectively (multiple commands in series on the CLI).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants