using System.CommandLine;
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
///
/// Factory methods for the global CLI options. Centralising option construction keeps
/// validation rules (e.g. the accepted --format values) in one place and makes
/// them testable without standing up the whole command tree.
///
internal static class CliOptions
{
///
/// Creates the global --format option. The option deliberately has no
/// DefaultValueFactory — format precedence (explicit flag → config/env →
/// "json") is resolved by , which
/// needs to distinguish an absent flag. The accepted values are constrained so a
/// typo (e.g. --format tabel) is rejected with a clear parse error rather
/// than silently falling through to JSON.
///
/// The configured --format option constrained to "json" or "table".
///
/// The global --password-file option: reads the LDAP password from a file or stdin
/// (ScadaBridge#137). Shared by every root so
/// can read it without threading another option through each command group.
///
internal static Option PasswordFileOption { get; } =
ValueSourceOptions.CreateFileOption("--password", "the LDAP password", recursive: true);
///
/// Adds to the root and rejects it alongside an inline
/// --password on every command. Call it after every command group is added.
///
/// The CLI root command.
/// The inline --password option already on the root.
internal static void AddPasswordFileOption(Command root, Option passwordOption)
{
root.Add(PasswordFileOption);
// System.CommandLine runs only the innermost command's validators, so the check goes on
// every command in the tree. Call this after all command groups are registered.
void AddTo(Command command)
{
command.Validators.Add(result =>
{
if (result.GetResult(passwordOption) is { Implicit: false } && result.GetResult(PasswordFileOption) is { Implicit: false })
result.AddError("Use either --password or --password-file, not both.");
});
foreach (var sub in command.Subcommands)
AddTo(sub);
}
AddTo(root);
}
internal static Option CreateFormatOption()
{
var formatOption = new Option("--format")
{
Description = "Output format (json or table)",
Recursive = true,
};
formatOption.AcceptOnlyFromAmong("json", "table");
return formatOption;
}
}