#!/usr/bin/env python3
# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Helper to avoid I/O redirection shell syntax.

Usage: %prog FILE MODULE [ARG ...]

MODULE should be an importable Python module, with a main() function
that takes an args parameter.

This executes MODULE with its standard output redirected (appended)
to FILE.  Standard error is unaffected.

"""

import os
import sys
from optparse import OptionParser

parser = OptionParser(usage=__doc__.strip())
parser.disable_interspersed_args()

from sigma.util.errors import usage, error

def main(args=None):
    opts, args = parser.parse_args(args)
    if len(args) < 2:
        return usage("please pass at least FILE and MODULE arguments")
    filename = args[0]
    modname = args[1]
    args = args[2:]
    sys.stdout = open(filename, "a")
    module = __import__(modname)
    return module.main(args)

if __name__ == "__main__":
    sys.exit(main())
