| 111 | } |
| 112 | |
| 113 | function generateOptionsFromFlags(parsed: ParsedArgs): GenerateOptions { |
| 114 | const p = parsed.positional; |
| 115 | if (p.length === 0) { |
| 116 | console.error("$P generate: missing <input.md>"); |
| 117 | console.error("Usage: $P generate <input.md> [output.pdf] [options]"); |
| 118 | process.exit(ExitCode.BadArgs); |
| 119 | } |
| 120 | const f = parsed.flags; |
| 121 | const booleanFlag = (key: string, def: boolean): boolean => { |
| 122 | if (f[key] === true) return true; |
| 123 | if (f[`no-${key}`] === true) return false; |
| 124 | return def; |
| 125 | }; |
| 126 | const to = typeof f.to === "string" ? f.to.toLowerCase() : "pdf"; |
| 127 | if (to !== "pdf" && to !== "html" && to !== "docx") { |
| 128 | console.error(`$P generate: invalid --to '${f.to}'. Expected pdf, html, or docx.`); |
| 129 | console.error("(--format is a --page-size alias, not the output format.)"); |
| 130 | process.exit(ExitCode.BadArgs); |
| 131 | } |
| 132 | return { |
| 133 | input: p[0], |
| 134 | output: p[1], |
| 135 | to: to as GenerateOptions["to"], |
| 136 | margins: f.margins as string | undefined, |
| 137 | marginTop: f["margin-top"] as string | undefined, |
| 138 | marginRight: f["margin-right"] as string | undefined, |
| 139 | marginBottom: f["margin-bottom"] as string | undefined, |
| 140 | marginLeft: f["margin-left"] as string | undefined, |
| 141 | pageSize: ((f["page-size"] ?? f.format) as any), |
| 142 | cover: f.cover === true, |
| 143 | toc: f.toc === true, |
| 144 | noChapterBreaks: f["no-chapter-breaks"] === true, |
| 145 | watermark: typeof f.watermark === "string" ? f.watermark : undefined, |
| 146 | headerTemplate: typeof f["header-template"] === "string" |
| 147 | ? f["header-template"] : undefined, |
| 148 | footerTemplate: typeof f["footer-template"] === "string" |
| 149 | ? f["footer-template"] : undefined, |
| 150 | confidential: booleanFlag("confidential", true), |
| 151 | pageNumbers: booleanFlag("page-numbers", true), |
| 152 | tagged: booleanFlag("tagged", true), |
| 153 | outline: booleanFlag("outline", true), |
| 154 | quiet: f.quiet === true, |
| 155 | verbose: f.verbose === true, |
| 156 | allowNetwork: f["allow-network"] === true, |
| 157 | strict: f.strict === true, |
| 158 | title: typeof f.title === "string" ? f.title : undefined, |
| 159 | author: typeof f.author === "string" ? f.author : undefined, |
| 160 | date: typeof f.date === "string" ? f.date : undefined, |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | function previewOptionsFromFlags(parsed: ParsedArgs): PreviewOptions { |
| 165 | const p = parsed.positional; |