dialect

package
v0.0.0-...-509a7ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2022 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Helpers that makes it easy to build CSV dialects. This API is currently in alpha. Feel free to discuss it on https://github.com/TakahiroSato/go-csv/issues.

Example (Flag)
package main

import (
	"flag"
	"os"

	csv "github.com/TakahiroSato/go-csv"
	"github.com/TakahiroSato/go-csv/dialect"
)

func main() {
	builder := dialect.FromCommandLine()

	flag.Parse()

	dialect, err := builder.Dialect()
	if err != nil {
		panic(err)
	}

	reader := csv.NewDialectWriter(os.Stdout, *dialect)
	reader.Write([]string{"Hello", "World"})
	reader.Flush()

}
Output:

Hello	World
Example (FlagSet)
package main

import (
	"flag"
	"os"

	csv "github.com/TakahiroSato/go-csv"
	"github.com/TakahiroSato/go-csv/dialect"
)

func main() {
	fset := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	builder := dialect.FromFlagSet(fset)

	fset.Parse([]string{})

	dialect, err := builder.Dialect()
	if err != nil {
		panic(err)
	}

	reader := csv.NewDialectWriter(os.Stdout, *dialect)
	reader.Write([]string{"Hello", "World"})
	reader.Flush()

}
Output:

Hello	World

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DialectBuilder

type DialectBuilder struct {
	// contains filtered or unexported fields
}

func FromCommandLine

func FromCommandLine() *DialectBuilder

Construct a CSV Dialect from command line using the `flag` package. This is three steps: First, call this function and store the handler. Optionally register other flags. Call `flag.Parse()`. A dialect can then be constructed by calling `DialectBuilder.Dialect()`.

func FromFlagSet

func FromFlagSet(f *flag.FlagSet) *DialectBuilder

Constructs a CSV Dialect from a specific flagset. Essentially the same as `FromCommandLine()`, except it supports a custom FlagSet. See `FromCommandLine()` for a description on how to use this one.

func (*DialectBuilder) Dialect

func (p *DialectBuilder) Dialect() (*csv.Dialect, error)

Construct a Dialect from a FlagSet. Make sure to parse the FlagSet before calling this.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL