All ToolsยทAI Code Suggestion CLI
๐Ÿ’ป AI Coding AssistantsFebruary 28, 2026โœ… Tests passing

AI Code Suggestion CLI

A CLI tool that integrates with AI coding assistants like Claude Code to provide real-time code suggestions for a given function description or partial code snippet. This is useful for developers who want quick suggestions without switching to an IDE.

What It Does

  • Fetch code suggestions from an AI API based on a description and programming language.
  • Save the suggested code snippet to a file or display it in the terminal.

Installation

1. Clone the repository:

git clone <repository_url>

cd ai_code_suggestion_cli

2. Install the required dependencies:

pip install -r requirements.txt

Usage

1. Set the OPENAI_API_KEY environment variable with your OpenAI API key:

export OPENAI_API_KEY=your_api_key_here

2. Run the CLI tool:

python ai_code_suggestion_cli.py --description "function to add two numbers" --language python

3. Optionally, save the output to a file:

python ai_code_suggestion_cli.py --description "function to add two numbers" --language python --output suggestion.py

Source Code

import argparse
import requests
import os

def get_code_suggestion(api_key, description, language):
    """
    Fetches a code suggestion from an AI API based on the provided description and language.

    Args:
        api_key (str): The API key for the AI service.
        description (str): The function description or partial code snippet.
        language (str): The programming language for the suggestion.

    Returns:
        str: The suggested code snippet.

    Raises:
        ValueError: If the API key is missing.
        requests.RequestException: If there is an issue with the API request.
    """
    if not api_key:
        raise ValueError("API key is required to fetch code suggestions.")

    url = "https://api.openai.com/v1/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "model": "text-davinci-003",
        "prompt": f"Write a {language} function: {description}",
        "max_tokens": 150
    }

    try:
        response = requests.post(url, headers=headers, json=payload)
        response.raise_for_status()
        data = response.json()
        return data.get("choices", [{}])[0].get("text", "No suggestion available.").strip()
    except requests.RequestException as e:
        raise requests.RequestException(f"Failed to fetch code suggestion: {e}")

def main():
    parser = argparse.ArgumentParser(description="AI Code Suggestion CLI")
    parser.add_argument("--description", required=True, help="Function description or partial code snippet.")
    parser.add_argument("--language", required=True, help="Programming language (e.g., python, javascript, etc.).")
    parser.add_argument("--output", help="File to save the suggested code snippet.")
    args = parser.parse_args()

    api_key = os.getenv("OPENAI_API_KEY")
    if not api_key:
        print("Error: OPENAI_API_KEY environment variable is not set.")
        return

    try:
        suggestion = get_code_suggestion(api_key, args.description, args.language)
        if args.output:
            with open(args.output, "w") as file:
                file.write(suggestion)
            print(f"Code suggestion saved to {args.output}")
        else:
            print("Suggested Code:")
            print(suggestion)
    except ValueError as e:
        print(f"Error: {e}")
    except requests.RequestException as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()

Details

Tool Name
ai_code_suggestion_cli
Category
AI Coding Assistants
Generated
February 28, 2026
Tests
Passing โœ…
Fix Loops
2

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-02-28/ai_code_suggestion_cli
cd generated_tools/2026-02-28/ai_code_suggestion_cli
pip install -r requirements.txt 2>/dev/null || true
python ai_code_suggestion_cli.py