Skip to content

Commit cf68f23

Browse files
committed
Add --unique option to modify --tree output
1 parent d44094c commit cf68f23

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ An example XPath would then look like `+/_:a/_:b/_:c+`.
1212

1313
== SYNOPSIS
1414

15-
*xmlsect* _file_ [_xpath_] [_relative_xpath] [*--tree*]
15+
*xmlsect* _file_ [_xpath_] [_relative_xpath_] [*--tree* [*--unique*]]
1616

1717
== Installation
1818

xmlsect.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func printNodeSet(n []dom.Node) {
6464
}
6565
}
6666

67-
func printNodeSetTree(n []dom.Node) {
67+
func printNodeSetTree(n []dom.Node, unique bool) {
6868
var str string
6969
l := len(n)
7070
for i, e := range n {
71-
str += printTreeNode(e, 0)
71+
str += printTreeNode(e, 0, unique)
7272
if i+1 < l {
7373
str += "\n"
7474
}
@@ -98,13 +98,13 @@ func printName(n *dom.Name) string {
9898
// Use of this source code is governed by a BSD-style
9999
// license that can be found in the LICENSE file.
100100
//
101-
func printTreeNode(n dom.Node, level int) string {
101+
func printTreeNode(n dom.Node, level int, unique bool) string {
102102
var str string
103103
switch n := n.(type) {
104104
case *dom.Document:
105105
log.Printf("Document. Children %d\n", len(n.Children()))
106106
for _, c := range n.Children() {
107-
str += printTreeNode(c, level+1)
107+
str += printTreeNode(c, level+1, unique)
108108
}
109109
case *dom.Element:
110110
str += "/"
@@ -123,21 +123,31 @@ func printTreeNode(n dom.Node, level int) string {
123123
}
124124
if len(n.Children()) != 0 {
125125
log.Printf("Element '%s' Children %d\n", n.Name, len(n.ChildNodes))
126-
unique := make(map[string]int)
126+
uniqueMap := make(map[string]int)
127127
for _, c := range n.Children() {
128-
tmpStr := printTreeNode(c, level+1)
128+
tmpStr := printTreeNode(c, level+1, unique)
129129
// Skip empty results
130130
if tmpStr == "" {
131131
continue
132132
}
133-
if v, ok := unique[tmpStr]; ok {
133+
if !unique {
134+
if strings.HasPrefix(tmpStr, "/") {
135+
str += "\n"
136+
str += strings.Repeat(" ", level+1)
137+
}
138+
str += tmpStr
139+
}
140+
if v, ok := uniqueMap[tmpStr]; ok {
134141
count := v + 1
135-
unique[tmpStr] = count
142+
uniqueMap[tmpStr] = count
136143
} else {
137-
unique[tmpStr] = 1
144+
uniqueMap[tmpStr] = 1
138145
}
139146
}
140-
for k, v := range unique {
147+
if !unique {
148+
return str
149+
}
150+
for k, v := range uniqueMap {
141151
if strings.HasPrefix(k, "/") {
142152
str += "\n"
143153
str += strings.Repeat(" ", level+1)
@@ -169,19 +179,21 @@ func printDoc(doc *dom.Document) {
169179

170180
func synopsis() {
171181
synopsis := `# USAGE:
172-
xsect <file> [<xpath>] [<relative_xpath>] [--tree]
182+
xsect <file> [<xpath>] [<relative_xpath>] [--tree [--unique]]
173183
174184
xsect [--help]
175185
`
176186
fmt.Fprintln(os.Stderr, synopsis)
177187
}
178188

179189
func main() {
190+
var unique bool
180191
opt := getoptions.New()
181192
opt.Bool("help", false)
182193
opt.Bool("debug", false)
183194
opt.Bool("version", false)
184195
opt.Bool("tree", false)
196+
opt.BoolVar(&unique, "unique", false)
185197
remaining, err := opt.Parse(os.Args[1:])
186198
if err != nil {
187199
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
@@ -247,7 +259,7 @@ func main() {
247259
}
248260
log.Printf("results: %d\n", len(nodeSet))
249261
if opt.Called("tree") {
250-
printNodeSetTree(nodeSet)
262+
printNodeSetTree(nodeSet, unique)
251263
os.Exit(0)
252264
}
253265
printNodeSet(nodeSet)

0 commit comments

Comments
 (0)