Commit 796e088e authored by Vladimir Malenovsky's avatar Vladimir Malenovsky
Browse files

fix --filter to accept minus-prefixed tokens like -JBM

parent 3a19dd35
Loading
Loading
Loading
Loading
+73 −1
Original line number Diff line number Diff line
@@ -53,6 +53,64 @@ import time
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))



def _preprocess_filter_args():
    """Preprocess sys.argv to handle --filter with minus-prefixed tokens.
    
    Argparse stops consuming args when it encounters e.g. '-TERM', treating it as a flag.
    This function escapes filter tokens by wrapping minus-prefixed tokens with a marker,
    allowing argparse to treat them as regular arguments.
    
    Stores the escape mapping in _FILTER_ESCAPES for later unescaping.
    
    Returns: modified sys.argv with filter tokens escaped
    """
    global _FILTER_ESCAPES
    _FILTER_ESCAPES = {}
    marker_prefix = "@FILT_"
    result_argv = []
    i = 0
    
    while i < len(sys.argv):
        arg = sys.argv[i]
        
        # When we see --filter, process all following tokens until next option (--) or end
        if arg == "--filter":
            result_argv.append(arg)
            i += 1
            
            # Collect all filter tokens, escaping those that look like flags
            while i < len(sys.argv):
                tok = sys.argv[i]
                
                # Stop if we hit the next option (--something or -X where X is not escaped)
                if tok.startswith("--") or (tok.startswith("-") and len(tok) <= 2 and tok != "-"):
                    # Exception: stop only if it's a known argparse option
                    known_short_options = {"-h", "--help"}
                    if tok in known_short_options or tok.startswith("--"):
                        break
                    break
                
                # Escape tokens starting with - to protect them from argparse
                if tok.startswith("-"):
                    escaped = f"{marker_prefix}{len(_FILTER_ESCAPES)}"
                    _FILTER_ESCAPES[escaped] = tok
                    result_argv.append(escaped)
                else:
                    result_argv.append(tok)
                
                i += 1
        else:
            result_argv.append(arg)
            i += 1
    

    return result_argv


# Module-level dict to track filter token escapes
_FILTER_ESCAPES = {}

def readfile(
    filename: str, nchannels: int = 1, fs: int = 48000, outdtype="float"
) -> Tuple[np.ndarray, int]:
@@ -1787,7 +1845,21 @@ if __name__ == "__main__":
        action="store_true",
        help="Run analysis and unconditionally regenerate mld_ref2 files for all tags",
    )
    args = parser.parse_args()
    # Preprocess sys.argv to handle --filter with minus-prefixed tokens like -JBM
    modified_argv = _preprocess_filter_args()
    args = parser.parse_args(modified_argv[1:])  # Skip program name; parse_args expects args without it

    # Unescape filter tokens that were escaped during sys.argv preprocessing
    if args.filter:
        marker_prefix = "@FILT_"
        unescaped_filter = []
        for tok in args.filter:
            # Unescape any tokens that were wrapped by preprocessing
            if tok in _FILTER_ESCAPES:
                unescaped_filter.append(_FILTER_ESCAPES[tok])
            else:
                unescaped_filter.append(tok)
        args.filter = unescaped_filter

    if not os.path.isdir(args.testvecDir):
        parser.error(