Skip to content

Lesson 5 of 6

Build a command-line question tool

Add command-line arguments for a file path and a question, combine them with your prompt, and print a clean answer.

7 minClaude, Gemini and ChatGPT

After this lesson, you will be able to turn your Gemini API script into a reusable command-line tool. You will accept a file path and a question as arguments, build a prompt that combines them, and print a clean answer.

Start from the long document script

In lesson 4, you wrote long_document.py, which reads a file called report.txt and asks Gemini a fixed question about it. Now you will make that script flexible. Instead of hard-coding the file name and the question, you will pass them on the command line.

Create a new file called ask_gemini.py and copy the code from long_document.py into it. The first part of the file stays the same:

import os
from google import genai

def read_file(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

api_key = os.environ["GEMINI_API_KEY"]
client = genai.Client(api_key=api_key)

# Replace with a current model from the provider's list of models.
MODEL_NAME = "gemini-2.0-flash"

The change comes after this. You will read the file path and the question from the command line, then build the prompt from those values.

Read command-line arguments

Python's standard library has a module called sys that gives you access to the arguments passed to your script. Add import sys at the top of the file. The list sys.argv contains the script name as its first item, followed by the arguments you type after the script name. For example, if you run python ask_gemini.py report.txt "What are the main challenges?", then sys.argv[1] is report.txt and sys.argv[2] is the question.

Add this code after the client setup:

if len(sys.argv) != 3:
    print("Usage: python ask_gemini.py <file_path> <question>")
    exit(1)

file_path = sys.argv[1]
question = sys.argv[2]

This checks that the user gave exactly two arguments. If not, it prints a short usage message and exits. Otherwise, it stores the file path and the question in variables.

Build the prompt from the arguments

Now you can read the file and build the prompt. Replace the lines that read report.txt and define the fixed prompt with this:

document_text = read_file(file_path)

prompt = f"""Read the document carefully. Answer this question: {question}

For each point in your answer, give a short quote from the document that supports it. Put the quote in quotation marks.

Document:
{document_text}"""

This prompt tells Gemini to answer the user's question and to support each point with a quote from the document. The question is inserted into the prompt using an f-string. The document text is included after the instructions, exactly as in lesson 4.

Print a clean answer

The final part of the script sends the request and prints the response. Keep it the same as before:

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=prompt,
)
print(response.text)

The complete ask_gemini.py file now looks like this:

import os
import sys
from google import genai

def read_file(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

api_key = os.environ["GEMINI_API_KEY"]
client = genai.Client(api_key=api_key)

# Replace with a current model from the provider's list of models.
MODEL_NAME = "gemini-2.0-flash"

if len(sys.argv) != 3:
    print("Usage: python ask_gemini.py <file_path> <question>")
    exit(1)

file_path = sys.argv[1]
question = sys.argv[2]

document_text = read_file(file_path)

prompt = f"""Read the document carefully. Answer this question: {question}

For each point in your answer, give a short quote from the document that supports it. Put the quote in quotation marks.

Document:
{document_text}"""

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=prompt,
)
print(response.text)

Run the tool with your own document and a question. For example, if you have a file called report.txt from lesson 4, you can run:

python ask_gemini.py report.txt "What are the main recommendations?"

The script reads the file, sends the prompt to Gemini, and prints the answer. The answer should include the recommendations and a quote for each one.

Handle a missing file

If the file path does not exist, read_file will raise an error and the script will stop with a traceback. A traceback is useful for a programmer, but a user of your tool would prefer a clear message. Add a check before reading the file:

import os

if not os.path.exists(file_path):
    print(f"File not found: {file_path}")
    exit(1)

Place this after the argument check and before read_file. Now the script prints a simple error and exits if the file is missing. The complete file with this check is:

import os
import sys
from google import genai

def read_file(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

api_key = os.environ["GEMINI_API_KEY"]
client = genai.Client(api_key=api_key)

# Replace with a current model from the provider's list of models.
MODEL_NAME = "gemini-2.0-flash"

if len(sys.argv) != 3:
    print("Usage: python ask_gemini.py <file_path> <question>")
    exit(1)

file_path = sys.argv[1]
question = sys.argv[2]

if not os.path.exists(file_path):
    print(f"File not found: {file_path}")
    exit(1)

document_text = read_file(file_path)

prompt = f"""Read the document carefully. Answer this question: {question}

For each point in your answer, give a short quote from the document that supports it. Put the quote in quotation marks.

Document:
{document_text}"""

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=prompt,
)
print(response.text)

You now have a command-line tool that answers any question about any text file. In the next lesson, you will make it more robust by adding a retry for failed API calls and testing it end-to-end.

Key takeaways

  • Use sys.argv to read command-line arguments in a Python script.
  • Check the number of arguments and the existence of the file, and exit with a clear message if something is wrong.
  • Build the prompt with an f-string that includes the user's question and the document text.
  • Keep the API call and response printing unchanged from the earlier script.

Try it

Write a new script called ask_any_file.py based on ask_gemini.py. Change the prompt so that it asks Gemini to answer the question and then list the three most important dates in the document, with a one-sentence description of each and a short quote for each date. Run the script on a long document of your own, at least 1500 words, with the question "What are the main challenges?" A good result includes an answer to the question, exactly three dates with descriptions and quotes, and the quotes match the document. If the result is not good, adjust the prompt and run it again.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.