From 51518207aeeede4858a72da5cbbdde3e34256f88 Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 6 Apr 2026 19:01:32 -0400 Subject: [PATCH] -d database flag added. --- remember.py | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/remember.py b/remember.py index b6b57d8..dd6b24f 100755 --- a/remember.py +++ b/remember.py @@ -9,8 +9,8 @@ home = Path.home() DB_NAME = os.path.join(str(home), "CommandsToRemember.db") VALID_ACTIONS = {"add", "list", "search", "edit", "delete"} -def init_db(): - conn = sqlite3.connect(DB_NAME) +def init_db(db): + conn = sqlite3.connect(db) cur = conn.cursor() cur.execute(""" @@ -26,8 +26,8 @@ def init_db(): conn.close() -def add_command(command, purpose, keywords): - conn = sqlite3.connect(DB_NAME) +def add_command(db, command, purpose, keywords): + conn = sqlite3.connect(db) cur = conn.cursor() cur.execute( @@ -41,8 +41,8 @@ def add_command(command, purpose, keywords): print("✅ Command saved.") -def list_commands(): - conn = sqlite3.connect(DB_NAME) +def list_commands(db): + conn = sqlite3.connect(db) cur = conn.cursor() cur.execute("SELECT id, command, purpose, keywords FROM commands") @@ -61,8 +61,8 @@ def list_commands(): conn.close() -def search_commands(term): - conn = sqlite3.connect(DB_NAME) +def search_commands(db, term): + conn = sqlite3.connect(db) cur = conn.cursor() like_term = f"%{term}%" @@ -90,8 +90,8 @@ def search_commands(term): conn.close() -def delete_command(cmd_id): - conn = sqlite3.connect(DB_NAME) +def delete_command(db, cmd_id): + conn = sqlite3.connect(db) cur = conn.cursor() cur.execute("SELECT * FROM commands WHERE id = ?", (cmd_id,)) @@ -113,8 +113,8 @@ def delete_command(cmd_id): conn.close() -def edit_command(cmd_id, command, purpose, keywords): - conn = sqlite3.connect(DB_NAME) +def edit_command(db, cmd_id, command, purpose, keywords): + conn = sqlite3.connect(db) cur = conn.cursor() cur.execute("SELECT command, purpose, keywords FROM commands WHERE id = ?", (cmd_id,)) @@ -153,6 +153,7 @@ def parse_args(argv): raise ValueError(f"Invalid action: {action}") args = { + "database": DB_NAME, "action": action, "id": None, "command": None, @@ -177,7 +178,12 @@ def parse_args(argv): raise ValueError("Missing value -c") else: args["command"] = value - + elif arg == "-d": + if value == "": + args["database"] = DB_NAME + else: + args["database"] = value + elif arg == "-p": if value == "": raise ValueError("Missing value -p") @@ -231,27 +237,28 @@ def validate_args(args): return args def main(): - - init_db() - + try: parsed = parse_args(sys.argv) validated = validate_args(parsed) + db = validated['database'] + init_db(db) + if validated['action'] == "add": - add_command(validated['command'], validated['purpose'], validated['keywords']) + add_command(db, validated['command'], validated['purpose'], validated['keywords']) elif validated['action'] == "list": - list_commands() + list_commands(db) elif validated['action'] == "search": - search_commands(validated['term']) + search_commands(db, validated['term']) elif validated['action'] == "delete": - delete_command(validated['id']) + delete_command(db, validated['id']) elif validated['action'] == "edit": - edit_command(validated['id'], validated['command'], validated['purpose'], validated['keywords']) + edit_command(db, validated['id'], validated['command'], validated['purpose'], validated['keywords']) except ValueError as e: print(f"Error: {e}")