The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a Python CLI for managing subscriptions

Here’s a Python CLI (Command Line Interface) application to manage subscriptions. It uses argparse for the CLI interface and json for data storage in a local file called subscriptions.json.

python
import argparse import json import os from datetime import datetime DATA_FILE = "subscriptions.json" def load_data(): if not os.path.exists(DATA_FILE): return [] with open(DATA_FILE, "r") as file: return json.load(file) def save_data(data): with open(DATA_FILE, "w") as file: json.dump(data, file, indent=4) def add_subscription(name, price, renewal_date): data = load_data() data.append({ "name": name, "price": float(price), "renewal_date": renewal_date }) save_data(data) print(f"Added subscription: {name}") def list_subscriptions(): data = load_data() if not data: print("No subscriptions found.") return for i, sub in enumerate(data, start=1): print(f"{i}. {sub['name']} - ${sub['price']:.2f} - Renews on {sub['renewal_date']}") def remove_subscription(name): data = load_data() updated = [sub for sub in data if sub['name'].lower() != name.lower()] if len(data) == len(updated): print(f"No subscription found with the name: {name}") else: save_data(updated) print(f"Removed subscription: {name}") def upcoming_renewals(days): data = load_data() now = datetime.now() future = now.timestamp() + days * 86400 found = False for sub in data: try: renewal = datetime.strptime(sub['renewal_date'], "%Y-%m-%d").timestamp() if now.timestamp() <= renewal <= future: print(f"{sub['name']} - ${sub['price']:.2f} - Renews on {sub['renewal_date']}") found = True except ValueError: print(f"Invalid date format for {sub['name']}, skipping.") if not found: print(f"No subscriptions renewing in the next {days} days.") def main(): parser = argparse.ArgumentParser(description="Subscription Manager CLI") subparsers = parser.add_subparsers(dest="command") # Add subscription add_parser = subparsers.add_parser("add", help="Add a new subscription") add_parser.add_argument("name", help="Name of the subscription") add_parser.add_argument("price", help="Monthly cost of the subscription") add_parser.add_argument("renewal_date", help="Renewal date (YYYY-MM-DD)") # List subscriptions list_parser = subparsers.add_parser("list", help="List all subscriptions") # Remove subscription remove_parser = subparsers.add_parser("remove", help="Remove a subscription") remove_parser.add_argument("name", help="Name of the subscription to remove") # Upcoming renewals upcoming_parser = subparsers.add_parser("upcoming", help="List upcoming renewals") upcoming_parser.add_argument("days", type=int, help="Days ahead to check for renewals") args = parser.parse_args() if args.command == "add": add_subscription(args.name, args.price, args.renewal_date) elif args.command == "list": list_subscriptions() elif args.command == "remove": remove_subscription(args.name) elif args.command == "upcoming": upcoming_renewals(args.days) else: parser.print_help() if __name__ == "__main__": main()

Usage Examples

  • Add a subscription:
    python subscription_cli.py add Netflix 15.99 2025-06-10

  • List subscriptions:
    python subscription_cli.py list

  • Remove a subscription:
    python subscription_cli.py remove Netflix

  • Check upcoming renewals within 30 days:
    python subscription_cli.py upcoming 30

Would you like this script to support recurring billing calculations or notifications via email?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About