import sys
import argparse

from elasticsearch import Elasticsearch

from annotator.reindexer import Reindexer

description = """
Reindex an elasticsearch index.

Performs reindexing and/or aliasing of an index.

WARNING: Documents that are created while reindexing may be lost!
"""

def main(argv):
    argparser = argparse.ArgumentParser(description=description)
    argparser.add_argument('host', help="Elasticsearch server, host[:port]")
    argparser.add_argument('--reindex', action='append', nargs=2, metavar=('old_index', 'new_index'),
                           help="Reindex old_index to new_index")
    argparser.add_argument('--alias', action='append', nargs=2, metavar=('index', 'alias'),
                           help="Create an alias for an index")
    args = argparser.parse_args()

    host = args.host
    reindex = args.reindex or []
    alias = args.alias or []

    conn = Elasticsearch([host])

    reindexer = Reindexer(conn, interactive=True)

    for (old_index, new_index) in reindex:
        reindexer.reindex(old_index, new_index)
    for (index, alias) in alias:
        reindexer.alias(index, alias)

if __name__ == '__main__':
    main(sys.argv)

import sys
import argparse

from elasticsearch import Elasticsearch

from annotator.reindexer import Reindexer

description = """
Reindex an elasticsearch index.

WARNING: Documents that are created while reindexing may be lost!
"""

def main(argv):
    argparser = argparse.ArgumentParser(description=description)
    argparser.add_argument('old_index', help="Index to read from")
    argparser.add_argument('new_index', help="Index to write to")
    argparser.add_argument('--host', help="Elasticsearch server, host[:port]")
    argparser.add_argument('--alias', help="Alias for the new index")
    args = argparser.parse_args()

    host = args.host
    old_index = args.old_index
    new_index = args.new_index
    alias = args.alias

    conn = Elasticsearch([host])

    reindexer = Reindexer(conn, interactive=True)

    reindexer.reindex(old_index, new_index)

    if alias:
        reindexer.alias(new_index, alias)

if __name__ == '__main__':
    main(sys.argv)

Simplify command line arguments