# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Support functions for command-line utilities.

Exported functions:

  usage  -- write a usage message to stderr and return 2
  error  -- write an error message to stderr and return 1
  plural -- return 's' or '' depending on argument cardinality

"""

import sys

def usage(msg, *args):
    """Write usage message to stderr and return status 2 for sys.exit().

    Also print 'use --help for help'

    """
    error(msg, *args)
    sys.stderr.write("Use --help for help\n")
    return 2

def error(msg, *args):
    """Write error message to stderr and return status 1 for sys.exit()."""
    msg = str(msg)
    if args:
        try:
            msg = msg % args
        except:
            msg = "%s %s" % (msg, " ".join(map(str, args)))
    sys.stderr.write(msg + "\n")
    return 1

def plural(n):
    """Return '' if the argument equals 1, 's' otherwise."""
    if n == 1:
        return ""
    else:
        return "s"
