-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
99 lines (82 loc) · 3.23 KB
/
cli.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
package main
import (
"fmt"
"os"
"github.com/gotidy/x2c/internal/converter"
"github.com/rs/zerolog"
)
type Context struct {
Logger zerolog.Logger
}
type ConvertCmd struct {
Encoding string `short:"c" name:"encoding" help:"Encoding of output CSV (http://www.w3.org/TR/encoding)." default:"utf-8"`
BOM bool `short:"b" name:"bom" help:"Write BOM to CSV."`
Delimiter string `short:"d" name:"delimiter" help:"Cells delimiter." default:","`
LineTerminator string `short:"l" name:"line_terminator" help:"line terminator - lines terminator in CSV. Default is \n" default:"\n" enum:"\n,\r\n,CRLF,LF"`
SheetName []string `short:"n" name:"sheetname" help:"Sheet names to convert."`
SheetID []int `short:"s" name:"sheet" help:"Sheet indexes to convert."`
ExcludeSheetName []string `short:"N" name:"exclude_sheet_name" help:"Exclude sheets with names."`
ExcludeSheetID []int `short:"S" name:"exclude_sheet" help:"Exclude sheets with ID."`
SheetPattern string `short:"I" help:"Only include sheets named matching given pattern."`
ExcludeSheetPattern string `short:"E" help:"Exclude sheets named matching given pattern."`
Columns []string `short:"C" name:"columns" help:"Columns names added to CSV as a zero row."`
MaxColumns uint `short:"m" help:"Columns maximum that will be exported."`
SkipEmpty bool `short:"e" help:"Skip empty lines."`
Trim bool `short:"t" help:"Remove all leading and trailing white space."`
Source string `arg:"" name:"source" help:"Paths to XLSX file." type:"path"`
Output string `arg:"" optional:"" name:"output" help:"Output CSV file path. It may include variables {{name}} and {{num}}" type:"path"`
}
func (c *ConvertCmd) Run(ctx *Context) error {
conv, err := converter.New(converter.Options{
Source: c.Source,
Output: os.Stdout,
Destination: c.Output,
Encoding: c.Encoding,
BOM: c.BOM,
Delimiter: c.Delimiter,
LineTerminator: c.LineTerminator,
SheetName: c.SheetName,
SheetID: c.SheetID,
ExcludeSheetName: c.ExcludeSheetName,
ExcludeSheetID: c.ExcludeSheetID,
SheetPattern: c.SheetPattern,
ExcludeSheetPattern: c.ExcludeSheetPattern,
MaxColumns: int(c.MaxColumns),
Trim: c.Trim,
SkipEmpty: c.SkipEmpty,
})
if err != nil {
ctx.Logger.Fatal().Msg(err.Error())
}
err = conv.Convert()
if err != nil {
ctx.Logger.Fatal().Msg(err.Error())
}
return nil
}
type ListCmd struct {
Source string `arg:"" required:"" name:"source" help:"Path to XLSX file." type:"path"`
}
func (l *ListCmd) Run(ctx *Context) error {
c, err := converter.New(converter.Options{Source: l.Source, Output: os.Stdout})
if err != nil {
ctx.Logger.Fatal().Msg(err.Error())
}
defer c.Close()
for _, l := range c.Sheets() {
fmt.Println(l)
}
return nil
}
type VersionCmd struct {
}
func (l *VersionCmd) Run(ctx *Context) error {
fmt.Printf("Version %s\n\n", Version)
fmt.Println(description)
return nil
}
var cli struct {
Convert ConvertCmd `cmd:"" help:"Convert XLSX file CSV." default:"withargs"`
List ListCmd `cmd:"" help:"List sheets of XLSX file."`
Version VersionCmd `cmd:"" help:"Version."`
}