9#include <ascii-chat/options/completions/powershell.h>
10#include <ascii-chat/options/registry.h>
11#include <ascii-chat/common.h>
17static void ps_escape_help(FILE *output,
const char *text) {
22 for (
const char *p = text; *p; p++) {
25 fprintf(output,
"''");
26 }
else if (*p ==
'\n' || *p ==
'\t') {
35static void ps_write_option(FILE *output,
const option_descriptor_t *opt) {
45 if (meta->input_type == OPTION_INPUT_ENUM && meta->enum_values && meta->enum_values[0] != NULL) {
47 if (opt->short_name !=
'\0') {
48 fprintf(output,
" @{ Name = '-%c'; Description = '", opt->short_name);
49 ps_escape_help(output, opt->help_text);
50 fprintf(output,
"'; Values = @(");
51 for (
size_t i = 0; meta->enum_values[i] != NULL; i++) {
53 fprintf(output,
", ");
54 fprintf(output,
"'%s'", meta->enum_values[i]);
56 fprintf(output,
") }\n");
58 fprintf(output,
" @{ Name = '--%s'; Description = '", opt->long_name);
59 ps_escape_help(output, opt->help_text);
60 fprintf(output,
"'; Values = @(");
61 for (
size_t i = 0; meta->enum_values[i] != NULL; i++) {
63 fprintf(output,
", ");
64 fprintf(output,
"'%s'", meta->enum_values[i]);
66 fprintf(output,
") }\n");
68 }
else if (meta->examples && meta->examples[0] != NULL) {
70 if (opt->short_name !=
'\0') {
71 fprintf(output,
" @{ Name = '-%c'; Description = '", opt->short_name);
72 ps_escape_help(output, opt->help_text);
73 fprintf(output,
"'; Values = @(");
74 for (
size_t i = 0; meta->examples[i] != NULL; i++) {
76 fprintf(output,
", ");
77 fprintf(output,
"'%s'", meta->examples[i]);
79 fprintf(output,
") }\n");
81 fprintf(output,
" @{ Name = '--%s'; Description = '", opt->long_name);
82 ps_escape_help(output, opt->help_text);
83 fprintf(output,
"'; Values = @(");
84 for (
size_t i = 0; meta->examples[i] != NULL; i++) {
86 fprintf(output,
", ");
87 fprintf(output,
"'%s'", meta->examples[i]);
89 fprintf(output,
") }\n");
91 }
else if (meta->input_type == OPTION_INPUT_NUMERIC) {
93 if (opt->short_name !=
'\0') {
94 fprintf(output,
" @{ Name = '-%c'; Description = '", opt->short_name);
95 ps_escape_help(output, opt->help_text);
96 fprintf(output,
" (numeric %d-%d)'; Values = @(", meta->numeric_range.min, meta->numeric_range.max);
97 fprintf(output,
"'%d'", meta->numeric_range.min);
98 if (meta->numeric_range.max > meta->numeric_range.min) {
99 int middle = (meta->numeric_range.min + meta->numeric_range.max) / 2;
100 fprintf(output,
", '%d'", middle);
101 fprintf(output,
", '%d'", meta->numeric_range.max);
103 fprintf(output,
") }\n");
105 fprintf(output,
" @{ Name = '--%s'; Description = '", opt->long_name);
106 ps_escape_help(output, opt->help_text);
107 fprintf(output,
" (numeric %d-%d)'; Values = @(", meta->numeric_range.min, meta->numeric_range.max);
108 fprintf(output,
"'%d'", meta->numeric_range.min);
109 if (meta->numeric_range.max > meta->numeric_range.min) {
110 int middle = (meta->numeric_range.min + meta->numeric_range.max) / 2;
111 fprintf(output,
", '%d'", middle);
112 fprintf(output,
", '%d'", meta->numeric_range.max);
114 fprintf(output,
") }\n");
120 if (opt->short_name !=
'\0') {
121 fprintf(output,
" @{ Name = '-%c'; Description = '", opt->short_name);
122 ps_escape_help(output, opt->help_text);
123 fprintf(output,
"' }\n");
125 fprintf(output,
" @{ Name = '--%s'; Description = '", opt->long_name);
126 ps_escape_help(output, opt->help_text);
127 fprintf(output,
"' }\n");
132 return SET_ERRNO(ERROR_INVALID_PARAM,
"Output stream cannot be NULL");
135 fprintf(output,
"# PowerShell completion script for ascii-chat\n"
136 "# Generated from options registry - DO NOT EDIT MANUALLY\n"
137 "# Usage: ascii-chat --completions powershell | Out-String | Invoke-Expression\n"
139 "$script:AsciiChatCompleter = {\n"
140 " param($wordToComplete, $commandAst, $cursorPosition)\n"
142 " $words = @($commandAst.CommandElements | ForEach-Object { $_.Value })\n"
145 " foreach ($word in $words) {\n"
146 " if ($word -in @('server', 'client', 'mirror')) {\n"
152 " $binaryOptions = @(\n");
155 size_t binary_count = 0;
159 for (
size_t i = 0; i < binary_count; i++) {
160 ps_write_option(output, &binary_opts[i]);
162 SAFE_FREE(binary_opts);
165 fprintf(output,
" )\n\n $serverOptions = @(\n");
168 size_t server_count = 0;
172 for (
size_t i = 0; i < server_count; i++) {
173 ps_write_option(output, &server_opts[i]);
175 SAFE_FREE(server_opts);
178 fprintf(output,
" )\n\n $clientOptions = @(\n");
181 size_t client_count = 0;
185 for (
size_t i = 0; i < client_count; i++) {
186 ps_write_option(output, &client_opts[i]);
188 SAFE_FREE(client_opts);
191 fprintf(output,
" )\n\n $mirrorOptions = @(\n");
194 size_t mirror_count = 0;
198 for (
size_t i = 0; i < mirror_count; i++) {
199 ps_write_option(output, &mirror_opts[i]);
201 SAFE_FREE(mirror_opts);
204 fprintf(output,
" )\n\n $discoverySvcOptions = @(\n");
207 size_t discovery_svc_count = 0;
208 const option_descriptor_t *discovery_svc_opts =
211 if (discovery_svc_opts) {
212 for (
size_t i = 0; i < discovery_svc_count; i++) {
213 ps_write_option(output, &discovery_svc_opts[i]);
215 SAFE_FREE(discovery_svc_opts);
221 " $options = $binaryOptions\n"
223 " if ($mode -eq 'server') {\n"
224 " $options += $serverOptions\n"
225 " } elseif ($mode -eq 'client') {\n"
226 " $options += $clientOptions\n"
227 " } elseif ($mode -eq 'mirror') {\n"
228 " $options += $mirrorOptions\n"
229 " } elseif ($mode -eq 'discovery-service') {\n"
230 " $options += $discoverySvcOptions\n"
233 " if (-not $mode -and -not $wordToComplete.StartsWith('-')) {\n"
234 " @('server', 'client', 'mirror', 'discovery-service') | Where-Object { $_ -like \"$wordToComplete*\" } | "
236 " [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', \"Mode: $_\")\n"
239 " $options | Where-Object { $_.Name -like \"$wordToComplete*\" } | ForEach-Object {\n"
240 " if ($_.Values) {\n"
241 " $_.Values | ForEach-Object {\n"
242 " [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_.Description)\n"
245 " [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', "
252 "Register-ArgumentCompleter -CommandName ascii-chat -ScriptBlock $script:AsciiChatCompleter\n");
asciichat_error_t completions_generate_powershell(FILE *output)
const option_descriptor_t * options_registry_get_for_display(asciichat_mode_t mode, bool for_binary_help, size_t *num_options)
const option_metadata_t * options_registry_get_metadata(const char *long_name)