Skip to content

Commit ea84305

Browse files
authored
refactor(parquet/cmd): replace uses of github.com/docopt/docopt-go (#764)
The github.com/docopt/docopt-go repository has not been maintained for many Years; replace it for stdlib flags, which is slightly more code to write, but removes the un-maintained dependency. ### Rationale for this change ### What changes are included in this PR? ### Are these changes tested? ### Are there any user-facing changes? Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent deed26c commit ea84305

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ require (
3030
github.com/andybalholm/brotli v1.2.1
3131
github.com/apache/thrift v0.22.0
3232
github.com/cespare/xxhash/v2 v2.3.0
33-
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
3433
github.com/goccy/go-json v0.10.6
3534
github.com/google/flatbuffers v25.12.19+incompatible
3635
github.com/google/uuid v1.6.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
4040
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4141
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
4242
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43-
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
44-
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
4543
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
4644
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
4745
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=

parquet/cmd/parquet_reader/main.go

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package main
1818

1919
import (
2020
"bufio"
21+
"errors"
22+
"flag"
2123
"fmt"
2224
"io"
2325
"log"
@@ -30,8 +32,6 @@ import (
3032
"github.com/apache/arrow-go/v18/parquet/file"
3133
"github.com/apache/arrow-go/v18/parquet/metadata"
3234
"github.com/apache/arrow-go/v18/parquet/schema"
33-
34-
"github.com/docopt/docopt-go"
3535
)
3636

3737
var version = ""
@@ -47,33 +47,68 @@ Commands:
4747
4848
Options:
4949
-h --help Show this screen.
50-
--print-key-value-metadata Print out the key-value metadata. [default: false]
51-
--only-metadata Stop after printing metadata, no values.
52-
--no-metadata Do not print metadata.
53-
--output=FILE Specify output file for data. [default: -]
54-
--no-memory-map Disable memory mapping the file.
55-
--int96-timestamp Parse INT96 as TIMESTAMP for legacy support.
56-
--json Format output as JSON instead of text.
57-
--csv Format output as CSV instead of text.
58-
--columns=COLUMNS Specify a subset of columns to print, comma delimited indexes.`
50+
`
51+
52+
func printUsage(fs *flag.FlagSet) {
53+
fmt.Fprint(fs.Output(), usage)
54+
fs.VisitAll(func(f *flag.Flag) {
55+
name, flagUsage := flag.UnquoteUsage(f)
56+
flagName := "--" + f.Name
57+
if name != "" {
58+
flagName += "=" + name
59+
}
60+
fmt.Fprintf(fs.Output(), " %-30s%s\n", flagName, flagUsage)
61+
})
62+
}
5963

6064
func main() {
61-
opts, _ := docopt.ParseDoc(usage)
6265
var config struct {
63-
ColumnIndexes bool `docopt:"column-indexes"`
66+
ColumnIndexes bool
6467
PrintKeyValueMetadata bool
6568
OnlyMetadata bool
6669
NoMetadata bool
6770
Output string
6871
NoMemoryMap bool
69-
JSON bool `docopt:"--json"`
70-
CSV bool `docopt:"--csv"`
71-
ParseInt96AsTimestamp bool `docopt:"--int96-timestamp"`
72+
JSON bool
73+
CSV bool
74+
ParseInt96AsTimestamp bool
7275
Columns string
7376
File string
7477
}
75-
opts.Bind(&config)
7678

79+
args := os.Args[1:]
80+
if len(args) > 0 && args[0] == "column-indexes" {
81+
config.ColumnIndexes = true
82+
args = args[1:]
83+
}
84+
85+
fs := flag.NewFlagSet("parquet_reader", flag.ContinueOnError)
86+
fs.SetOutput(os.Stderr)
87+
fs.BoolVar(&config.OnlyMetadata, "only-metadata", false, "Stop after printing metadata, no values.")
88+
fs.BoolVar(&config.NoMetadata, "no-metadata", false, "Do not print metadata.")
89+
fs.BoolVar(&config.NoMemoryMap, "no-memory-map", false, "Disable memory mapping the file.")
90+
fs.BoolVar(&config.JSON, "json", false, "Format output as JSON instead of text.")
91+
fs.BoolVar(&config.CSV, "csv", false, "Format output as CSV instead of text.")
92+
fs.StringVar(&config.Output, "output", "-", "Specify output `FILE` for data.")
93+
fs.BoolVar(&config.PrintKeyValueMetadata, "print-key-value-metadata", false, "Print out the key-value metadata.")
94+
fs.BoolVar(&config.ParseInt96AsTimestamp, "int96-timestamp", false, "Parse INT96 as TIMESTAMP for legacy support.")
95+
fs.StringVar(&config.Columns, "columns", "", "Specify a subset of `COLUMNS` to print, comma delimited indexes.")
96+
fs.Usage = func() {
97+
printUsage(fs)
98+
}
99+
100+
if err := fs.Parse(args); err != nil {
101+
if errors.Is(err, flag.ErrHelp) {
102+
os.Exit(0)
103+
}
104+
os.Exit(1)
105+
}
106+
if fs.NArg() != 1 {
107+
fs.Usage()
108+
fmt.Fprintln(os.Stderr, "expected exactly one parquet file")
109+
os.Exit(1)
110+
}
111+
config.File = fs.Arg(0)
77112
parseInt96AsTimestamp = config.ParseInt96AsTimestamp
78113

79114
var dataOut io.Writer

parquet/cmd/parquet_schema/main.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,39 @@ import (
2222

2323
"github.com/apache/arrow-go/v18/parquet/file"
2424
"github.com/apache/arrow-go/v18/parquet/schema"
25-
"github.com/docopt/docopt-go"
2625
)
2726

2827
const usage = `Parquet Schema Dumper.
28+
2929
Usage:
3030
parquet_schema -h | --help
3131
parquet_schema <file>
32+
3233
Options:
33-
-h --help Show this screen.`
34+
-h --help Show this screen.
35+
`
3436

3537
func main() {
36-
args, _ := docopt.ParseDoc(usage)
37-
rdr, err := file.OpenParquetFile(args["<file>"].(string), false)
38-
if err != nil {
39-
fmt.Fprintln(os.Stderr, "Error opening parquet file: ", err)
38+
args := os.Args[1:]
39+
40+
switch len(args) {
41+
case 1:
42+
switch args[0] {
43+
case "-h", "--help":
44+
fmt.Fprint(os.Stderr, usage)
45+
os.Exit(0)
46+
}
47+
48+
rdr, err := file.OpenParquetFile(args[0], false)
49+
if err != nil {
50+
fmt.Fprintln(os.Stderr, "Error opening parquet file:", err)
51+
os.Exit(1)
52+
}
53+
54+
schema.PrintSchema(rdr.MetaData().Schema.Root(), os.Stdout, 2)
55+
default:
56+
fmt.Fprint(os.Stderr, usage)
57+
fmt.Fprintln(os.Stderr, "expected exactly one parquet file")
4058
os.Exit(1)
4159
}
42-
43-
schema.PrintSchema(rdr.MetaData().Schema.Root(), os.Stdout, 2)
4460
}

0 commit comments

Comments
 (0)