main = do
  Options arrayDelim expr outfile debugKeyPaths maxLen <- O.execParser opts
  x <- BL.getContents 
  ct <- getPOSIXTime
  let xs :: [Value]
      xs = decodeStream x
      ks = parseKeyPath $ T.pack expr
      ks' :: [[Key]]
      ks' = [k | KeyPath k _ <- ks]
      arrayDelim' = T.pack arrayDelim
      hs :: [Text] -- header labels
      hs = map keyPathToHeader ks
  -- extract JSON
  let xs' :: [[Value]]
      xs' = map (evalToValues arrayDelim' ks') xs
      headerCells = map mkHeaderCell hs
      headerIndexedCells :: [((Int,Int), Cell)]
      headerIndexedCells = zip [(1, x) | x <- [1..]] headerCells
      rows :: [[Cell]]
      rows = map (map (jsonToCell . truncateStr maxLen)) xs'
      rowsIndexedCells :: [[((Int,Int), Cell)]]
      rowsIndexedCells = map mkRowIndexedCells $ zip [2..] rows
      allCells = concat (headerIndexedCells:rowsIndexedCells)
      cellMap :: CellMap
      cellMap = M.fromList allCells

  when debugKeyPaths $ do
     Prelude.putStrLn $ "Key Paths: " ++ show ks
     print hs
     print headerCells
     print headerIndexedCells
     exitSuccess
  let ws = def { _wsCells = cellMap }
  let xlsx = def { _xlSheets = M.fromList [("test", ws)] }
  if outfile == "-"
  then L8.putStr $ fromXlsx ct xlsx
  else L.writeFile outfile $ fromXlsx ct xlsx
main = do
  Options arrayDelim expr outfile debugKeyPaths maxLen <- O.execParser opts
  x <- BL.getContents 
  ct <- getPOSIXTime
  let xs :: [Value]
      xs = decodeStream x
      ks = parseKeyPath $ T.pack expr
      ks' :: [[Key]]
      ks' = [k | KeyPath k _ <- ks]
      arrayDelim' = T.pack arrayDelim
      hs :: [Text] -- header labels
      hs = map keyPathToHeader ks
  -- extract JSON
  let xs' :: [[Value]]
      xs' = map (evalToValues arrayDelim' ks') xs
      headerCells = map mkHeaderCell hs
      headerIndexedCells :: [((Int,Int), Cell)]
      headerIndexedCells = zip [(1, x) | x <- [1..]] headerCells
      rows :: [[Cell]]
      rows = map (map (jsonToCell . truncateStr maxLen)) xs'
      rowsIndexedCells :: [[((Int,Int), Cell)]]
      rowsIndexedCells = map mkRowIndexedCells $ zip [2..] rows
      allCells = concat (headerIndexedCells:rowsIndexedCells)
      cellMap :: CellMap
      cellMap = M.fromList allCells

  when debugKeyPaths $ do
     Prelude.putStrLn $ "Key Paths: " ++ show ks
     print hs
     print headerCells
     print headerIndexedCells
     exitSuccess
  let ws = def { _wsCells = cellMap }
  let xlsx = def { _xlSheets = M.fromList [("test", ws)] }
  if outfile == "-"
  then L8.putStr $ fromXlsx ct xlsx
  else L.writeFile outfile $ fromXlsx ct xlsx
main = do
  Options arrayDelim expr outfile debugKeyPaths maxLen <- O.execParser opts
  x <- BL.getContents 
  ct <- getPOSIXTime
  let xs :: [Value]
      xs = decodeStream x
      ks = parseKeyPath $ T.pack expr
      ks' :: [[Key]]
      ks' = [k | KeyPath k _ <- ks]
      arrayDelim' = T.pack arrayDelim
      hs :: [Text] -- header labels
      hs = map keyPathToHeader ks
  -- extract JSON
  let xs' :: [[Value]]
      xs' = map (evalToValues arrayDelim' ks') xs
      headerCells = map mkHeaderCell hs
      headerIndexedCells :: [((Int,Int), Cell)]
      headerIndexedCells = zip [(1, x) | x <- [1..]] headerCells
      rows :: [[Cell]]
      rows = map (map (jsonToCell . truncateStr maxLen)) xs'
      rowsIndexedCells :: [[((Int,Int), Cell)]]
      rowsIndexedCells = map mkRowIndexedCells $ zip [2..] rows
      allCells = concat (headerIndexedCells:rowsIndexedCells)
      cellMap :: CellMap
      cellMap = M.fromList allCells

  when debugKeyPaths $ do
     Prelude.putStrLn $ "Key Paths: " ++ show ks
     print hs
     print headerCells
     print headerIndexedCells
     exitSuccess
  let ws = def { _wsCells = cellMap }
  let xlsx = def { _xlSheets = M.fromList [("test", ws)] }
  if outfile == "-"
  then L8.putStr $ fromXlsx ct xlsx
  else L.writeFile outfile $ fromXlsx ct xlsx