-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharguments.go
101 lines (86 loc) · 2.38 KB
/
arguments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package wildcat
import (
"bufio"
"io"
"path/filepath"
"strings"
)
// ReadOptions represents the set of options about reading file.
type ReadOptions struct {
FileList bool
NoIgnore bool
NoExtract bool
AllFiles bool
}
type RuntimeOptions struct {
ShowProgress bool
ThreadNumber int64
StoreContent bool
}
// Argf shows the command line arguments and stdin (if no command line arguments).
type Argf struct {
Options *ReadOptions
RuntimeOpts *RuntimeOptions
Arguments []*Arg
}
// Arg represents the one of command line arguments and its index.
type Arg struct {
name string
index *Order
}
// NewArg creates an instance of Arg with the given name.
func NewArg(name string) *Arg {
return NewArgWithIndex(NewOrder(), name)
}
// NewArgWithIndex creates an instance of Arg with given parameters.
func NewArgWithIndex(index *Order, name string) *Arg {
return &Arg{index: index, name: name}
}
// Name returns the name of receiver Arg object.
func (arg *Arg) Name() string {
return arg.name
}
// Index returns the index of receiver Arg object.
func (arg *Arg) Index() *Order {
return arg.index
}
// NewArgf creates an instance of Argf for treating command line arguments.
func NewArgf(arguments []string, opts *ReadOptions, runtimeOpts *RuntimeOptions) *Argf {
entries := []*Arg{}
for index, arg := range arguments {
entries = append(entries, NewArgWithIndex(NewOrderWithIndex(index), arg))
}
return &Argf{Arguments: entries, Options: opts, RuntimeOpts: runtimeOpts}
}
// Generator is the type for generating Counter object.
type Generator func() Counter
// DefaultGenerator is the default generator for counting all (bytes, characters, words, and lines).
var DefaultGenerator Generator = func() Counter { return NewCounter(All) }
func drainDataFromReader(in io.Reader, counter Counter) error {
reader := bufio.NewReader(in)
for {
line, err := reader.ReadBytes('\n')
counter.update(line)
if err == io.EOF {
break
}
if err != nil {
return err
}
}
return nil
}
func ignores(dir string, withIgnoreFile bool, parent Ignore) Ignore {
if withIgnoreFile {
return newIgnoreWithParent(dir, parent)
}
return &noIgnore{parent: parent}
}
func isIgnore(opts *ReadOptions, ignore Ignore, name string) bool {
base := filepath.Base(name)
ignoreFlag := !opts.AllFiles && strings.HasPrefix(base, ".")
if !opts.NoIgnore {
return ignoreFlag || ignore.IsIgnore(name)
}
return ignoreFlag
}