import click
import requests
from click_repl import register_repl
from prompt_toolkit.shortcuts import print_tokens
from scrapinghub import Connection

from shub_cli.commands.job import get_job, get_jobs
from shub_cli.config.display import shub_not_configured_tokens, error_style, no_internet_connection_tokens
from shub_cli.util.display import display, display_jobs, display_log
from shub_cli.util.parse import parse_options
from shub_cli.util.scrapinghub import get_sh_api_key, get_sh_project

API_KEY = None
PROJECT = None


@click.group()
@click.option('-api', nargs=1, type=click.STRING, help='ScrapingHub API KEY')
@click.option('-project', nargs=1, type=click.STRING, help='Scrapinghub Project Id')
def main(api, project):
    """
    Main CLI Entrypoint

    :param api: A ScrapingHub API KEY
    :param project: A ScrapingHub PROJECT ID
    """
    global API_KEY
    global PROJECT
    API_KEY = get_sh_api_key(api)
    PROJECT = get_sh_project(project)
    if API_KEY is None or PROJECT is None:
        print_tokens(shub_not_configured_tokens, style=error_style)
        exit()


@main.command()
@click.option('-id', type=click.STRING, help='Job Id')
@click.option('--with-log', is_flag=True, help='Presents the log of a job')
def job(id, with_log):
    """
    Allow user to see information of a job

    :param id: Job id
    :param with_log: A flag that tells if the program should or not exhibit the log messages
    """
    conn = Connection(apikey=API_KEY)
    try:
        job = get_job(id, conn, PROJECT)
        display(job, click)
        if with_log:
            display_log(job, click)

    except requests.exceptions.ConnectionError:
        print_tokens(no_internet_connection_tokens, style=error_style)


@main.command()
@click.option('-tag', nargs=1, type=click.STRING, help='Tag that the jobs must contain.')
@click.option('-lacks', nargs=1, type=click.STRING, help='Tag that the jobs can not contain.')
@click.option('-spider', nargs=1, type=click.STRING, help='Name of the spider.')
@click.option('-state', nargs=1, type=click.STRING, help='State of the job.')
@click.option('-count', nargs=1, type=click.INT, help='Quantity of results.', default=10)
def jobs(tag, lacks, spider, state, count):
    """
    Allow user to see information of a number of Jobs

    :param tag: The tag the jobs must contain
    :param lacks: Tag that the jobs can not contain.
    :param spider: Name of the spider.
    :param state: State of the job.
    :param count: Quantity of jobs to be exhibited.
    """
    params = parse_options(tag, lacks, spider, state)
    conn = Connection(apikey=API_KEY)
    try:
        jobs = get_jobs(params, conn, PROJECT, count)
        display_jobs(jobs, click)
    except requests.exceptions.ConnectionError:
        print_tokens(no_internet_connection_tokens, style=error_style)
        print()


register_repl(main)

import click
import requests
from click_repl import register_repl
from prompt_toolkit.shortcuts import print_tokens
from scrapinghub import Connection

from shub_cli.commands.job import get_job, get_jobs
from shub_cli.config.display import shub_not_configured_tokens, error_style, no_internet_connection_tokens
from shub_cli.util.display import display, display_jobs, display_log
from shub_cli.util.parse import parse_options
from shub_cli.util.scrapinghub import get_sh_api_key, get_sh_project

API_KEY = None
PROJECT = None


@click.group()
@click.option('-api', nargs=1, type=click.STRING, help='ScrapingHub API KEY')
@click.option('-project', nargs=1, type=click.STRING, help='Scrapinghub Project Id')
def main(api, project):
    """Main CLI Entrypoint"""
    global API_KEY
    global PROJECT
    API_KEY = get_sh_api_key(api)
    PROJECT = get_sh_project(project)
    if API_KEY is None or PROJECT is None:
        print_tokens(shub_not_configured_tokens, style=error_style)
        exit()


@main.command()
@click.option('-id', type=click.STRING, help='Job Id')
@click.option('--with-log', is_flag=True, help='Presents the log of a job')
def job(id, with_log):
    """See information of a job"""
    conn = Connection(apikey=API_KEY)
    try:
        job = get_job(id, conn, PROJECT)
        display(job, click)
        if with_log:
            display_log(job, click)

    except requests.exceptions.ConnectionError:
        print_tokens(no_internet_connection_tokens, style=error_style)


@main.command()
@click.option('-tag', nargs=1, type=click.STRING, help='Tag that the jobs must contain.')
@click.option('-lacks', nargs=1, type=click.STRING, help='Tag that the jobs can not contain.')
@click.option('-spider', nargs=1, type=click.STRING, help='Name of the spider.')
@click.option('-state', nargs=1, type=click.STRING, help='State of the job.')
@click.option('-count', nargs=1, type=click.INT, help='Quantity of results.', default=10)
def jobs(tag, lacks, spider, state, count):
    """See information of N jobs"""
    params = parse_options(tag, lacks, spider, state)
    conn = Connection(apikey=API_KEY)
    try:
        jobs = get_jobs(params, conn, PROJECT, count)
        display_jobs(jobs, click)
    except requests.exceptions.ConnectionError:
        print_tokens(no_internet_connection_tokens, style=error_style)
        print()


register_repl(main)

Remove comments that annoy the CLI commands