Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions packages/markitdown/src/markitdown/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT
import argparse
import sys
import os
import codecs
from typing import Any, Dict
from textwrap import dedent
Expand Down Expand Up @@ -98,13 +99,13 @@ def main():
"-e",
"--endpoint",
type=str,
help="Document Intelligence Endpoint. Required if using Document Intelligence.",
help="Document Intelligence Endpoint. Required if using Document Intelligence (or set MARKITDOWN_DOCINTEL_ENDPOINT).",
)

parser.add_argument(
"--cu-endpoint",
type=str,
help="Content Understanding Endpoint. Required if using --use-cu.",
help="Content Understanding Endpoint. Required if using --use-cu (or set MARKITDOWN_CU_ENDPOINT).",
)

parser.add_argument(
Expand Down Expand Up @@ -201,26 +202,32 @@ def main():
sys.exit(0)

if args.use_docintel:
if args.endpoint is None:
docintel_endpoint = args.endpoint or os.environ.get(
"MARKITDOWN_DOCINTEL_ENDPOINT"
)
if docintel_endpoint is None:
_exit_with_error(
"Document Intelligence Endpoint is required when using Document Intelligence."
"Document Intelligence Endpoint is required when using Document Intelligence. "
"Pass -e / --endpoint or set MARKITDOWN_DOCINTEL_ENDPOINT."
)
elif args.filename is None:
_exit_with_error("Filename is required when using Document Intelligence.")

markitdown = MarkItDown(
enable_plugins=args.use_plugins, docintel_endpoint=args.endpoint
enable_plugins=args.use_plugins, docintel_endpoint=docintel_endpoint
)
elif args.use_cu:
if args.cu_endpoint is None:
cu_endpoint = args.cu_endpoint or os.environ.get("MARKITDOWN_CU_ENDPOINT")
if cu_endpoint is None:
_exit_with_error(
"Content Understanding Endpoint (--cu-endpoint) is required when using --use-cu."
"Content Understanding Endpoint (--cu-endpoint) is required when using --use-cu. "
"Pass --cu-endpoint or set MARKITDOWN_CU_ENDPOINT."
)
elif args.filename is None:
_exit_with_error("Filename is required when using Content Understanding.")

cu_kwargs: Dict[str, Any] = {
"cu_endpoint": args.cu_endpoint,
"cu_endpoint": cu_endpoint,
}
if args.cu_analyzer is not None:
cu_kwargs["cu_analyzer_id"] = args.cu_analyzer
Expand Down
76 changes: 74 additions & 2 deletions packages/markitdown/tests/test_cli_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3 -m pytest
import subprocess
import sys
import os
from markitdown import __version__

# This file contains CLI tests that are not directly tested by the FileTestVectors.
Expand All @@ -8,7 +10,9 @@

def test_version() -> None:
result = subprocess.run(
["python", "-m", "markitdown", "--version"], capture_output=True, text=True
[sys.executable, "-m", "markitdown", "--version"],
capture_output=True,
text=True,
)

assert result.returncode == 0, f"CLI exited with error: {result.stderr}"
Expand All @@ -17,7 +21,9 @@ def test_version() -> None:

def test_invalid_flag() -> None:
result = subprocess.run(
["python", "-m", "markitdown", "--foobar"], capture_output=True, text=True
[sys.executable, "-m", "markitdown", "--foobar"],
capture_output=True,
text=True,
)

assert result.returncode != 0, f"CLI exited with error: {result.stderr}"
Expand All @@ -27,8 +33,74 @@ def test_invalid_flag() -> None:
assert "SYNTAX" in result.stderr, "Expected 'SYNTAX' to appear in STDERR"


def test_docintel_missing_endpoint_error() -> None:
result = subprocess.run(
[sys.executable, "-m", "markitdown", "--use-docintel"],
capture_output=True,
text=True,
env={"PATH": os.environ.get("PATH", "")},
)

assert result.returncode != 0
assert (
"MARKITDOWN_DOCINTEL_ENDPOINT" in result.stdout
or "MARKITDOWN_DOCINTEL_ENDPOINT" in result.stderr
)


def test_docintel_env_endpoint_fallback() -> None:
# When MARKITDOWN_DOCINTEL_ENDPOINT is set, missing endpoint check passes and it asks for filename
result = subprocess.run(
[sys.executable, "-m", "markitdown", "--use-docintel"],
capture_output=True,
text=True,
env={
**os.environ,
"MARKITDOWN_DOCINTEL_ENDPOINT": "https://example.cognitiveservices.azure.com",
},
)

assert result.returncode != 0
assert "Filename is required" in result.stdout or "Filename is required" in result.stderr


def test_cu_missing_endpoint_error() -> None:
result = subprocess.run(
[sys.executable, "-m", "markitdown", "--use-cu"],
capture_output=True,
text=True,
env={"PATH": os.environ.get("PATH", "")},
)

assert result.returncode != 0
assert (
"MARKITDOWN_CU_ENDPOINT" in result.stdout
or "MARKITDOWN_CU_ENDPOINT" in result.stderr
)


def test_cu_env_endpoint_fallback() -> None:
# When MARKITDOWN_CU_ENDPOINT is set, missing endpoint check passes and it asks for filename
result = subprocess.run(
[sys.executable, "-m", "markitdown", "--use-cu"],
capture_output=True,
text=True,
env={
**os.environ,
"MARKITDOWN_CU_ENDPOINT": "https://example.cognitiveservices.azure.com",
},
)

assert result.returncode != 0
assert "Filename is required" in result.stdout or "Filename is required" in result.stderr


if __name__ == "__main__":
"""Runs this file's tests from the command line."""
test_version()
test_invalid_flag()
test_docintel_missing_endpoint_error()
test_docintel_env_endpoint_fallback()
test_cu_missing_endpoint_error()
test_cu_env_endpoint_fallback()
print("All tests passed!")