Skip to content

onamfc/case-shift

Repository files navigation

CaseShift

License: MIT Python 3.8+

A fast CLI tool for converting variable names between naming conventions.

Description

caseshift converts variable names between multiple naming conventions with intelligent detection and colorized output. Perfect for refactoring code, working across languages, or standardizing naming conventions in your projects.

Features

  • Convert between 9 popular naming conventions
  • Auto-detect source case style
  • Batch processing for multiple variables
  • Pipeline-friendly stdin/stdout interface
  • Intelligent acronym handling (HTTPSConnection → https_connection)
  • Colorized output for better readability
  • Interactive mode with user-friendly prompts
  • Zero configuration required
  • Programmatic API for Python projects

Supported Case Styles

Style Example Description
camel myVariableName Lowercase first word, capitalize rest
pascal MyVariableName Capitalize all words
snake my_variable_name Lowercase with underscores
kebab my-variable-name Lowercase with hyphens
screaming MY_VARIABLE_NAME Uppercase with underscores
dot my.variable.name Lowercase with dots
title My_Variable_Name Capitalize with underscores
train My-Variable-Name Capitalize with hyphens
upper-kebab MY-VARIABLE-NAME Uppercase with hyphens

Installation

From GitHub (Recommended)

# Clone the repository
git clone https://github.com/onamfc/caseshift.git
cd caseshift

# Install in development mode
pip install -e .

# Or install with development dependencies
pip install -e ".[dev]"

Direct Installation

pip install git+https://github.com/onamfc/caseshift.git

For Development

# Clone and set up development environment
git clone https://github.com/onamfc/caseshift.git
cd caseshift
pip install -r requirements-dev.txt
pip install -e .

Quick Start

Command Line Usage

Basic conversion:

caseshift myVariable --to snake
# Output: myVariable -> my_variable

Interactive mode:

caseshift --interactive
# Launches interactive prompts for easy conversion

Detect case style:

caseshift HTTPSConnection --to snake --detect
# Output: Detected: pascal
#         HTTPSConnection -> https_connection

Batch processing:

cat variables.txt | caseshift --to kebab --batch
# Converts multiple variables from stdin

Quiet mode (for scripting):

echo "myVariable" | caseshift --to snake --quiet
# Output: my_variable (no colors or arrows)

Python API Usage

from caseshift import convert_case, detect_case, to_snake_case

# Direct conversion
result = convert_case("myVariable", "snake")
print(result)  # my_variable

# Detect case style
case_style = detect_case("HTTPSConnection")
print(case_style)  # pascal

# Use specific converter
snake = to_snake_case("myVariable")
print(snake)  # my_variable

Usage Examples

Single Variable Conversion

# Convert to different styles
caseshift myVariable --to snake      # my_variable
caseshift my_variable --to camel     # myVariable
caseshift my-variable --to pascal    # MyVariable
caseshift MyVariable --to kebab      # my-variable
caseshift http_request --to screaming # HTTP_REQUEST

Batch Processing

# Process multiple variables
cat << EOF | caseshift --to snake --batch
myFirstVariable
mySecondVariable
myThirdVariable
EOF

# Output:
# myFirstVariable -> my_first_variable
# mySecondVariable -> my_second_variable
# myThirdVariable -> my_third_variable

Pipeline Usage

# Extract and convert variable names
grep -oP '\b[a-z][a-zA-Z0-9]*\b' file.js | caseshift --to snake --batch --quiet

# Convert clipboard content (macOS)
pbpaste | caseshift --to kebab

# Convert and save to file
echo "myVariable" | caseshift --to snake --quiet > output.txt

Interactive Mode

caseshift --interactive

This launches an interactive session where you can:

  1. Enter variable names
  2. See auto-detected case style
  3. Choose target case from a menu
  4. See colorized conversion results

Programmatic Usage

from caseshift import (
    to_camel_case,
    to_pascal_case,
    to_snake_case,
    to_kebab_case,
    to_screaming_snake_case,
    to_dot_case,
    to_title_case,
    to_train_case,
    to_upper_kebab_case,
    convert_case,
    detect_case,
)

# Use specific converters
print(to_snake_case("myVariable"))        # my_variable
print(to_camel_case("my_variable"))       # myVariable
print(to_pascal_case("my-variable"))      # MyVariable
print(to_kebab_case("MyVariable"))        # my-variable
print(to_screaming_snake_case("myVar"))   # MY_VAR
print(to_dot_case("myVariable"))          # my.variable
print(to_title_case("myVariable"))        # My_Variable
print(to_train_case("myVariable"))        # My-Variable
print(to_upper_kebab_case("myVariable"))  # MY-VARIABLE

# Generic converter with detection
text = "HTTPSConnection"
detected = detect_case(text)
print(f"Detected: {detected}")  # pascal

converted = convert_case(text, "snake")
print(converted)  # https_connection

Command Line Options

Usage: caseshift [OPTIONS] [TEXT]

  Convert variable names between naming conventions.

Options:
  -t, --to [camel|pascal|snake|kebab|screaming|dot|title|train|upper-kebab]
                                  Target case style
  -b, --batch                     Process multiple lines from stdin
  -q, --quiet                     Output only converted text
  -d, --detect                    Show detected case style
  -i, --interactive               Interactive mode with prompts
  --version                       Show the version and exit
  --help                          Show this message and exit

Advanced Examples

Refactoring Code Variables

# Extract all camelCase variables and convert to snake_case
grep -oP '\b[a-z][a-zA-Z0-9]*\b' src/app.js | \
  sort -u | \
  caseshift --to snake --batch

Convert Between Languages

# Convert Python variables to JavaScript style
grep -oP '(?<=self\.)[a-z_]+' python_class.py | \
  caseshift --to camel --batch --quiet

Integration with Git Hooks

# In a pre-commit hook, check for consistent naming
git diff --cached --name-only | \
  xargs grep -h "const\s\+\K[a-zA-Z_][a-zA-Z0-9_]*" | \
  caseshift --to camel --detect

Testing

Run the test suite:

# Install dev dependencies first
pip install -r requirements-dev.txt

# Run all tests
pytest

# Run with coverage
pytest --cov=var_case --cov-report=html

# Run specific test file
pytest tests/test_converter.py

Development

Setup Development Environment

git clone https://github.com/onamfc/caseshift.git
cd caseshift
pip install -r requirements-dev.txt
pip install -e .

Code Quality

# Format code
black src/ tests/

# Type checking
mypy src/

# Linting
flake8 src/ tests/

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Troubleshooting

Command not found after installation

Make sure your Python scripts directory is in your PATH:

# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.local/bin:$PATH"

Import errors when using as library

Ensure the package is installed:

pip install -e .

Tests failing

Install dev dependencies:

pip install -r requirements-dev.txt

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See CHANGELOG.md for version history and release notes.

Acknowledgments

  • Built with Click for the CLI interface
  • Inspired by the need for consistent naming across different programming languages

Made for developers who value clean, consistent code

About

A fast CLI tool for converting variable names between naming conventions.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published