ArchiveBox/archivebox/cli/archivebox.py

64 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# archivebox [command]
__package__ = 'archivebox.cli'
__command__ = 'archivebox'
import sys
import argparse
2019-04-27 21:26:24 +00:00
from typing import Optional, List, IO
2019-04-27 21:26:24 +00:00
from . import list_subcommands, run_subcommand
from ..config import OUTPUT_DIR
2019-04-27 21:26:24 +00:00
def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional[str]=None) -> None:
subcommands = list_subcommands()
parser = argparse.ArgumentParser(
prog=__command__,
description='ArchiveBox: The self-hosted internet archive',
add_help=False,
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--help', '-h',
action='store_true',
help=subcommands['help'],
)
group.add_argument(
'--version',
action='store_true',
help=subcommands['version'],
)
group.add_argument(
"subcommand",
type=str,
help= "The name of the subcommand to run",
nargs='?',
choices=subcommands.keys(),
default=None,
)
parser.add_argument(
2019-04-27 21:26:24 +00:00
"subcommand_args",
help="Arguments for the subcommand",
nargs=argparse.REMAINDER,
)
2019-04-27 21:26:24 +00:00
command = parser.parse_args(args or ())
2019-04-27 21:26:24 +00:00
if command.help or command.subcommand is None:
command.subcommand = 'help'
if command.version:
command.subcommand = 'version'
2019-04-27 21:26:24 +00:00
run_subcommand(
subcommand=command.subcommand,
subcommand_args=command.subcommand_args,
stdin=stdin,
pwd=pwd or OUTPUT_DIR,
)
2019-04-03 05:54:15 +00:00
if __name__ == '__main__':
2019-04-27 21:26:24 +00:00
main(args=sys.argv[1:], stdin=sys.stdin)