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 ._markitdown import MarkItDown, DocumentConverterResult
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert various file formats to markdown.",
|
||||
prog="markitdown",
|
||||
|
|
@ -32,7 +30,7 @@ def main():
|
|||
parser.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
dest="filename",
|
||||
metavar="outfilename",
|
||||
help="if unspecified, defaults to stdout",
|
||||
)
|
||||
parser.add_argument(
|
||||
|
|
@ -62,23 +60,25 @@ def main():
|
|||
"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:
|
||||
# List installed plugins, then exit
|
||||
print("Installed MarkItDown 3rd-party Plugins:\n")
|
||||
plugin_entry_points = list(entry_points(group="markitdown.plugin"))
|
||||
if len(plugin_entry_points) == 0:
|
||||
print(" * No 3rd-party plugins installed.")
|
||||
print(
|
||||
"\nFind plugins by searching for the hashtag #markitdown-plugin on GitHub.\n"
|
||||
)
|
||||
else:
|
||||
if plugin_entry_points:
|
||||
for entry_point in plugin_entry_points:
|
||||
print(f" * {entry_point.name:<16}\t(package: {entry_point.value})")
|
||||
print(
|
||||
"\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)
|
||||
|
||||
if args.use_docintel:
|
||||
|
|
@ -94,10 +94,10 @@ def main():
|
|||
else:
|
||||
markitdown = MarkItDown(enable_plugins=args.use_plugins)
|
||||
|
||||
if args.filename is None:
|
||||
result = markitdown.convert_stream(sys.stdin.buffer)
|
||||
else:
|
||||
if args.filename:
|
||||
result = markitdown.convert(args.filename)
|
||||
else:
|
||||
result = markitdown.convert_stream(sys.stdin.buffer)
|
||||
|
||||
if args.output:
|
||||
with open(args.output, "w", encoding="utf-8") as f:
|
||||
|
|
|
|||
Loading…
Reference in a new issue