Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fgm/pflagheaders
Supports HTTP header flags with spf13/pflag
https://github.com/fgm/pflagheaders
cli cobra golang pflag spf13-cobra
Last synced: 14 days ago
JSON representation
Supports HTTP header flags with spf13/pflag
- Host: GitHub
- URL: https://github.com/fgm/pflagheaders
- Owner: fgm
- License: mit
- Created: 2021-05-07T08:27:41.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-12T12:46:12.000Z (10 months ago)
- Last Synced: 2024-10-19T16:28:51.150Z (2 months ago)
- Topics: cli, cobra, golang, pflag, spf13-cobra
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP Headers for spf13/pflag
[![Go Reference](https://pkg.go.dev/badge/pkg.go.dev/github.com/fgm/pflagheaders.svg)](https://pkg.go.dev/github.com/fgm/pflagheaders)
[![fgm](https://circleci.com/gh/fgm/pflagheaders.svg?style=shield)](https://app.circleci.com/pipelines/github/fgm/pflagheaders)
[![codecov](https://codecov.io/gh/fgm/pflagheaders/branch/main/graph/badge.svg?token=O6LQF0BGGF)](https://codecov.io/gh/fgm/pflagheaders)
[![Go Report Card](https://goreportcard.com/badge/github.com/fgm/pflagheaders)](https://goreportcard.com/report/github.com/fgm/pflagheaders)
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/fgm/pflagheaders/badge)](https://securityscorecards.dev/viewer/?uri=github.com/fgm/pflagheaders)This package allows use of repeated CLI flags defining HTTP headers, like:
## CLI usage
```bash
# Long format
mycommand --header "Accept: text/plain" --header "Authorization: bearer sometoken"# Short format
mycommand -H "Accept: text/plain" -H "Authorization: bearer sometoken"# Repeated headers are supported and combined
mycommand -H "X-Array-Header: value1" -H "X-Array-Header: value2"
# Will return a slice value with value1 and value2 for key X-Array-Header# Headers are canonicalized
mycommand -H "content-type: application/json"
# Will have key Content-Type
```## Code usage
### Simple```go
package mainimport (
"fmt""github.com/spf13/pflag"
"github.com/fgm/pflagheaders"
)func main() {
// HeaderFlag provides a preconfigured default flag
h := pflagheaders.HeaderFlag()
pflag.Parse()fmt.Printf("Headers:\n%s\n", h)
// The resulting http.Header is available after Parse:
fmt.Printf("Inner header:\n%#v\n", h.Header)
}
```### In projects using spf13/cobra
```go
// In cmd/root.go
package cmdimport (
pfh "github.com/fgm/pflagheaders"
"github.com/spf13/cobra" // and others
)var header = &pfh.Header{}
var rootCmd = &cobra.Command{
/* generated by Cobra */
Run: func(_ *cobra.Command, _ []string) { fmt.Println(header) }
}func init() {
cobra.OnInitialize(initConfig) // Generated by Cobra
// ...other lines generated by Cobra
// You can use the provided constants, or use other values.
rootCmd.Flags().VarP(header, pfh.NameLong, pfh.NameShort, pfh.Help)
}
```