import argparse
import subprocess
import shlex

def main():
    sgpt_options_parser = argparse.ArgumentParser(add_help=False)
    sgpt_options_parser.add_argument('--model', type=str)
    sgpt_options_parser.add_argument('--temperature', type=float)
    sgpt_options_parser.add_argument('--top-probability', type=float)
    sgpt_options_parser.add_argument('--cache', action="store_true")
    sgpt_options_parser.add_argument('--no-cache', action="store_true")
    sgpt_options_parser.add_argument('--chat', type=str)
    sgpt_options_parser.add_argument('-s', '--shell', action='store_true')
    sgpt_options_parser.add_argument('-d', '--describe-shell', action='store_true')
    sgpt_options_parser.add_argument('--code', action='store_true')
    sgpt_options_parser.add_argument('--no-code', action='store_true')
    sgpt_options_parser.add_argument('--editor', action="store_true")
    sgpt_options_parser.add_argument('--no-editor', action="store_true")
    sgpt_options_parser.add_argument('--repl', type=str)
    sgpt_options_parser.add_argument('--show-chat', type=str)
    sgpt_options_parser.add_argument('--list-chat', action="store_true")
    sgpt_options_parser.add_argument('--no-list-chat', action="store_true")

    sgpt_options, remaining_args = sgpt_options_parser.parse_known_args()

    query_parser = argparse.ArgumentParser()
    query_parser.add_argument('query', nargs=argparse.REMAINDER, help='The prompt to generate completions for')

    query_args = query_parser.parse_args(remaining_args)

    command = "sgpt"

    for key, value in vars(sgpt_options).items():
        if value:
            if key == 'shell':
                command += f" -s"
            else:
                if isinstance(value, bool):
                    command += f" --{key.replace('_', '-')}"
                else:
                    command += f" --{key.replace('_', '-')} {value}"

    if query_args.query:
        prompt = ' '.join(query_args.query)
        command += f' "{prompt}"'

    subprocess.call(shlex.split(command))

if __name__ == "__main__":
    main()
