Introduction: Bringing It All Together with a CLI

Welcome back! So far, you have learned how to scan a codebase for files and extract git history using Python. In this lesson, you will see how to combine these tools into a single command-line interface (CLI) program. This CLI will let you analyze any codebase by simply running a command in your terminal.

Using a CLI is a common way to interact with developer tools. It allows you to run your analysis on any project folder, see results right away, and even automate tasks. By the end of this lesson, you will understand how to build a simple CLI that ties together your codebase scanner and git history extractor and displays useful statistics about a project.

Quick Recap: Scanning and Git Extraction

Before we dive in, let’s quickly remind ourselves of the two main components you built in previous lessons:

  • Repository Scanner: This tool walks through a project directory, finds code files, reads their contents, and detects their programming language.
  • Git History Extractor: This tool uses the gitpython library to read the commit history and file changes from a git repository.

You already have classes like RepositoryScanner and GitHistoryExtractor that handle these tasks. In this lesson, you will see how to use them together in a new way.

Building a Simple CLI with argparse

To make your tool easy to use from the terminal, you will use Python’s argparse library. This library helps you accept arguments from the command line, such as the path to the repository you want to analyze.

Let’s start by importing argparse and setting up a basic CLI:

Explanation:

  • argparse.ArgumentParser() creates a parser for command-line arguments.
  • add_argument('--repo', ...) lets the user specify a repository path with --repo. If not provided, it defaults to the current directory (.).
  • args = parser.parse_args() reads the arguments from the command line.
  • The script prints out the path it will analyze.

Example usage:

This command tells the program to analyze the sample-ecommerce-api folder.

Connecting the Pieces: Running the Analysis

Now, let’s see how to use your existing scanner and git extractor inside the CLI. You will create a new class called CodebaseAnalyzer that brings everything together.

First, import your scanner and git extractor:

Next, define the CodebaseAnalyzer class:

Explanation:

  • The CodebaseAnalyzer class creates instances of your scanner and git extractor.
  • The analyze_repository method:
    • Scans the repository for code files.
    • Extracts the git commit history.
    • Prints out the number of files, commits, file changes, and a breakdown of programming languages.

Now, update your main() function to use this analyzer:

Displaying Results: Making Sense of the Output

When you run your CLI tool, you will see output like this:

What does this mean?

  • Files: The number of code files found in the repository.
  • Commits: The number of git commits extracted (up to the limit set in your extractor).
  • File changes: The number of file changes detected in the commit history.
  • Languages: A dictionary showing how many files were found for each programming language.

This output gives you a quick overview of the project’s size, activity, and language breakdown. It’s a useful starting point for any code review or analysis.

Summary and Practice Preview

In this lesson, you learned how to build a simple command-line interface (CLI) that brings together your codebase scanner and git history extractor. You saw how to use argparse to accept user input, how to connect your existing components, and how to display useful statistics about a codebase.

Next, you will get a chance to practice running and modifying this CLI tool yourself. Try analyzing different repositories, experiment with the output, and see how the results change. This hands-on practice will help you become comfortable using and extending CLI tools for code analysis.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal