// Provenance: own design — shared run scripts (CLI)
using System.Globalization;
namespace Godosa.Core.Runs.Scripts;
/// Run-script CLI options (--script, --var k=v, --output, --no-cache, --no-snapshots, --from,
/// --until, --nondeterministic, --step-cap, --report-dir); determinism and seed come from
/// .
public sealed class ScriptRunFlags
{
public string? Script { get; set; }
public Dictionary Vars { get; } = new(StringComparer.Ordinal);
public OutputLevel? Output { get; set; }
public bool NoCache { get; set; }
public bool NoSnapshots { get; set; }
public string? From { get; set; }
public string? Until { get; set; }
public bool NonDeterministic { get; set; }
public int? StepCap { get; set; }
public string? ReportDir { get; set; }
public static bool TryTake(string[] args, ref int i, ScriptRunFlags flags)
{
string Value(ref int j)
{
if (j + 1 >= args.Length)
throw new ArgumentException($"{args[j]} needs a value");
return args[++j];
}
switch (args[i])
{
case "--script":
flags.Script = Value(ref i);
return true;
case "--var":
{
var v = Value(ref i);
var eq = v.IndexOf('=');
if (eq <= 0)
throw new ArgumentException($"--var needs name=value, got '{v}'");
flags.Vars[v[..eq]] = v[(eq + 1)..];
return true;
}
case "--output":
{
var v = Value(ref i);
try
{
flags.Output = RunOutput.Parse(v);
}
catch (ScriptFault e)
{
throw new ArgumentException(e.Message);
}
return true;
}
case "--no-cache":
flags.NoCache = true;
return true;
case "--no-snapshots":
flags.NoSnapshots = true;
return true;
case "--from":
flags.From = Value(ref i);
return true;
case "--until":
flags.Until = Value(ref i);
return true;
case "--nondeterministic":
flags.NonDeterministic = true;
return true;
case "--step-cap":
{
var v = Value(ref i);
flags.StepCap = int.TryParse(v, CultureInfo.InvariantCulture, out var n) && n > 0 ? n : throw new ArgumentException($"bad --step-cap '{v}'");
return true;
}
case "--report-dir":
flags.ReportDir = Value(ref i);
return true;
default:
return false;
}
}
public ScriptRunOptions ToOptions(RunFlags run, string? cacheDir) => new()
{
Deterministic = !NonDeterministic,
Seed = run.Seed,
Output = Output ?? OutputLevel.Labels,
Floor = Output ?? OutputLevel.Quiet,
StepCap = StepCap ?? 36_000,
CacheDir = cacheDir,
UseCache = !NoCache,
Snapshots = !NoSnapshots,
From = From,
Until = Until,
ReportDir = ReportDir,
};
}