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 aliases #125

Closed
rustyoz opened this issue Jul 14, 2015 · 3 comments
Closed

Command aliases #125

rustyoz opened this issue Jul 14, 2015 · 3 comments

Comments

@rustyoz
Copy link

rustyoz commented Jul 14, 2015

How would one go about creating a command that is an alias of another command with given flags?

for instance
program subcommand1 --flag1 --flag2 --flag3
can become
program subcommand2

@epelc
Copy link

epelc commented Jul 22, 2015

@rustyoz if you use viper you can register an alias

https://github.com/spf13/viper#registering-and-using-aliases

@cassava
Copy link

cassava commented Jul 30, 2015

On a related note, you can define aliases in the sense that sub1 is the same as sub2. From the definition:

type Command struct {
    // Name is the command name, usually the executable's name.
    name string
    // The one-line usage message.
    Use string
    // An array of aliases that can be used instead of the first word in Use.
    Aliases []string
    // The short description shown in the 'help' output.
    Short string
    // The long message shown in the 'help <this-command>' output.
    Long string
    // Examples of how to use the command
    Example string
    ...

Perhaps there is a way to discover which command has been used, and then continue on from there?

@apriendeau
Copy link
Contributor

@rustyoz I am sure this is not ideal but this one way of achieving the same desired effect.

var Verbose bool

var subCmd = &cobra.Command{
    Use:   "sub [no options!]",
    Short: "My sub command",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Printf("Inside subCmd Run with args: %v\n", args)
        if Verbose {
            println("I am verbose")
        }
    },
}

subcmd.Flags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")

var subAlias = &cobra.Command{
    Use:   "subAlias [no options!]",
    Short: "alias for sub",
    Run: func(cmd *cobra.Command, args []string) {
        Verbose = true
        subCmd.Run(cmd, args)
    },
}

@spf13 spf13 closed this as completed Nov 6, 2015
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

5 participants