A simple command to easily get explanations using Gemini CLI

6 min

language: ja bn en es hi pt ru zh-cn zh-tw

Hello, I'm incompetent.
Long time no see. I was saving up energy. Just kidding, I was just slacking off. Sorry.

So, this is a minor tip, but the Gemini CLI has been released, which is great news for CLI users.

I just added this to my .bashrc.

ghelp() {
 (echo "Explain this" ; cat $1 ) | gemini
}

It's nothing special, but it eliminates the extra step of going through a browser to have an LLM explain code, which makes things easier.
For example, with this:
[curljs]

thepassenger:[haturatu]:~/git/curljs$ ghelp curl.js 
This code is a Node.js script that uses the [Puppeteer](https://pptr.dev/) library to retrieve the content of a specified web page.

Below, I will explain what each part of the code does.

1.  **`const puppeteer = require('puppeteer');`**
    *   Loads the Puppeteer library. This library is used to programmatically control Chrome or Chromium browsers.

2.  **`(async () => { ... })();`**
    *   This is an "Immediately Invoked Async Function". By wrapping the entire code in an async function, it allows the use of the `await` keyword internally. `await` is necessary to wait for time-consuming processes (like launching a browser or loading a page) to complete.

3.  **`const url = process.argv[2];`**
    *   Stores the third argument passed from the command line into the `url` variable.
    *   Example: If executed as `node script.js https://example.com`, `process.argv[2]` will contain `https://example.com`.

4.  **`if (!url) { ... }`**
    *   If no URL is specified, an error message is displayed, and the script is terminated abnormally with `process.exit(1)`.

5.  **`const browser = await puppeteer.launch({ headless: true });`**
    *   Launches a new browser instance.
    *   `headless: true` is a setting to run the browser in the background without displaying its UI (screen). If you want to visually confirm the browser's behavior, such as during debugging, set it to `false`.

6.  **`const page = await browser.newPage();`**
    *   Opens a new tab (page) in the launched browser.

7.  **`await page.goto(url, { waitUntil: 'networkidle2' });`**
    *   Navigates to the specified URL.
    *   `waitUntil: 'networkidle2'` is an important option for waiting for the page to load. It waits until there are no new network connections for 500 milliseconds after the number of network connections has dropped to 2 or less. This is useful for correctly displaying modern websites (like SPAs) that dynamically load content via JavaScript.

8.  **`await new Promise(resolve => setTimeout(resolve, 5000));`**
    *   After the page has finished loading, it waits for an additional 5 seconds.
    *   This additional waiting time is inserted to wait for animations that `networkidle2` alone might not fully load, or for content that appears after specific operations.

9.  **`const html = await page.content();`**
    *   Retrieves the complete HTML content of the page. This includes all HTML elements dynamically generated by JavaScript.

10. **`console.log(html);`**
    *   Outputs the retrieved HTML to the console.

11. **`await browser.close();`**
    *   Closes the browser instance and releases resources. Forgetting this can cause the process to remain running indefinitely.

### Summary

This script performs the process of **"retrieving the web page at the URL specified on the command line in its final state after JavaScript execution is complete, and displaying its HTML to standard output."**

This is a very useful method when you want to retrieve information from dynamically rendered websites (like SPAs) that cannot be obtained with a simple HTTP request (e.g., for web scraping).

It outputs something like this.
Well, that's all there is to it...

Related Posts