Help us shape the future of Go! Take the 2026 Go Developer Survey

argvreader

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2019 License: BSD-2-Clause-Views Imports: 2 Imported by: 4

README

go-argvreader

import argvreader "github.com/Maki-Daisuke/go-argvreader"

Package argvreader makes it a bit less painful to write filter programs in Go especially for Perl programmers.

Description

The argvreader.Reader behaves just like Perl's <ARGV>, that reads lines from one file after another if file list is given in command line arguments, reads from STDIN otherwise. This may be useful to implement UNIX-style filter command, like gzip or grep.

Example
    import argvreader "github.com/Maki-Daisuke/go-argvreader"

    func main(){
      r := argvreader.New()
      b := bufio.NewReader(r)
      for{
        line, err := b.ReadString('\n')
        if err != nil {
          if err == io.EOF {
            break
          }
          panic(err)
        }
        do_something(line)
      }
    }

Then, in your command line:

$ ./main foo bar baz

This reads lines from foo, bar and baz one after another. If no argument is given:

$ ./main

This reads lines from STDIN instead.

You can pass file list manually, of course:

    import flags "github.com/jessevdk/go-flags"

    var opts struct { ... }

    args, err := flags.ParseArgs(&opts, args)
    r := argvreader.NewReader(args)

Usage

type Reader
type Reader interface {
	io.Reader
	CurrentFileName() string
}

Reader provides functionality just like Perl's ARGV file-handle and $ARGV variable.

CurrnetFileName returns a file name that is currently open and being read. It returns "-" if the Reader is reading from STDIN.

func New
func New() Reader

New is just shorthand of NewReader(os.Args[1:]).

func NewReader
func NewReader(args []string) Reader

NewReader creates Reader by manually passing file list. If the list is empty or nil, it returns a Reader reading from os.Stdin.

License

The Simplified BSD License (2-clause). See LICENSE file also.

Author

Daisuke (yet another) Maki

Documentation

Overview

Package argvreader makes it a bit less painful to write filter programs in Go especially for Perl programmers.

Description

The `argvreader.Reader` behaves just like Perl's `<ARGV>`, that reads lines from one file after another if file list is given in command line arguments, reads from STDIN otherwise. This may be useful to implement UNIX-style filter command, like gzip or grep.

Example

import argvreader "github.com/Maki-Daisuke/go-argvreader"

func main(){
  r := argvreader.New()
  b := bufio.NewReader(r)
  for{
    line, err := b.ReadString('\n')
    if err != nil {
      if err == io.EOF {
        break
      }
      panic(err)
    }
    do_something(line)
  }
}

Then, in your command line:

$ ./main foo bar baz

This reads lines from foo, bar and baz one after another. If no argument is given:

$ ./main

This reads lines from STDIN instead.

You can pass file list manually, of course:

import flags "github.com/jessevdk/go-flags"

var opts struct { ... }

args, err := flags.ParseArgs(&opts, args)
r := argvreader.NewReader(args)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader interface {
	io.Reader
	Name() string
}

Reader provides functionality just like Perl's `ARGV` file-handle and `$ARGV` variable.

Name returns a file name that is currently open and being read. It returns "-" if the Reader is reading from STDIN.

func New

func New() Reader

New is just shorthand of `NewReader(os.Args[1:])`.

func NewReader

func NewReader(args []string) Reader

NewReader creates Reader by manually passing file list. If the list is empty or nil, it returns a Reader reading from os.Stdin.

Jump to

Keyboard shortcuts

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