cli icon indicating copy to clipboard operation
cli copied to clipboard

cli [OPTION] -flagA arg0 -flagB arg1 -flagC arg2

Open hnistlzy opened this issue 6 years ago • 5 comments

How to achieve such a program just like “cli [OPTION] -flagA arg0 -flagB arg1 -flagC arg2”

hnistlzy avatar Apr 12 '20 07:04 hnistlzy

If [OPTION] is a positional argument, which covers arbitrary values that get passed in like a function parameter would be, the docs for usage are here: https://github.com/urfave/cli/blob/master/docs/v2/manual.md#arguments

If [OPTION] should be a command, which covers a fixed number of different actions that could be taken, the docs are here: https://github.com/urfave/cli/blob/master/docs/v2/manual.md#subcommands

If the question is about how to pass the same flags to every command, there's two options:

  1. Define a list of flags, then use them as configuration for each command:
flags := []cli.Flag{
    // ...
}
app := cli.NewApp()
app.Commands = []*cli.Command{
    {
        Name: "cmd1",
        Flags: flags,
        // ...
    },
    {
        Name: "cmd2",
        Flags: flags,
        // ...
    },
}
  1. Define them globally so they are available for all commands out of the box. If this option is picked, flags must be passed before the command rather than after, like so: cli -flagA arg0 -flagB arg1 -flagC arg2 [COMMAND]
app := cli.NewApp()
app.Flags = []cli.Flag{
    // ...
}

Let me know if that doesn't answer your question.

rliebz avatar Apr 14 '20 11:04 rliebz

thanks my bro

igzzr avatar Jun 10 '20 14:06 igzzr

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

stale[bot] avatar Sep 09 '20 19:09 stale[bot]

Closing this as it has become stale.

stale[bot] avatar Oct 10 '20 02:10 stale[bot]

I think (?) this is resolved? @hnistlzy Please let us know if this should be reopened if that's not the case 😁

meatballhat avatar Apr 24 '22 17:04 meatballhat