跳转到内容

Gemini CLI 使用示例

不确定如何开始使用 Gemini CLI?本文档涵盖了如何使用 Gemini CLI 完成各种任务的示例。

注意: 结果是旨在展示潜在用例的示例。您的结果可能会有所不同。

场景:你有一个包含以下文件的文件夹:

Terminal window
photos/photo1.png
photos/photo2.png
photos/photo3.png

向 Gemini 提供以下提示:

Rename the photos in my "photos" directory based on their contents.

结果:Gemini 会请求权限以重命名你的文件。

选择 允许一次,你的文件将被重命名:

Terminal window
photos/yellow_flowers.png
photos/antique_dresser.png
photos/green_android_robot.png

场景:你想通过检查代码,而不仅仅是 README,来了解一个流行的开源工具是如何工作的。

向 Gemini CLI 提供以下提示:

Clone the 'chalk' repository from https://github.com/chalk/chalk, read its key source files, and explain how it works.

结果:Gemini 将执行一系列动作来回答你的请求。

  1. 首先,它会请求权限运行 git clone 以下载仓库。
  2. 接下来,它会找到重要的源文件并请求读取它们的权限。
  3. 最后,在分析代码之后,它会提供一个摘要。

Gemini CLI 将根据实际的源代码返回一个解释:

The `chalk` library is a popular npm package for styling terminal output with
colors. After analyzing the source code, here's how it works:
- **Core functionality:** The main file sets up a chainable API. Each color or
modifier (like `bold` or `italic`) is a getter that appends the corresponding
ANSI escape code to an internal stack.
- **Chainable API:** When you call `chalk.red.bold('Hello')`, you are accessing
getters. The `red` getter adds the red color code, and the `bold` getter adds
the bold code.
- **Output generation:** When the chain is treated as a string (e.g., in
`console.log`), a final `toString()` method is called. This method joins all
the stored ANSI codes, wraps them around the input string ('Hello'), and adds
a reset code at the end. This produces the final, styled string that the
terminal can render.

场景:你有两个 .csv 文件:Revenue - 2023.csvRevenue - 2024.csv。每个文件都包含如下所示的月度收入数据:

January,0
February,0
March,0
April,900
May,1000
June,1000
July,1200
August,1800
September,2000
October,2400
November,3400
December,2100

你想将这两个 .csv 文件合并成一个 .csv 文件。

向 Gemini CLI 提供以下提示:

Combine the two .csv files into a single .csv file, with each year a different column.

结果:Gemini CLI 将读取每个文件,然后请求权限写入一个新文件。提供你的权限后,Gemini CLI 将提供以下 .csv:

Month,2023,2024
January,0,1000
February,0,1200
March,0,2400
April,900,500
May,1000,800
June,1000,900
July,1200,1000
August,1800,400
September,2000,2000
October,2400,3400
November,3400,1800
December,2100,9000

场景:你已经编写了一个简单的登录页面。你希望编写单元测试以确保登录页面有代码覆盖率。

向 Gemini CLI 提供以下提示:

Write unit tests for Login.js.

结果:Gemini CLI 将请求权限以写入新文件,并为您的登录页面创建一个测试。

import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import Login from './Login';
// Mock the API call for login
jest.mock('./api', () => ({
login: jest.fn(),
}));
describe('Login component', () => {
it('renders the login form correctly', () => {
const { getByLabelText, getByText } = render(<Login />);
expect(getByLabelText('Username')).toBeInTheDocument();
expect(getByLabelText('Password')).toBeInTheDocument();
expect(getByText('Login')).toBeInTheDocument();
});
it('shows an error message with invalid credentials', async () => {
const { getByLabelText, getByText, findByText } = render(<Login />);
const usernameInput = getByLabelText('Username');
const passwordInput = getByLabelText('Password');
const loginButton = getByText('Login');
// Mock the API to return an error
require('./api').login.mockRejectedValue(new Error('Invalid credentials'));
fireEvent.change(usernameInput, { target: { value: 'testuser' } });
fireEvent.change(passwordInput, { target: { value: 'wrongpassword' } });
fireEvent.click(loginButton);
const errorMessage = await findByText('Invalid credentials');
expect(errorMessage).toBeInTheDocument();
});
it('logs in successfully with valid credentials', async () => {
const onLoginSuccess = jest.fn();
const { getByLabelText, getByText } = render(
<Login onLoginSuccess={onLoginSuccess} />,
);
const usernameInput = getByLabelText('Username');
const passwordInput = getByLabelText('Password');
const loginButton = getByText('Login');
// Mock the API to return a success message
require('./api').login.mockResolvedValue({ success: true });
fireEvent.change(usernameInput, { target: { value: 'testuser' } });
fireEvent.change(passwordInput, { target: { value: 'correctpassword' } });
fireEvent.click(loginButton);
await waitFor(() => {
expect(onLoginSuccess).toHaveBeenCalled();
});
});
it('disables the submit button while submitting', async () => {
const { getByLabelText, getByText } = render(<Login />);
const usernameInput = getByLabelText('Username');
const passwordInput = getByLabelText('Password');
const loginButton = getByText('Login');
// Mock the API to have a delay
require('./api').login.mockImplementation(
() =>
new Promise((resolve) =>
setTimeout(() => resolve({ success: true }), 1000),
),
);
fireEvent.change(usernameInput, { target: { value: 'testuser' } });
fireEvent.change(passwordInput, { target: { value: 'correctpassword' } });
fireEvent.click(loginButton);
expect(loginButton).toBeDisabled();
await waitFor(() => {
expect(loginButton).not.toBeDisabled();
});
});
});