urfave / cli

A simple, fast, and fun package for building command line apps in Go
https://cli.urfave.org
MIT License
21.91k stars 1.69k forks source link

Free Non Strict Options for a Command #1793

Closed kudla closed 11 months ago

kudla commented 11 months ago

Hi! Please how could the flags be defined for the app level. Yet for the command level to let provide any flag without strict advance definition.

So that the application options would be available by name and the command options would be available as raw slice separated of command arguments. Something like this?

&cli.App{
        Name:  "app",
        Flags: []cli.Flag{
            &cli.StringFlag{
                Name:    "appOption",
                Aliases: []string{"o"},
            },
        },
        Commands: []*cli.Command{
            {
                Name:      "task",
                Action:    func(cCtx *cli.Context) error {
                                    appOption := cCtx.String("appOption")
                                    comOptions := cCtx.RestOfOptions()
                                    comArgs := cCtx.Args().Slice()
                                    return nil
                                },
            },
        },
    }
app -o app-level-options task --what ever -bc task-arguments next

Thx.

dearchap commented 11 months ago

@kudla You can try setting the Command.SkipFlagParsing to true https://github.com/urfave/cli/blob/v2-maint/command.go#L45 This will allow you to get all the rest of the options in cCtx.Args().

kudla commented 11 months ago

That's great . Thank you @dearchap What about partial definition ? E.g. some specific options would adjust the behavior of application it self yet all the rest options would be fed as is to a downstream application.

dearchap commented 11 months ago

@kudla There is no support for that in the urfave/cli library. Its either all or nothing. Anything else you have to implement yourself.

kudla commented 11 months ago

Thanks @dearchap. I wen exactly the same direction