spread-cli 0.2.0

Converts spreadsheets and CSV files to a friendly JSON format with many field name and data-type options.
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use std::str::FromStr;

use clap::Parser;
use spreadsheet_to_json::heck::ToSnakeCase;
use spreadsheet_to_json::serde_json::{Number, Value};
use spreadsheet_to_json::FieldNameMode;
use spreadsheet_to_json::{is_truthy::*, options::{Column, DateTimeMode, OptionSet}, Format, ReadMode};
use simple_string_patterns::{SimpleMatch, StripCharacters};
use to_segments::ToSegments;

const DEFAULT_MAX_FOR_PREVIEW: u32 = 10;

/// Command line arguments configuration
#[derive(Parser, Debug)]
#[clap(
  author,
  version,
  about,
  long_about = "Converts spreadsheets (xlsx, xlsm, xls, xlsb, ods) and CSV/TSV files to JSON, \
    JSON Lines, or plain text, with control over which sheet(s), rows, columns, and field \
    names/types come out the other end. Designed to pipe straight into jq/yq -- see \
    https://github.com/neilg63/spreadsheet_to_json_cli for the full README.",
  after_help = "EXAMPLES:\n    \
    spread-cli sales.xlsx                        Read the first sheet as JSON\n    \
    spread-cli sales.xlsx -s Q1 -r               Just the rows from the sheet named \"Q1\"\n    \
    spread-cli sales.xlsx -n 2                    Read the 2nd sheet (1-based)\n    \
    spread-cli big.csv -d -o out.jsonl            Stream a large CSV to a JSONL file in the background\n    \
    spread-cli workbook.xlsx -px --json | jq .columns   Structural overview of every worksheet\n\n\
    Run with no arguments (or no PATH) to see this help again."
)]
pub struct Args {

  #[clap(help = "Path to the source spreadsheet or CSV/TSV file")]
  pub path: Option<String>,

  #[clap(
    short = 'p', long, value_parser, default_value_t = false,
    help = "Sample rows from every worksheet (multi-sheet mode), not just the selected one",
    long_help = "Switches to multi-sheet mode and samples up to --max/-m rows (default 10) \
      from *every* worksheet, not just the selected one -- --sheet/--index/--number are \
      ignored in this mode. Field names for every sheet come back in a top-level \"columns\" \
      map instead of a single \"fields\" array; each worksheet's own row count and rows live \
      under \"data\", one block per sheet."
  ) ]
  pub preview: bool,

  #[clap(short = 'r', long, value_parser, default_value_t = false, help = "Output just the data rows, as a JSON array, with no metadata wrapper") ]
  pub rows: bool,

  #[clap(short = 'l', long, value_parser, default_value_t = false, help = "Output JSON Lines: one compact JSON object per row, no surrounding array (implies --rows)") ]
  pub lines: bool,

  #[clap(
    short = 'x', long = "overview", alias = "exclude-cells", value_parser, default_value_t = false,
    help = "Structural overview only: sheet names, row counts, field names -- no cell values",
    long_help = "Drops row *data* from the result while keeping a structural overview with \
      sheet names, row counts, column/field names -- with no actual cell values. Alone, it \
      just omits an always-empty \"data\" array from --json output for the single selected \
      sheet. Combined with --preview (-xp), it surveys the whole workbook: every sheet's \
      name, field names (\"columns\"), and row count (\"row_counts\"), with zero cell data \
      -- handy for large multi-sheet files (e.g. statistics-agency spreadsheets) where you \
      want to see what's in the file before deciding what to pull out of it. (Also accepted \
      as --exclude-cells, its original, more literal name.)"
  ) ]
  pub exclude_cells: bool,

  #[clap(short = 'j', long, value_parser, default_value_t = false, help = "Format JSON output as indented, multi-line JSON") ]
  pub json: bool,

  #[clap(short, long, value_parser, help = "Sheet name to select (case-insensitive, ignores spaces/punctuation); falls back to the first sheet if unmatched") ]
  pub sheet: Option<String>,

  #[clap(
    short = 'n', long, value_parser, conflicts_with = "index",
    help = "Sheet number, 1-based",
    long_help = "Sheet number, 1-based (1 is the first sheet). Equivalent to --index but \
      1-based, for matching how you'd count sheets off in a spreadsheet app (\"the 3rd \
      sheet\") without the usual off-by-one. `-n 1` is the same as `-i 0` (or `-s sheet1`, \
      if the first sheet happens to be named \"sheet1\"). Cannot be combined with --index."
  ) ]
  pub number: Option<u32>,

  #[clap(short, long, value_parser, help = "Maximum number of rows to return (per sheet, when combined with --preview)") ]
  pub max: Option<u32>,

  #[clap(
    short = 't', long, value_parser, conflicts_with = "header_index",
    help = "Header row number, 1-based (1 is the first row) -- if the headers aren't on the first row",
    long_help = "Header row number, 1-based (1 is the first row), if the headers aren't \
      on the first row. Equivalent to --header-index but 1-based, for matching the row \
      numbers you'd see in a spreadsheet app. `-t 1` is the same as `--header-index 0` \
      (both mean \"the first row\"). Cannot be combined with --header-index. If neither \
      is given, the header row is guessed automatically from the sheet's own layout \
      (title/notes rows are detected and skipped)."
  ) ]
  pub top: Option<u32>,
  #[clap(
    short = 'b', long, value_parser, conflicts_with = "body_index",
    help = "Row number, 1-based, where actual data begins -- if there's a gap below the header row",
    long_help = "Row number, 1-based, where actual data begins, if there's a gap below \
      the header row -- e.g. a blank or subtitle row before the real data starts. Rows \
      between the header row and this one are skipped entirely -- neither captured as \
      headers nor as data. Equivalent to --body-index but 1-based. Defaults to \
      immediately after the header row when unset. Setting it equal to the header row is \
      valid -- e.g. a CSV with predefined/external headers (via --keys) and \
      --omit-header, where no line is consumed as a header at all; only a value strictly \
      before the header row is rejected and falls back to the default. Cannot be \
      combined with --body-index."
  ) ]
  pub body_start: Option<u32>,

  #[clap(long, value_parser, default_value_t = false, help = "Skip the header row; assign fallback keys (a, b, c... or c01, c02... -- see --colstyle) instead") ]
  pub omit_header: bool, // no short flag: -o is --output's

  #[clap(
    short = 'k', long, value_parser, allow_hyphen_values = true,
    help = "Column overrides: source_key[:new_key][|format[|default]], comma-separated; a leading \"-\" (e.g. \"-colour\") suppresses that column instead",
    long_help = "Comma-separated list of column overrides, each in the form \
      source_key[:new_key][|format[|default]]. source_key is matched against the column's \
      natural (auto-detected, snake_cased) header key wherever that column actually is, so \
      you only need to list the columns you want to change; an unmatched source_key is \
      silently ignored. Omit :new_key to change only the format/default and keep the \
      natural name. Examples: \"start_date|date\" casts start_date to a date; \
      \"start_date:start|date\" also renames it to start; \"a:b|int,c:d|text\" mixes \
      multiple overrides in one value. An entry starting with \"-\" suppresses that \
      column outright, e.g. \"-colour\" (matched against the natural header key, or the \
      A1 letter/R1C1 number in --a1/--r1c1 mode -- \"-d\" or \"-4\"); \
      multiple suppressions can be comma-separated with everything else, e.g. \
      \"-colour,start_date|date\"."
  ) ]
  pub keys: Option<String>,

  #[clap(
    short = 'X', long = "exclude-null", value_parser, default_value_t = false,
    help = "Drop any key whose value is JSON null from the output, instead of \"key\": null",
    long_help = "Drop any key whose value is JSON null from the output, recursively through \
      nested objects/arrays too, instead of emitting \"key\": null. Only ever targets \
      genuine null -- an empty string is a different, deliberate value and is left alone. \
      Off by default; existing output is unchanged unless this is set."
  ) ]
  pub exclude_null: bool,

  #[clap(
    short = 'c', long, value_parser,
    help = "Fallback naming style for columns with no usable header: a1 or c01[:mode]",
    long_help = "Overrides the fallback column-naming convention for columns without a \
      usable header, in the form style[:mode]. style is a1 for spreadsheet-style letters \
      (a, b, ... z, aa, ab, ...) or c01/n/r1/r1c1 for zero-padded numbers (c01, c02, ...). \
      mode controls whether this replaces *every* column's name (\"all\", or the default \
      when :mode is omitted) or only fills in for columns lacking a real header (anything \
      else, e.g. \"a1:auto\")."
  ) ]
  pub colstyle: Option<String>,

  #[clap(
    short = 'A', long, short_alias = 'a', value_parser, default_value_t = false,
    help = "Shorthand for --colstyle a1 -- every column named by its spreadsheet-style letter (a, b, ... z, aa, ab, ...) instead of its snake_cased header text",
    long_help = "Shorthand for --colstyle a1, useful for --keys overrides that match by \
      column letter rather than the natural snake_cased header (e.g. \"b:sales.north\" \
      instead of needing the header's own text). -a is an alias. Ignored if --colstyle \
      is also given explicitly -- that always takes precedence over this shorthand."
  ) ]
  pub a1: bool,

  #[clap(
    short = 'R', long = "r1c1", value_parser, default_value_t = false,
    help = "Shorthand for --colstyle c01 -- every column named by its zero-padded number (c01, c02, ...) instead of its snake_cased header text",
    long_help = "Shorthand for --colstyle c01, useful for --keys overrides that match by \
      column number rather than the natural snake_cased header. -r already means --rows, \
      hence the capital -R here. Ignored if --colstyle is also given explicitly -- that \
      always takes precedence over this shorthand."
  ) ]
  pub r1c1: bool,

  #[clap(
    short = 'd', long, value_parser, default_value_t = false,
    help = "Stream rows to a JSONL export file instead of returning them directly (for large files)",
    long_help = "For large files: streams rows straight to a .jsonl file one at a time \
      rather than holding them all in memory. The file defaults to a random-UUID filename \
      under EXPORT_FILE_DIRECTORY (a .env variable, default ./); use --output/-o to name it \
      yourself. On Linux and macOS, this also hands the export off to a detached background \
      process and returns control to the shell immediately; on Windows it falls back to the \
      same in-process, streamed-but-blocking behavior, still memory-efficient, just not \
      backgrounded."
  ) ]
  pub deferred: bool,

  #[clap(short = 'o', long, value_parser, help = "Export file path for --deferred (overrides the random UUID filename); has no effect without --deferred") ]
  pub output: Option<String>,

    // Indexed options
  #[clap(short, long, value_parser, default_value_t = 0, help = "Sheet index, 0-based (0 is the first sheet), same as --number but 0-based") ]
  pub index: u32,
  #[clap(
    long, value_parser,
    help = "Header row index, 0-based (0 is the first row) -- same as --top but 0-based",
    long_help = "Header row index, 0-based (0 is the first row), if the headers aren't \
      on the first row. Equivalent to --top but 0-based -- this is the raw value passed \
      straight through to the underlying library's OptionSet.header_row. Cannot be \
      combined with --top. If neither is given, the header row is guessed automatically \
      from the sheet's own layout (title/notes rows are detected and skipped)."
  ) ]
  pub header_index: Option<u32>,

  #[clap(
    long, value_parser, conflicts_with = "body_start",
    help = "Row index, 0-based, where actual data begins -- same as --body-start but 0-based",
    long_help = "Row index, 0-based, where actual data begins -- same as --body-start but \
      0-based. This is the raw value passed straight through to the underlying library's \
      OptionSet.data_row_index. Cannot be combined with --body-start."
  ) ]
  pub body_index: Option<u32>,

  #[clap(long, value_parser, default_value_t = false, help = "Print processing time, and extra diagnostic detail on error") ]
  pub debug: bool,

  #[clap(long, value_parser, default_value_t = false, help = "Format date-time columns as dates only, with no time component") ]
  pub date_only: bool,

  #[clap(long, value_parser, default_value_t = false, help = "Format date-time columns as times only, with no date component (--date-only wins if both are set)") ]
  pub time_only: bool,

  #[clap(long, value_parser, default_value_t = false, help = "Format date-time columns as hours:minutes only, discarding seconds and any date component (--date-only/--time-only win if set too)") ]
  pub hm_only: bool,

  #[clap(long, value_parser, default_value_t = false, help = "Format date-time columns without milliseconds or a trailing Z, e.g. 2026-07-18T18:07:34 (--date-only/--time-only/--hm-only win if set too)") ]
  pub simple: bool,

  #[clap(long, value_parser, default_value_t = false, help = "Parse decimal commas when converting formatted strings to numbers") ]
  pub euro_number_format: bool,

  // Internal: set when this invocation IS the detached background worker spawned by a
  // user-facing --deferred run, so it knows to do the actual (blocking, from its own
  // point of view) export work instead of spawning yet another worker. Not for direct use.
  #[clap(long, hide = true, default_value_t = false)]
  pub background_worker: bool,

}

/// Column overrides whose --keys source is a placeholder pattern (`"file_[n]"`,
/// `"sales_{region}"`) can't be resolved into real Columns yet -- doing so needs the
/// actual column headers, which from_args (synchronous, no file access) doesn't have.
/// Returned alongside the OptionSet for the caller to expand once it does (see
/// key_pattern::expand_pattern_entry), after a header-only read.
pub type PendingKeyPattern = (String, String, Format);

/// A raw "-identifier" from --keys ("colname", "d", "4") -- like PendingKeyPattern,
/// can't resolve into a real Column (KeySegment::Excluded) until the real headers are
/// known, since interpreting the identifier depends on which colstyle mode is active
/// (natural key, A1 letter, or R1C1 number).
pub type PendingSuppression = String;

pub trait FromArgs {
    fn from_args(args: &Args) -> Result<(Self, Vec<PendingKeyPattern>, Vec<PendingSuppression>), String> where Self: Sized;
}

impl FromArgs for OptionSet {
    fn from_args(args: &Args) -> Result<(Self, Vec<PendingKeyPattern>, Vec<PendingSuppression>), String> {

    // --keys entries are `source_key[:new_key][|format[|default]]`. source_key is matched
    // against each column's natural (auto-detected, snake_cased) header key, wherever that
    // column actually is -- so overriding one field out of many doesn't require padding
    // out the columns ahead of it with empty entries. A source_key that doesn't match any
    // column in the file is silently ignored (e.g. a typo, or the wrong sheet/file).
    // The key mapping (source_key[:new_key]) and the datatype override (format[|default])
    // are separated by "|", e.g. "weight_kg:weight|int" or "weight_kg|int" (no rename).
    let mut columns: Vec<Column> = vec![];
    let mut pending_key_patterns: Vec<(String, String, Format)> = vec![];
    let mut pending_suppressions: Vec<String> = vec![];
    if let Some(k_string) = args.keys.clone() {
      let split_parts = k_string.to_parts(",");
      for ck in split_parts {
        // to_parts (not to_segments) is required throughout here: to_segments collapses
        // empty segments (e.g. "weight_kg||0" would lose the empty format slot entirely),
        // which would silently misalign the default onto the wrong field.
        let pipe_parts = ck.to_parts("|");
        let key_part = pipe_parts.first().cloned().unwrap_or_default();
        let key_sub_parts = key_part.to_parts(":");
        let raw_source = key_sub_parts.first().map(|s| s.trim()).filter(|s| !s.is_empty());
        let Some(raw_source) = raw_source else {
          continue;
        };
        // "-colname"/"-d"/"-4": suppress a column outright. The identifier after the "-"
        // is resolved against the real column headers later (once they're known -- see
        // main.rs), the same way placeholder patterns are, since interpreting it (natural
        // key vs A1 letter vs R1C1 number) depends on which colstyle mode is active.
        if let Some(identifier) = raw_source.strip_prefix('-').map(|s| s.trim()).filter(|s| !s.is_empty()) {
          pending_suppressions.push(identifier.to_string());
          continue;
        }
        // A placeholder pattern ("file_[n]", "sales_{region}", "download_*") must be
        // detected on the *raw* source text -- to_snake_case (used below for the
        // ordinary case) silently strips "[", "]", "{", "}", "*" entirely (e.g.
        // "file_[n]" -> "file_n"), which would destroy the pattern before it could ever
        // be recognised.
        if (raw_source.contains('[') && raw_source.ends_with(']'))
          || (raw_source.contains('{') && raw_source.ends_with('}'))
          || (raw_source.ends_with('*') && raw_source.len() > 1) {
          let raw_target = key_sub_parts.get(1).map(|s| s.trim()).filter(|s| !s.is_empty());
          let Some(raw_target) = raw_target else {
            return Err(format!("invalid --keys entry '{}': a placeholder pattern needs an explicit target path (e.g. \"file_[n]:files[]\")", ck));
          };
          let fmt = pipe_parts.get(1)
            .map(|s| Format::from_str(s).unwrap_or(Format::Auto))
            .unwrap_or(Format::Auto);
          pending_key_patterns.push((raw_source.to_string(), raw_target.to_string(), fmt));
          continue;
        }
        let source_key = Some(raw_source.to_snake_case()).filter(|s| !s.is_empty());
        let Some(source_key) = source_key else {
          continue;
        };
        let new_key = key_sub_parts.get(1)
          .map(|s| s.trim())
          .filter(|s| !s.is_empty())
          .map(|s| s.to_snake_case());
        let fmt = pipe_parts.get(1)
          .map(|s| Format::from_str(s).unwrap_or(Format::Auto))
          .unwrap_or(Format::Auto);
        let mut default_val = None;
        if let Some(def_val) = pipe_parts.get(2).filter(|s| !s.is_empty()) {
          default_val = match fmt {
            Format::Integer => {
              let parsed = i128::from_str(def_val).map_err(|_| {
                format!("invalid --keys entry '{}': '{}' is not a valid integer default", ck, def_val)
              })?;
              let num = Number::from_i128(parsed).ok_or_else(|| {
                format!("invalid --keys entry '{}': integer default '{}' is out of range", ck, def_val)
              })?;
              Some(Value::Number(num))
            },
            Format::Boolean => {
              is_truthy_core(def_val, false).map(Value::Bool)
            },
            _ => Some(Value::String(def_val.clone()))
          };
        }

        columns.push(Column::from_source_key_with_format(&source_key, new_key.as_deref(), fmt, default_val, DateTimeMode::Full, false));
      }
    }
    let read_mode = if args.preview {
        ReadMode::PreviewMultiple
    } else if args.deferred {
        ReadMode::Async
    } else {
        ReadMode::Sync
    };
    let mut field_mode = FieldNameMode::AutoA1;
    // -a/--a1 is just a default for --colstyle, not a separate code path -- an explicit
    // --colstyle still wins if somehow both are given, and everything below (mode
    // parsing, r1/r1c1 handling) applies identically either way.
    let colstyle = args.colstyle.clone()
      .or_else(|| args.a1.then(|| "a1".to_string()))
      .or_else(|| args.r1c1.then(|| "c01".to_string()));
    if let Some(colstyle) = colstyle {
        let (col_key, col_mode) = colstyle.to_head_tail(":");
        if let Some(col_key) = col_key {
            // No ":mode" suffix at all (e.g. just "-c c01") means "apply to every field",
            // same as an explicit "-c c01:all" -- not "leave the default A1-auto style
            // in place", which is what a bare value used to do (silently, since matching
            // both halves of the tuple failed and the whole block was skipped).
            let override_all = col_mode.is_none_or(|m| m.starts_with_ci_alphanum("all"));
            let colkey = col_key.strip_non_alphanum();
            // r1c1 and r1 are also interpreted as c1
            let col_key = if colkey.starts_with_ci("r1") {
                "c1"
            } else {
                &colkey
            };
            field_mode = FieldNameMode::from_key(col_key, override_all);
        }
    }
    let jsonl = args.lines || read_mode.is_async();
    let selected = args.sheet.clone().map(|sheet| sheet.to_segments(","));
    let max = if args.preview {
        Some(DEFAULT_MAX_FOR_PREVIEW)
    } else {
        args.max
    };
    // --number/-n is 1-based ("the 1st sheet"); the core library only knows --index's
    // 0-based position, so this is just --number - 1 wherever it's set. --index and
    // --number are mutually exclusive (see conflicts_with above), so there's no
    // precedence to resolve between them.
    let index = match args.number {
        Some(0) => return Err("invalid --number value: sheets are numbered starting at 1".to_string()),
        Some(n) => n - 1,
        None => args.index,
    };
    // Same 1-based/0-based pairing as --number/--index, but for the header row: --top is
    // 1-based, --header-index is the library's own 0-based OptionSet.header_row directly.
    // Unlike --index (which always has a concrete default), leaving *both* unset here
    // stays None -- that's what tells the library to auto-detect the header row instead
    // of assuming row 0.
    let header_row = match (args.top, args.header_index) {
        (Some(0), _) => return Err("invalid --top value: rows are numbered starting at 1".to_string()),
        (Some(t), _) => Some((t - 1) as usize),
        (None, Some(hi)) => Some(hi as usize),
        (None, None) => None,
    };
    // --body-start (1-based) / --body-index (0-based) map to OptionSet.data_row_index the
    // same way. Unlike --index/--number, both sides are optional here -- None means
    // "unset", matching data_row_index's own Option<usize>.
    let data_row_index = match (args.body_start, args.body_index) {
        (Some(0), _) => return Err("invalid --body-start value: rows are numbered starting at 1".to_string()),
        (Some(b), _) => Some((b - 1) as usize),
        (None, Some(bi)) => Some(bi as usize),
        (None, None) => None,
    };
    // --date-only/--time-only/--hm-only are mutually exclusive row-wide defaults,
    // checked in this order of precedence when more than one is somehow set.
    let datetime_mode = if args.date_only {
        DateTimeMode::DateOnly
    } else if args.time_only {
        DateTimeMode::TimeOnly
    } else if args.hm_only {
        DateTimeMode::HmOnly
    } else if args.simple {
        DateTimeMode::Simple
    } else {
        DateTimeMode::Full
    };
    Ok((OptionSet {
        selected,
        indices: vec![index],
        path: args.path.clone(),
        max,
        header_row,
        data_row_index,
        // spread-cli has no --header-span flag yet -- always a single header row for now.
        header_row_span: 1,
        // spread-cli's own default UX: guess the header/data row from the sheet's layout
        // when neither --top/--header-index nor --body-start/--body-index is given,
        // rather than the library's own plain "assume row 0" default.
        detect_header: true,
        omit_header: args.omit_header,
        rows: crate::RowOptionSet {
            decimal_comma: args.euro_number_format,
            datetime_mode,
            omit_null_values: args.exclude_null,
            columns,
        },
        jsonl,
        read_mode,
        field_mode
    }, pending_key_patterns, pending_suppressions))
    }
}