Skip to content

Commit 579569a

Browse files
Improve documentation on behavior of xlsx subcommand (#59)
1 parent 9ecddef commit 579569a

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,21 +563,23 @@ If the length of a cell exceeds `--max-width` it will be truncated with an ellip
563563

564564
### xlsx
565565

566-
Convert sheets of a XLSX file to CSV.
566+
Convert sheets of an XLSX file to CSVs.
567+
568+
The command defaults to writing all converted sheets to a directory with the same name as the XLSX file (without the ".xlsx" extension).
567569

568570
Usage:
569571

570572
```shell
571-
gocsv xlsx [--list-sheets] [--dirname DIRNAME] [--sheet SHEET] FILE
573+
gocsv xlsx [--list-sheets | --dirname DIRNAME | --sheet SHEET] FILE
572574
```
573575

574576
Arguments:
575577

576-
- `--list-sheets` (optional) List the sheets in the XLSX file.
577-
- `--sheet` (optional) Specify the sheet (by index or name) of the sheet to convert.
578-
- `--dirname` (optional) Name of directory to output CSV conversions of sheets from `FILE`. If this is not specified, the command will output the CSV files to a directory with the same name as `FILE` (without the `.xlsx` extension).
578+
- `--list-sheets` (optional) List the sheets, by index and name, in the XLSX file.
579+
- `--sheet` (optional) Specify a single sheet, by index or name, to convert and write to stdout.
580+
- `--dirname` (optional) Specify the name of the directory for the converted sheets. The command defaults to the same name as `FILE`, minus the extension.
579581

580-
By default the `xlsx` subcommand will convert all the sheets in `FILE` to CSVs to a directory with the same name as `FILE`.
582+
Only one option can be used; multiple options cannot be combined.
581583

582584
### zip
583585

cmd/xlsx.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,37 @@ func (sub *XlsxSubcommand) Description() string {
2626
return "Convert sheets of a XLSX file to CSV."
2727
}
2828
func (sub *XlsxSubcommand) SetFlags(fs *flag.FlagSet) {
29-
fs.BoolVar(&sub.listSheets, "list-sheets", false, "List sheets in file")
30-
fs.StringVar(&sub.dirname, "dirname", "", "Name of folder to output sheets to")
31-
fs.StringVar(&sub.sheet, "sheet", "", "Name of sheet to convert")
29+
fs.BoolVar(&sub.listSheets, "list-sheets", false, "List sheets in file by index and name")
30+
fs.StringVar(&sub.dirname, "dirname", "", "Name of folder to write converted sheets to (defaults to file name minus \".xlsx\" extension)")
31+
fs.StringVar(&sub.sheet, "sheet", "", "Index or name of sheet to write to stdout")
3232
}
3333

3434
func (sub *XlsxSubcommand) Run(args []string) {
35+
// Check args
3536
if len(args) > 1 {
3637
fmt.Fprintln(os.Stderr, "Can only convert one file")
3738
os.Exit(1)
3839
} else if len(args) < 1 {
3940
fmt.Fprintln(os.Stderr, "Cannot convert file from stdin")
4041
os.Exit(1)
4142
}
43+
// Check flags
44+
flags := []string{}
45+
if sub.listSheets {
46+
flags = append(flags, "list-sheets")
47+
}
48+
if sub.dirname != "" {
49+
flags = append(flags, "dirname")
50+
}
51+
if sub.sheet != "" {
52+
flags = append(flags, "sheet")
53+
}
54+
if len(flags) > 1 {
55+
fmt.Fprintf(os.Stderr, "Cannot combine flags %s\n", strings.Join(flags, ", "))
56+
os.Exit(1)
57+
}
58+
59+
// Run
4260
filename := args[0]
4361
if sub.listSheets {
4462
ListXlxsSheets(filename)

0 commit comments

Comments
 (0)