cd "$(dirname "$(readlink -f "$0")")"
+PROG=${0##*/}
+
+usage() {
+ cat <<EOF
+$PROG — print a -fplugin-library= flag (or cabal stanza) that lets a
+downstream package load the classgraph GHC plugin WITHOUT adding classgraph
+to its build-depends, so the plugin's dependency bounds don't leak into the
+consumer's build plan.
+
+Usage:
+ $PROG [--dir DIR] [--cabal [--package PKG]]
+ $PROG -h | --help
+
+Options:
+ --dir DIR Directory the plugin writes its per-module JSON dumps to
+ (emitted as the plugin option dir=DIR). Relative to the
+ consumer package's build cwd. Default: .classgraph
+ --cabal Emit a ready-to-append cabal.project(.local) stanza instead
+ of the bare flag. Without --package this is a global
+ 'program-options' block (applies to every package); with
+ --package it is a 'package PKG' block.
+ --package PKG Scope the --cabal stanza to the single package PKG. Only
+ meaningful together with --cabal.
+ -h, --help Show this help and exit.
+
+Environment:
+ CLASSGRAPH_SO Absolute path to the plugin .so to use, bypassing discovery
+ entirely (and the chooser prompt below).
+
+Output goes to stdout; any prompts/diagnostics go to stderr, so the result is
+safe to redirect (e.g. '$PROG --cabal >> cabal.project.local').
+
+The plugin .so is discovered from ./dist-newstyle and the cabal store. If
+more than one candidate is found you are prompted to pick one. Re-run this
+script after rebuilding classgraph itself — the package id baked into the .so
+path changes on each build.
+
+Examples:
+ $PROG # bare flag, dumps to ./.classgraph
+ $PROG --dir build/classgraph # bare flag, custom output dir
+ $PROG --cabal >> cabal.project.local # global stanza, appended
+ $PROG --cabal --package ouroboros-consensus
+EOF
+}
+
DIR=.classgraph
EMIT=raw # raw | cabal
PACKAGE=
--dir) DIR=$2; shift 2 ;;
--cabal) EMIT=cabal; shift ;;
--package) PACKAGE=$2; shift 2 ;;
- -h|--help)
- sed -n '/^#$/q;/^# /{s/^# \?//;p;}' "$0"
- exit 0 ;;
- *) echo "unknown argument: $1" >&2; exit 2 ;;
+ -h|--help) usage; exit 0 ;;
+ *)
+ echo "$PROG: unknown argument: $1" >&2
+ echo "Try '$PROG --help'." >&2
+ exit 2 ;;
esac
done