Parse ``/export`` command arguments. Supported flags: ``--format `` or ``-f ``. Returns parsed filename, format, and optional error string.
(args: str)
| 158 | |
| 159 | |
| 160 | def parse_export_args(args: str) -> ParsedExportArgs: |
| 161 | """Parse ``/export`` command arguments. |
| 162 | |
| 163 | Supported flags: ``--format <value>`` or ``-f <value>``. |
| 164 | |
| 165 | Returns parsed filename, format, and optional error string. |
| 166 | """ |
| 167 | tokens, tok_error = _tokenize_export_args(args) |
| 168 | if tok_error: |
| 169 | return ParsedExportArgs(error=tok_error) |
| 170 | |
| 171 | if len(tokens) == 0: |
| 172 | return ParsedExportArgs() |
| 173 | |
| 174 | fmt: Optional[ExportFormat] = None |
| 175 | error: Optional[str] = None |
| 176 | filename_tokens: List[str] = [] |
| 177 | |
| 178 | i = 0 |
| 179 | n = len(tokens) |
| 180 | while i < n: |
| 181 | token = tokens[i] |
| 182 | if not token.quoted and token.value == "--": |
| 183 | filename_tokens.extend(t.value for t in tokens[i + 1 :]) |
| 184 | break |
| 185 | if not token.quoted and token.value in ("--format", "-f"): |
| 186 | i += 1 |
| 187 | value = tokens[i].value if i < n else None |
| 188 | if not value: |
| 189 | error = f"Missing value for {token.value}. {SUPPORTED_FORMATS}" |
| 190 | break |
| 191 | normalized = normalize_export_format(value) |
| 192 | if not normalized: |
| 193 | error = f"Unsupported export format: {value}. {SUPPORTED_FORMATS}" |
| 194 | break |
| 195 | fmt = normalized |
| 196 | elif ( |
| 197 | not token.quoted |
| 198 | and token.value.startswith("-") |
| 199 | and token.value != "-" |
| 200 | ): |
| 201 | error = ( |
| 202 | f"Unsupported export option: {token.value}. " |
| 203 | "Supported options: --format, -f." |
| 204 | ) |
| 205 | break |
| 206 | else: |
| 207 | filename_tokens.append(token.value) |
| 208 | i += 1 |
| 209 | |
| 210 | filename = " ".join(filename_tokens) if filename_tokens else None |
| 211 | |
| 212 | if error: |
| 213 | return ParsedExportArgs(filename=filename, format=fmt, error=error) |
| 214 | |
| 215 | return ParsedExportArgs(filename=filename, format=fmt) |
| 216 | |
| 217 |