Introduction to CLI Formatting
Code formatting is an essential aspect of software development that ensures consistency, readability, and maintainability across projects. While web-based formatters offer convenience, command-line interface (CLI) formatters provide more power, flexibility, and integration capabilities.
Our interactive CLI formatter terminal supports multiple programming languages and offers a seamless experience for developers who prefer working in terminal environments. This comprehensive guide will walk you through everything you need to know about using our CLI formatter effectively.
Why Use a CLI Formatter?
CLI formatters offer several advantages over their web-based counterparts:
- Automation: Easily integrate with build processes, CI/CD pipelines, and git hooks
- Batch processing: Format multiple files or entire directories with a single command
- Privacy: Your code never leaves your machine, ensuring complete privacy
- Performance: Process large codebases more efficiently without browser limitations
- Customization: Fine-tune formatting rules through configuration files
- Offline access: Format your code without an internet connection
Supported Languages
Our CLI formatter supports a wide range of programming languages, including:
Getting Started
Installation
Our CLI formatter can be installed using npm, the Node.js package manager:
npm install -g web-formatter-cli
For users who prefer other package managers:
# Using Yarn
yarn global add web-formatter-cli
# Using pnpm
pnpm add -g web-formatter-cli
Verify the installation by running:
wformat --version
Basic Usage
To format a single file, simply run:
wformat path/to/file.js
The formatter automatically detects the language based on the file extension. To format multiple files:
wformat path/to/file1.js path/to/file2.py path/to/file3.rb
To format all files in a directory:
wformat --dir path/to/directory
By default, the formatter will modify files in place. To preview changes without modifying files:
wformat --check path/to/file.js
Command Options
The CLI formatter supports various options to customize its behavior:
Options:
--version, -v Show version number
--help, -h Show help
--lang, -l Specify language explicitly (js, py, rb, etc.)
--dir, -d Format all supported files in directory
--recursive, -r Format directories recursively
--config, -c Path to config file
--check Check if files are formatted without modifying them
--write Write formatted output to files (default)
--ignore-path Path to ignore file (like .gitignore)
--stdin Read input from stdin
--stdout Print formatted output to stdout
--fix-errors Try to fix syntax errors before formatting
--verbose Show detailed output
Language-Specific Features
JavaScript/TypeScript
For JavaScript and TypeScript files, the formatter uses Prettier with sensible defaults:
wformat --lang js path/to/file.js
wformat --lang ts path/to/file.ts
You can customize the formatting rules by creating a{" "}
.wformatrc.js
or .wformatrc.json
file:
{
"javascript": {
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 100
}
}
Python
Python formatting is handled by Black with isort for import sorting:
wformat --lang py path/to/file.py
Python-specific configuration:
{
"python": {
"lineLength": 88,
"skipStringNormalization": false,
"sortImports": true
}
}
Ruby
Ruby files are formatted using RuboCop:
wformat --lang rb path/to/file.rb
Ruby-specific configuration:
{
"ruby": {
"indent": 2,
"maxLineLength": 100,
"singleQuotes": true
}
}
Java
Java formatting is handled by Google Java Format:
wformat --lang java path/to/file.java
Java-specific configuration:
{
"java": {
"style": "google",
"indent": 2
}
}
Go
Go files are formatted using gofmt:
wformat --lang go path/to/file.go
Go has minimal configuration options since gofmt is opinionated:
{
"go": {
"simplify": true,
"rewriteImports": true
}
}
Advanced Usage
Configuration Files
The CLI formatter supports several configuration file formats:
-
.wformatrc.json
-
.wformatrc.js
-
.wformatrc.yaml
-
wformat.config.js
Configuration files can be placed in the project root or specified
with the --config
option:
wformat --config path/to/config.json path/to/file.js
A comprehensive configuration file might look like:
{
"ignoreFiles": ["node_modules/**", "dist/**", "build/**"],
"javascript": {
"semi": true,
"singleQuote": true,
"tabWidth": 2
},
"python": {
"lineLength": 88,
"sortImports": true
},
"ruby": {
"indent": 2,
"maxLineLength": 100
},
"html": {
"wrapLineLength": 100,
"indentSize": 2
}
}
CI/CD Integration
Integrate the formatter into your CI/CD pipeline to ensure all code meets your formatting standards:
# GitHub Actions example
name: Code Formatting
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install formatter
run: npm install -g web-formatter-cli
- name: Check formatting
run: wformat --check --recursive .
Git Hooks
Set up a pre-commit hook to automatically format code before committing:
#!/bin/sh
# .git/hooks/pre-commit
files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\\.(js|jsx|ts|tsx|py|rb|java|go|php|html|css)$')
if [ -n "$files" ]; then
wformat $files
git add $files
fi
For easier setup, you can use husky with lint-staged:
// package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": ["wformat --write"],
"*.py": ["wformat --write"],
"*.rb": ["wformat --write"],
"*.{html,css}": ["wformat --write"]
}
}
Editor Integration
Visual Studio Code
Install our VS Code extension for seamless integration:
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Search for "Web Formatter CLI"
- Click Install
Configure the extension in your settings.json:
{
"editor.formatOnSave": true,
"webFormatter.enableFormatOnSave": true,
"webFormatter.respectEditorIndentation": true,
"webFormatter.configPath": ".wformatrc.json"
}
IntelliJ IDEA
For IntelliJ IDEA and other JetBrains IDEs:
- Go to Settings/Preferences
- Navigate to Tools {">"} External Tools
- Click the + button to add a new tool
-
Configure as follows:
- Name: Web Formatter
- Program: wformat
- Arguments: --write $FilePath$
- Working directory: $ProjectFileDir$
To run on save, you can use File Watchers plugin.
Vim/Neovim
For Vim or Neovim, you can use the formatter with plugins like ALE or null-ls:
" For ALE
let g:ale_fixers = {
\\ '*': ['remove_trailing_lines', 'trim_whitespace'],
\\ 'javascript': ['wformat'],
\\ 'typescript': ['wformat'],
\\ 'python': ['wformat'],
\\ 'ruby': ['wformat'],
\\}
let g:ale_fix_on_save = 1
" For null-ls in Neovim
require('null-ls').setup({
sources = {
require('null-ls').builtins.formatting.wformat,
},
})
Troubleshooting Common Issues
Here are solutions to common issues you might encounter:
Issue | Solution |
---|---|
Command not found | Ensure the package is installed globally and the global npm bin directory is in your PATH |
Formatting errors |
Check if the file has syntax errors; use{" "}
--fix-errors flag to attempt automatic
fixes
|
Configuration not applied |
Verify the config file path and format; use{" "}
--verbose to see which config is being used
|
Slow performance |
Use --ignore-path to skip unnecessary
files; format only changed files in large projects
|
Language not detected |
Explicitly specify the language with --lang {" "}
option
|
Best Practices
Follow these best practices to get the most out of the CLI formatter:
- Version control your configuration to ensure consistency across team members
- Integrate with CI/CD to enforce formatting standards automatically
- Use git hooks to format code before committing
- Format incrementally on large codebases to avoid massive diffs
- Combine with linters for comprehensive code quality checks
- Document your formatting standards in your project README
- Configure editor integration for a seamless development experience
Conclusion
Our interactive CLI formatter terminal provides a powerful, flexible solution for maintaining consistent code formatting across multiple programming languages. By integrating it into your development workflow, you can ensure code quality, improve readability, and focus on what matters most—writing great software.
Whether you're a solo developer or part of a large team, the CLI formatter offers the tools you need to maintain clean, consistent code with minimal effort. Start using it today to streamline your development process and elevate your codebase.
For more information, feature requests, or to report issues, visit our{" "} GitHub repository .