Gemini CLI 使用示例
Gemini CLI 使用示例
Section titled “Gemini CLI 使用示例”不确定如何开始使用 Gemini CLI?本文档涵盖了如何使用 Gemini CLI 完成各种任务的示例。
注意: 结果是旨在展示潜在用例的示例。您的结果可能会有所不同。
根据内容重命名照片
Section titled “根据内容重命名照片”场景:你有一个包含以下文件的文件夹:
photos/photo1.pngphotos/photo2.pngphotos/photo3.png向 Gemini 提供以下提示:
Rename the photos in my "photos" directory based on their contents.结果:Gemini 会请求权限以重命名你的文件。
选择 允许一次,你的文件将被重命名:
photos/yellow_flowers.pngphotos/antique_dresser.pngphotos/green_android_robot.png通过阅读代码解释仓库
Section titled “通过阅读代码解释仓库”场景:你想通过检查代码,而不仅仅是 README,来了解一个流行的开源工具是如何工作的。
向 Gemini CLI 提供以下提示:
Clone the 'chalk' repository from https://github.com/chalk/chalk, read its key source files, and explain how it works.结果:Gemini 将执行一系列动作来回答你的请求。
- 首先,它会请求权限运行
git clone以下载仓库。 - 接下来,它会找到重要的源文件并请求读取它们的权限。
- 最后,在分析代码之后,它会提供一个摘要。
Gemini CLI 将根据实际的源代码返回一个解释:
The `chalk` library is a popular npm package for styling terminal output withcolors. 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.将两个电子表格合并为一个
Section titled “将两个电子表格合并为一个”场景:你有两个 .csv 文件:Revenue - 2023.csv 和 Revenue - 2024.csv。每个文件都包含如下所示的月度收入数据:
January,0February,0March,0April,900May,1000June,1000July,1200August,1800September,2000October,2400November,3400December,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,2024January,0,1000February,0,1200March,0,2400April,900,500May,1000,800June,1000,900July,1200,1000August,1800,400September,2000,2000October,2400,3400November,3400,1800December,2100,9000运行单元测试
Section titled “运行单元测试”场景:你已经编写了一个简单的登录页面。你希望编写单元测试以确保登录页面有代码覆盖率。
向 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 loginjest.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(); }); });});