Skip to content

Latest commit

 

History

History
310 lines (235 loc) · 5.29 KB

File metadata and controls

310 lines (235 loc) · 5.29 KB

CLI Tool Template with sqlite-worker

A command-line interface template for managing SQLite databases using sqlite-worker.

Quick Start

# Make executable
chmod +x cli.py

# Show help
./cli.py --help

# List items
./cli.py list

# Add item
./cli.py add "My Item" --desc "Description"

Features

  • ✅ CRUD operations (Create, Read, Update, Delete)
  • ✅ Search functionality
  • ✅ JSON import/export
  • ✅ Database statistics
  • ✅ Thread-safe operations
  • ✅ Clean command-line interface

Available Commands

List Items

./cli.py list
./cli.py list --limit 10

Add Item

./cli.py add "Item Name"
./cli.py add "Item Name" --desc "Description"

Get Item

./cli.py get 1

Update Item

./cli.py update 1 --name "New Name"
./cli.py update 1 --desc "New Description"
./cli.py update 1 --name "New Name" --desc "New Description"

Delete Item

./cli.py delete 1

Search Items

./cli.py search "keyword"

Export to JSON

./cli.py export output.json

Import from JSON

./cli.py import data.json

Show Statistics

./cli.py stats

Custom Database

Use a different database file:

./cli.py --db custom.db list
./cli.py --db custom.db add "Item"

Example Usage

# Add some items
./cli.py add "Task 1" --desc "Complete documentation"
./cli.py add "Task 2" --desc "Write tests"
./cli.py add "Task 3" --desc "Deploy to production"

# List all items
./cli.py list

# Search for specific items
./cli.py search "test"

# Export to JSON
./cli.py export tasks.json

# Show statistics
./cli.py stats

# Update an item
./cli.py update 1 --desc "Documentation completed"

# Delete an item
./cli.py delete 2

Customization

Add New Commands

def your_command(self):
    """Your command implementation"""
    # Your logic here
    pass

# In main():
your_parser = subparsers.add_parser('your_command', help='Your help text')
your_parser.add_argument('arg', help='Argument help')

Add New Fields

# Update schema
self.worker.execute("""
    ALTER TABLE items ADD COLUMN your_field TEXT
""")

# Update add_item
def add_item(self, name, description=None, your_field=None):
    token = self.worker.insert("items", {
        "name": name,
        "description": description,
        "your_field": your_field
    })

Add Colors

# Install colorama
# pip install colorama

from colorama import Fore, Style

def list_items(self):
    print(f"{Fore.GREEN}✅ Items:{Style.RESET_ALL}")
    # ...

Use Cases

Task Manager

./cli.py add "Buy groceries" --desc "Milk, eggs, bread"
./cli.py add "Call dentist" --desc "Schedule appointment"
./cli.py list

Note Taking

./cli.py add "Meeting Notes" --desc "Discussed Q4 goals..."
./cli.py search "meeting"

Inventory Management

./cli.py add "Laptop" --desc "Dell XPS 15, SN: 12345"
./cli.py add "Monitor" --desc "27 inch 4K"

Contact Manager

# Customize for contacts
self.worker.execute("""
    CREATE TABLE contacts (
        id INTEGER PRIMARY KEY,
        name TEXT,
        email TEXT,
        phone TEXT
    )
""")

Installation as Package

Create setup.py:

from setuptools import setup

setup(
    name='your-cli-tool',
    version='1.0.0',
    py_modules=['cli'],
    install_requires=['sqlite-worker'],
    entry_points={
        'console_scripts': [
            'your-tool=cli:main',
        ],
    },
)

Install:

pip install -e .
your-tool list

Testing

import unittest
from cli import CLITool

class TestCLI(unittest.TestCase):
    def setUp(self):
        self.tool = CLITool(':memory:')
    
    def test_add_item(self):
        self.tool.add_item("Test", "Description")
        items = self.tool.worker.fetch_results(
            self.tool.worker.select("items")
        )
        self.assertEqual(len(items), 1)

Advanced Features

Add Confirmation Prompts

def delete_item(self, item_id):
    response = input(f"Delete item #{item_id}? (y/N): ")
    if response.lower() != 'y':
        print("Cancelled")
        return
    # ... delete logic

Add Progress Bars

from tqdm import tqdm

def import_json(self, filename):
    data = json.load(open(filename))
    for item in tqdm(data, desc="Importing"):
        self.worker.insert("items", item)

Add Configuration File

import configparser

config = configparser.ConfigParser()
config.read('config.ini')
db_path = config.get('database', 'path', fallback='data.db')

Troubleshooting

Permission Denied

chmod +x cli.py

Database Locked

  • Ensure no other process is using the database
  • WAL mode is enabled by default for better concurrency

Module Not Found

pip install sqlite-worker

Resources

Next Steps

  • Add authentication
  • Implement backup/restore
  • Add shell completion
  • Create man pages
  • Package as executable
  • Add logging
  • Implement undo/redo
  • Add bulk operations

Perfect starting point for building command-line tools with SQLite!