Cloving Generate Code Command Documentation

Learn how to use the cloving generate code command to generate and manage code snippets.

Usage

cloving generate code [options]

There is also shortcut to save you a few chars:

cloving g code [options]

And if you really hate typing:

cloving g c [options]

Description

The cloving generate code command helps you generate code snippets based on a prompt and context files. It uses the AI APIs configured with cloving config as well as the cloving.json file created by cloving init to analyze the prompt and generate relevant code, which can then be reviewed, revised, or saved.

Options

This command accepts the following options:

  • --save: Automatically save all files after generating the code.
  • -i, --interactive: Auto-save generated code and then show a new prompt with existing context to revise the code further.
  • -s, --silent: Run the command without asking for confirmation of submitting prompts.
  • -m, --model <model>: Select the model to use (e.g., openai, claude, ollama, ollama:llama3, claude:claude-3-5:sonnet-20240620).
  • -p, --prompt <prompt>: Specify the prompt to use.
  • -f, --files <filenames...>: Specify filenames of files with context to use for generating code.

Interactive Prompts

The cloving generate code command uses the following interactive prompts if you don't set them up in the CLI options:

  • Context files: Prompt to include additional files or directories as context.
  • Code prompt: Prompt to provide a description of the code to be generated.
  • Revise: Prompt to revise the generated code and include additional context files.
  • Explain: Prompt to explain the generated code.
  • Save: Prompt to save the generated code to specific files.
  • Copy: Prompt to copy the generated code to the clipboard.

Example Process

Example Code Generation Session

Here is an example of what a code generation session might look like:


$ cloving generate code --prompt "Create a function to calculate the factorial of a number"
Generated code:

1. **src/factorial.ts**:

```typescript
function factorial(n: number): number {
  if (n <0) return -1
  else if (n === 0) return 1
  else return n * factorial(n - 1)
}
```

What would you like to do?
1. Revise
2. Explain
3. Save a Source Code File
4. Save All Source Code Files
5. Copy Source Code to Clipboard
6. Copy Entire Response to Clipboard
7. Done

      

If you choose to save the code, the process continues as follows:


$ cloving generate code --prompt "Create a function to calculate the factorial of a number" --save
Generated code:

1. **src/factorial.ts**:

```typescript
function factorial(n: number): number {
  if (n < 0) return -1
  else if (n === 0) return 1
  else return n * factorial(n - 1)
}
```

Saving generated files...

src/factorial.ts has been saved.