dev-blog

Directory Tree CLI Tool

A small Python CLI utility to print a directory tree, with support for:


Features


Requirements

Install dependencies:

pip install pyperclip

Installation

Save the script as, for example:

tree.py

(Optional) Make it executable on Unix systems:

chmod +x tree.py

Usage

python tree.py [path] [options]

If no path is provided, the current directory (.) is used.


Options

Flag Description
-l, --level <n> Maximum directory depth
-c, --clipboard Copy output to clipboard instead of printing
-s, --show-ignore Print loaded ignore configuration and exit

Examples

python tree.py
python tree.py /path/to/project

Limit tree depth to 2 levels

python tree.py -l 2

Copy tree output to clipboard

python tree.py -c

Show ignore configuration

python tree.py --show-ignore

Output Example

project/
├── README.md
├── src/
│   ├── main.py
│   └── utils.py
└── tests/
    └── test_main.py

Configuration (config.json)

The tool looks for a config.json file in the current working directory.

If the file does not exist, defaults are used.

Default Configuration

{
  "ignore": {
    "folders": [],
    "files": []
  }
}

Example Configuration

{
  "ignore": {
    "folders": ["__pycache__", ".git", "node_modules"],
    "files": [".DS_Store", "*.log"]
  }
}

Note: Ignore matching is based on exact names only (no glob or wildcard support).


How Ignore Logic Works


Error Handling


Internals Overview


License

MIT (or specify your preferred license)


Notes


Happy hacking 🌲

```