GithubHelp home page GithubHelp logo

pflagheaders's Introduction

HTTP Headers for spf13/pflag

Go Reference fgm codecov Go Report Card OpenSSF Scorecard

This package allows use of repeated CLI flags defining HTTP headers, like:

CLI usage

# 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

package main

import (
	"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

// In cmd/root.go
package cmd

import (
	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)
}

pflagheaders's People

Contributors

fgm avatar

Stargazers

 avatar

Watchers

 avatar

pflagheaders's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.