ARTICLE AD BOX
I am building a CLI tool for docker compose to selectively exclude some containers from running/stopping operations (from a docker-compose.yaml having many containers, user can specify the containers to exclude via an --exclude flag). The tool parses a docker-compose.yaml file and builds a command string dynamically based on which containers should be excluded.
The --exclude flag has been implemented. I want to add a --file flag (the CLI defaults the docker compose file name as docker-compose.yaml). How do I add a --file flag?
This is my code so far:
excludeFlag := &cli.StringSliceFlag{ Name: "exclude", Aliases: []string{"e"}, Usage: "services to exclude", } cmd := &cli.Command{ Name: "Dockexclude", Usage: "Manage your Docker Compose stack with the ability to exclude services.", UsageText: "dockexclude <command> [--exclude <service>...]", Description: "Manage your Docker Compose stack with the ability to exclude services.", Flags: []cli.Flag{ &cli.StringFlag{ Name: "file", Aliases: []string{"f"}, Usage: "target docker compose file", }, }, Action: func(ctx context.Context, cmd *cli.Command) error { if cmd.NArg() > 0 { composeFilePath = cmd.Args().Get(0) } return nil }, Commands: []*cli.Command{ { Name: "up", Usage: "Start and create containers", UsageText: "dockexclude up [--exclude <service>...]", Flags: []cli.Flag{excludeFlag}, Action: func(ctx context.Context, cmd *cli.Command) error { excludedServices := cmd.StringSlice("exclude") runDockerCompose("up", excludedServices) return nil }, }, // and so on...I followed the documentation and added this block of code:
Flags: []cli.Flag{ &cli.StringFlag{ Name: "file", Aliases: []string{"f"}, Usage: "target docker compose file", }, }, Action: func(ctx context.Context, cmd *cli.Command) error { if cmd.NArg() > 0 { composeFilePath = cmd.Args().Get(0) } return nil }It works, but the --exclude flag doesn't work anymore.
