AI-powered code snippet generator using ChatGPT. Generate code in various languages based on natural language descriptions!

SnipForge - AI-Powered Code Snippet Generator

GitHub

SnipForge is a powerful command-line interface (CLI) tool that utilizes OpenAI's GPT technology to generate code snippets for various programming and tooling languages based on a given description. It aims to save time and effort for developers by providing a convenient way to generate code on-demand.

Table of Contents

Features

  • Leverages OpenAI's GPT technology for intelligent code snippet generation
  • Supports multiple programming and tooling languages
  • Matches your specific needs with customizable options
  • Guides you through the code generation process in interactive mode step-by-step
  • Outputs code snippets to stdout or saves them to a file

Installation

Binary Installation

  1. Download the appropriate binary for your operating system from the latest release page.
  2. Extract the downloaded archive and place the snipforge binary in a directory that is included in your system's PATH.
  3. Open a terminal and type snipforge version to confirm the installation was successful.

Local Installation

If you prefer to run SnipForge from source, follow these steps:

  1. Clone the repository:
$ git clone https://github.com/peetya/snipforge-cli.git
  1. Change into the snipforge-cli directory:
$ cd snipforge-cli
  1. Install the necessary dependencies:
$ go mod download
  1. Run:
$ go run main.go version

Usage

To get started with SnipForge, install the CLI tool and run the generate command, providing the required flags and options. Alternatively, you can start the interactive mode by running the generate command without any flags, which will guide you through the code generation process step-by-step.

$ snipforge generate [flags]

For more information on available commands and flags, refer to the help output by running:

$ snipforge generate --help

Flags

Here's a detailed explanation of the available flags for the generate command:

  -d, --dry-run                      do not generate a code snippet, only print the generated description
  -g, --goal string                  the functionality description for the code snippet
  -h, --help                         help for generate
  -l, --language string              the programming or tooling language to generate code in (e.g. PHP, Golang, etc...)
  -v, --language-version string      the version of the programming or tooling language to generate code for (if applicable)
  -k, --openai-key string            the OpenAI API key
      --openai-max-tokens int        the maximum number of tokens to generate
  -m, --openai-model string          the OpenAI model to use
      --openai-temperature float32   the sampling temperature for the OpenAI model (between 0.0 and 2.0)
  -o, --output string                the output file path for the generated code snippet
  -q, --quiet                        suppress all output except for the generated code snippet
      --stdout                       print the generated code snippet to isStdout instead of saving to a file

Example

Basic example

Here's a basic example of how to use SnipForge to generate a Python code snippet for sorting a list of numbers:

$ snipforge generate --language python --language-version 3.11 --goal "sort a list of numbers" --output sorted_numbers.py

This command will generate a Python code snippet in the sorted_numbers.py file, with the goal of sorting a list of numbers.

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort()
print(numbers)

Advanced example

Here's an advanced example demonstrating how to use SnipForge to generate a PHP code snippet that fulfills more complex goals. In this example, we'll use SnipForge in interactive mode.

$ snipforge generate

First, we need to define a set of goals that will be used to generate the snippet.

What are your goals?

┃  1 A controller that returns a list of users via the "/api/v1/users" endpoint                     
┃  2 The output format can be changed via content negotiaton                                        
┃  3 Support pagination using the page and limit query parameters                                   
┃  4 Read the users from the injected UserRepositoryInterface                                       
┃  5 The controller must follow PSR-4 and PSR-12 standards                                          

Next, we need to define the programming language and version to generate the snippet for.

Which programming or tooling language do you want to use?

> Symfony 
Which version of PHP do you want to use? (optional)

> 6 

Then we need to define the output path:

Where do you want to save the snippet? 

> src/Controller/Api/V1/UserController.php 

Then it will generate the following code snippet for you in src/Controller/Api/V1/UserController.php:

<?php

// src/Controller/Api/V1/UserController.php

namespace App\Controller\Api\V1;

use App\Repository\UserRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/api/v1/users')]
class UserController extends AbstractController
{
    public function __construct(private UserRepositoryInterface $userRepository)
    {
    }

    #[Route('', name: 'api_v1_users_index', methods: ['GET'])]
    public function index(Request $request): Response
    {
        $page = $request->query->getInt('page', 1);
        $limit = $request->query->getInt('limit', 10);

        $users = $this->userRepository->getPaginatedUsers($page, $limit);

        $serializer = $this->container->get('serializer');
        $content = $serializer->serialize(['data' => $users], $request->getPreferredFormat(), ['groups' => ['user']]

        return new Response($content, 200, ['Content-Type' => $request->getMimeType($request->getPreferredFormat())]);
    }
}

Important Note on Generated Output

While SnipForge does its best to generate accurate and functional code snippets, it's crucial to review the generated output before using it in your projects. AI-generated code may sometimes contain errors or inconsistencies, so always double-check the results to ensure correctness.

Contributing

SnipForge is an open-source project, and we welcome contributions from developers like you! Feel free to submit issues, suggest new features, or create pull requests to help improve the project. We appreciate your support and collaboration. ❤️

License

SnipForge is released under the MIT License.

Owner
Péter Halász
Full Stack Developer
Péter Halász
Similar Resources

sttr is command line software that allows you to quickly run various transformation operations on the string.

sttr is command line software that allows you to quickly run various transformation operations on the string.

sttr is command line software that allows you to quickly run various transformation operations on the string.

Sep 21, 2021

go program that installs and customizes ohmyzsh tmux vim via various plugins and other nice to haves

go program that installs and customizes ohmyzsh tmux vim via various plugins and other nice to haves

Pimp-My-Shell Table of Contents Pimp-My-Shell Install Usage About Resources Tmux Hotkeys VIM Hotkeys Adjusting Custom Aliases Mac Fix Terminal bind ke

Dec 22, 2022

A golang CLI to display various stats about Hockey teams and their players

A golang CLI to display various stats about Hockey teams and their players

Oct 26, 2021

A very basic cli keyring tool to use accross various OS.

A very basic cli keyring tool to use accross various OS.

Dec 14, 2022

Various CLI tools in go

tools A collection of small command line utilities: hxd - a small hexdumper imgsize - show sizes of jpeg or png images kwed-dl - download latest track

Dec 6, 2022

Tnbassist - A CLI tool for thenewboston blockchain to perform various mundane tasks like taking daily accounts backup

TNB Assist is a CLI (Command Line Interface) tool for thenewboston blockchain to perform various mundane tasks like taking daily accounts backup, computing statistics, etc easier.

Feb 14, 2022

A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a proxy.

A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a proxy.

Fake-SMS A simple command line tool using which you can skip phone number based SMS verification by using a temporary phone number that acts like a pr

Dec 31, 2022

Code generator to produce CLI from R packages

cmd Create command line applications from R packages. How it works It's a code generator that outputs Go code which produces a Command Line Applicatio

Apr 30, 2022

Envopts - Provides a code generator for turning env structs into functional options

envopts Provides a code generator to turn structs annotated for the popular env

Jan 10, 2022
Comments
  • Possibility to get output in stdout

    Possibility to get output in stdout

    I wonder if it would be possible (if not already the case) to have the possibility to get output directly in stdout, so when you're in a coding session you dont have to leave your current work and open another file.

    In addition, you could use glamour to colorize such stdout snippets (if you ask AI to generate in ususal markdown format), it makes it even fancier :)

  • Provide install script

    Provide install script

    Since you already use goreleaser and provide already compiled binaries per releases, it would be nice to have a simple install script to make your tool adoption easier.

    You an take a look at this one I made for example: https://github.com/ekkinox/yai/blob/main/install.sh (auto detect os & arch and install latest), and users just have to curl https://.../install.sh | bash to get your tool up and running.

Snippet CLI manger for quickly using code snippets without leaving the terminal
Snippet CLI manger for quickly using code snippets without leaving the terminal

SnipKit - Snippet CLI manager This repository is still work in progress! SnipKit aims to paste code snippets from your favorite snippet manager into y

Dec 27, 2022
brewfile-desc add descriptions of formulae to Brewfile.

brewfile-desc brewfile-desc add descriptions of formulae to Brewfile. Usage $ cat path/to/Brewfile tap "golangci/tap" tap "k1low/tap" tap "mas-cli/tap

Dec 13, 2022
Simple command-line snippet manager, written in Go.
Simple command-line snippet manager, written in Go.

pet : CLI Snippet Manager Simple command-line snippet manager, written in Go You can use variables (<param> or <param=default_value> ) in snippets. Ab

Dec 29, 2022
QOwnNotes command-line snippet manager.
QOwnNotes command-line snippet manager.

QOwnNotes command-line snippet manager GitHub | Changelog | Releases You can use the QOwnNotes command-line snippet manager to execute command snippet

Oct 26, 2022
Powerful CLI written in GO to generate projects in various technologies
Powerful CLI written in GO to generate projects in various technologies

Barca CLI is a project generator written in GO and its purpose is to build and configure HTTP servers, web proxy, SPA/PWA, Blog and custom landing page. It's easy, fast and productive.

Aug 26, 2022
simple TOTP CLI, powered by keychain of macOS

macos-totp-cli macos-totp-cli is a simple TOTP CLI, powered by keychain of macOS. $ totp Usage: totp [command] Available Commands: completion ge

Dec 7, 2022
There is a certain amount of work to be done before you can implement the features of your Go powered CLI app

go-project-template-cli There is a certain amount of work to be done before you can implement the features of your Go powered CLI app. A few of those

Jan 23, 2022
Browser based Ascii-art generator with simple web design

Browser based Ascii-art generator with simple web design

Oct 31, 2022
A small CLI tool to check connection from a local machine to a remote target in various protocols.

CHK chk is a small CLI tool to check connection from a local machine to a remote target in various protocols.

Oct 10, 2022
cross-platform, cli app to perform various operations on string
cross-platform, cli app to perform various operations on string

sttr is command line software that allows you to quickly run various transformation operations on the string.

Dec 30, 2022