global parser
makes external integration with e.g. `shtab` easy
This commit is contained in:
parent
c0c0533a5e
commit
b0406ca2c7
1 changed files with 62 additions and 62 deletions
|
|
@ -8,8 +8,6 @@ from importlib.metadata import entry_points
|
||||||
from .__about__ import __version__
|
from .__about__ import __version__
|
||||||
from ._markitdown import MarkItDown, DocumentConverterResult
|
from ._markitdown import MarkItDown, DocumentConverterResult
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Convert various file formats to markdown.",
|
description="Convert various file formats to markdown.",
|
||||||
prog="markitdown",
|
prog="markitdown",
|
||||||
|
|
@ -32,7 +30,7 @@ def main():
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-o",
|
"-o",
|
||||||
"--output",
|
"--output",
|
||||||
dest="filename",
|
metavar="outfilename",
|
||||||
help="if unspecified, defaults to stdout",
|
help="if unspecified, defaults to stdout",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -62,23 +60,25 @@ def main():
|
||||||
"filename", nargs="?", help="if unspecified, defaults to stdin"
|
"filename", nargs="?", help="if unspecified, defaults to stdin"
|
||||||
)
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
def main(args=None):
|
||||||
|
args = parser.parse_args(args)
|
||||||
|
|
||||||
if args.list_plugins:
|
if args.list_plugins:
|
||||||
# List installed plugins, then exit
|
# List installed plugins, then exit
|
||||||
print("Installed MarkItDown 3rd-party Plugins:\n")
|
print("Installed MarkItDown 3rd-party Plugins:\n")
|
||||||
plugin_entry_points = list(entry_points(group="markitdown.plugin"))
|
plugin_entry_points = list(entry_points(group="markitdown.plugin"))
|
||||||
if len(plugin_entry_points) == 0:
|
if plugin_entry_points:
|
||||||
print(" * No 3rd-party plugins installed.")
|
|
||||||
print(
|
|
||||||
"\nFind plugins by searching for the hashtag #markitdown-plugin on GitHub.\n"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
for entry_point in plugin_entry_points:
|
for entry_point in plugin_entry_points:
|
||||||
print(f" * {entry_point.name:<16}\t(package: {entry_point.value})")
|
print(f" * {entry_point.name:<16}\t(package: {entry_point.value})")
|
||||||
print(
|
print(
|
||||||
"\nUse the -p (or --use-plugins) option to enable 3rd-party plugins.\n"
|
"\nUse the -p (or --use-plugins) option to enable 3rd-party plugins.\n"
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
print("No 3rd-party plugins installed.")
|
||||||
|
print(
|
||||||
|
"\nFind plugins by searching for the hashtag #markitdown-plugin on GitHub.\n"
|
||||||
|
)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if args.use_docintel:
|
if args.use_docintel:
|
||||||
|
|
@ -94,10 +94,10 @@ def main():
|
||||||
else:
|
else:
|
||||||
markitdown = MarkItDown(enable_plugins=args.use_plugins)
|
markitdown = MarkItDown(enable_plugins=args.use_plugins)
|
||||||
|
|
||||||
if args.filename is None:
|
if args.filename:
|
||||||
result = markitdown.convert_stream(sys.stdin.buffer)
|
|
||||||
else:
|
|
||||||
result = markitdown.convert(args.filename)
|
result = markitdown.convert(args.filename)
|
||||||
|
else:
|
||||||
|
result = markitdown.convert_stream(sys.stdin.buffer)
|
||||||
|
|
||||||
if args.output:
|
if args.output:
|
||||||
with open(args.output, "w", encoding="utf-8") as f:
|
with open(args.output, "w", encoding="utf-8") as f:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue