Pass global flags down to subcommands #54

Merged
noerw merged 6 commits from zeripath/changelog:fix-51-set-flags-after-subcommand into main 2022-09-16 09:31:37 +00:00
3 changed files with 41 additions and 8 deletions

5
.gitignore vendored

@ -3,4 +3,7 @@
# Binaries
/changelog
/changelog.exe
/changelog.exe
# Go
/vendor/

@ -1,4 +1,5 @@
# Changelog
A changelog generator for Gitea
[![Build Status](https://drone.gitea.com/api/badges/gitea/changelog/status.svg)](https://drone.gitea.com/gitea/changelog)
@ -19,18 +20,21 @@ See the [changelog.example.yml](config/changelog.example.yml) example file.
## Usage
#### Changelog Entries
```
changelog -m=1.11.0 -c=/path/to/my_config_file generate
### Changelog Entries
```sh
changelog generate -m=1.11.0 -c=/path/to/my_config_file
```
#### Contributors List
```
changelog -m=1.11.0 -c=/path/to/my_config_file contributors
### Contributors List
```sh
changelog contributors -m=1.11.0 -c=/path/to/my_config_file
```
## Building
```
```sh
go build
```

@ -80,6 +80,7 @@ func New() *cli.App {
Init,
},
}
copyGlobalFlags(app)
return app
}
@ -95,3 +96,28 @@ func getDefaultConfigFile() string {
}
return ""
}
func copyGlobalFlags(app *cli.App) {
for _, command := range app.Commands {
originalFlagNames := make([]string, 0, len(command.Flags))
for _, flag := range command.Flags {
originalFlagNames = append(originalFlagNames, flag.Names()...)
}
for _, flag := range app.Flags {
found := false
flagNameLoop:
for _, name := range flag.Names() {
for _, originalName := range originalFlagNames {
if name == originalName {
found = true
break flagNameLoop
}
}
}
if !found {
command.Flags = append(command.Flags, flag)
}
}
}
}