https://github.com/maki-daisuke/go-argvreader
Make it a bit less painful to write filter programs in Go for Perl programmers
https://github.com/maki-daisuke/go-argvreader
argv command-line cui go golang
Last synced: about 1 year ago
JSON representation
Make it a bit less painful to write filter programs in Go for Perl programmers
- Host: GitHub
- URL: https://github.com/maki-daisuke/go-argvreader
- Owner: Maki-Daisuke
- License: bsd-2-clause
- Created: 2015-11-14T17:00:02.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-10-11T22:12:37.000Z (over 6 years ago)
- Last Synced: 2025-03-24T07:06:00.282Z (about 1 year ago)
- Topics: argv, command-line, cui, go, golang
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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 ``, 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
```go
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:
```go
import flags "github.com/jessevdk/go-flags"
var opts struct { ... }
args, err := flags.ParseArgs(&opts, args)
r := argvreader.NewReader(args)
```
## Usage
#### type Reader
```go
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
```go
func New() Reader
```
New is just shorthand of `NewReader(os.Args[1:])`.
#### func NewReader
```go
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](LICENSE) file also.
## Author
Daisuke (yet another) Maki