#!/usr/bin/python3

import json
import sys
import subprocess

from colorama import init as colorama_init, Fore

def get_totp_codes():
    totp_json = subprocess.check_output(["pass", "show", "misc/totp-codes"])
    totp_codes = json.loads(totp_json)
    return { entry["name"]: entry for entry in totp_codes }

def get_totp(secret):
    raw_output = subprocess.check_output(["oathtool", "--base32", "--totp", secret])
    return raw_output.decode("utf-8").strip()

def find_most_similar(totp_data, search_name):
    case_insensitive = search_name.islower()

    for name, entry in totp_data.items():
        if case_insensitive:
            name = name.lower()

        if search_name in name:
            return entry

    return None

if __name__ == "__main__":
    exit_code = 0
    colorama_init()
    totp_data = get_totp_codes()

    # No arguments, list all app names
    if len(sys.argv) < 2:
        print("List of all TOTP applications:")
        if not totp_data:
            print("* (no entries found)")
        for name in totp_data:
            totp = get_totp(totp_data[name]["secret"])
            print(f"* {Fore.MAGENTA}{name}{Fore.RESET}: {totp}")

        sys.exit(0)

    # Print TOTP codes for each app listed
    for app_name in sys.argv[1:]:
        entry = find_most_similar(totp_data, app_name)
        if entry is None:
            print(f"No match for '{app_name}'", file=sys.stderr)
            exit_code = 1
            continue

        name = entry["name"]
        totp = get_totp(entry["secret"])
        print(f"{Fore.MAGENTA}{name}{Fore.RESET}: {totp}")

    sys.exit(exit_code)

@@ -25,16 +25,26 @@
 rt json%0A
+import re%0A
 import s
@@ -474,35 +474,31 @@
 %0A%0Adef find_m
-ost_similar
+atching
 (totp_data, 
@@ -501,202 +501,114 @@
 ta, 
-search_name):%0A    case_insensitive = search_name.islower()%0A%0A    for name, entry in totp_data.items():%0A        if case_insensitive:%0A            name = name.lower()%0A%0A        if search_name in 
+app_regex):%0A    matching = %5B%5D%0A%0A    for name, entry in totp_data.items():%0A        if app_regex.search(
 name
+)
 :%0A  
@@ -621,20 +621,30 @@
     
-return 
+matching.append(
 entry
+)
 %0A%0A  
@@ -652,20 +652,24 @@
  return 
-None
+matching
 %0A%0Aif __n
@@ -1170,20 +1170,23 @@
 for app_
-name
+pattern
  in sys.
@@ -1195,16 +1195,75 @@
 gv%5B1:%5D:%0A
+        app_regex = re.compile(app_pattern, re.IGNORECASE)%0A
         
@@ -1266,17 +1266,19 @@
     entr
-y
+ies
  = find_
@@ -1282,19 +1282,15 @@
 nd_m
-ost_similar
+atching
 (tot
@@ -1301,20 +1301,21 @@
 ta, app_
-name
+regex
 )%0A      
@@ -1323,21 +1323,19 @@
  if 
-entry is None
+not entries
 :%0A  
@@ -1360,16 +1360,18 @@
 No match
+es
  for '%7Ba
@@ -1373,20 +1373,23 @@
 r '%7Bapp_
-name
+pattern
 %7D'%22, fil
@@ -1450,16 +1450,50 @@
 ntinue%0A%0A
+        for entry in entries:%0A    
         
@@ -1513,16 +1513,20 @@
 %22name%22%5D%0A
+    
         
@@ -1550,32 +1550,36 @@
 ntry%5B%22secret%22%5D)%0A
+    
         print(f%22

Change get-totp to use regex, with multiple results.