zod-args-parser 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -47,7 +47,8 @@ function ${cli.cliName} {
|
|
|
47
47
|
)
|
|
48
48
|
$scriptPath = (Get-Command '${cli.cliName}.ps1').Source
|
|
49
49
|
if ($scriptPath) {
|
|
50
|
-
|
|
50
|
+
$argumentList = @($subcommand) + ($arguments | Where-Object { $_ -notin '--', '--%' }) | Where-Object { $_ -ne '' }
|
|
51
|
+
& $scriptPath @argumentList
|
|
51
52
|
return
|
|
52
53
|
}
|
|
53
54
|
Write-Error "Could not find '${cli.cliName}.ps1' script"
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_utils","require","generateBashAutocompleteScript","_len","arguments","length","params","Array","_key","cli","subcommands","mappedCommands","reduce","acc","subcommand","name","options","map","option","transformOptionToArg","aliases","switchCase","key","Object","entries","join","cliName","keys","generatePowerShellAutocompleteScript","_len2","_key2","subcommandsStr","cliOptionsStr","optionsStr","forEach","a","functionInfo","description","example"],"sourceRoot":"../../src","sources":["autocomplete.ts"],"sourcesContent":["import { transformOptionToArg } from \"./utils.js\";\n\nimport type { Cli, Subcommand } from \"./types.js\";\n\n/**\n * - Generate bash autocomplete script for your CLI\n * - The generated script should be added to your `.bash_profile` or `.bashrc` file:\n *\n * - Run: `nano $HOME/.bash_profile` or `nano $HOME/.bashrc`\n * - Add the following line: `source <generated script path>`\n * - Save and reopen bash to take effect\n */\nexport function generateBashAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n let switchCase = \"\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n switchCase += ` ${key}${aliases.length ? \"|\" : \"\"}${aliases.join(\"|\")})\\n`;\n switchCase += ` opts=\"${options.join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n if (cli.options?.length) {\n switchCase += ` \"-\"*)\\n`;\n switchCase += ` opts=\"${cli.options.map(option => transformOptionToArg(option.name)).join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n return `_${cli.cliName}_autocomplete() {\n local cur prev commands opts subcommand used_opts filtered_opts\n\n cur=\"\\${COMP_WORDS[COMP_CWORD]}\"\n prev=\"\\${COMP_WORDS[COMP_CWORD-1]}\"\n subcommand=\"\\${COMP_WORDS[1]}\"\n\n commands=\"${Object.keys(mappedCommands).join(\" \")}\"\n\n case \"$subcommand\" in\n${switchCase}\n esac\n\n used_opts=\"\"\n if [[ \" \\${commands[@]} \" =~ \" $subcommand \" ]]; then\n for word in \"\\${COMP_WORDS[@]:2}\"; do\n if [[ \"$word\" =~ ^- ]]; then\n used_opts+=\" $word\"\n fi\n done\n fi\n\n if [[ -n \"$opts\" ]]; then\n filtered_opts=\"\"\n for opt in $opts; do\n if [[ ! \" $used_opts \" =~ \" $opt \" ]]; then\n filtered_opts+=\"$opt \"\n fi\n done\n COMPREPLY=( $(compgen -W \"$filtered_opts\" -- \"$cur\") )\n return\n fi\n\n if [[ \"\\${COMP_CWORD}\" -eq 1 ]]; then\n COMPREPLY=( $(compgen -W \"$commands\" -- \"$cur\") )\n fi\n}\n\ncomplete -F _${cli.cliName}_autocomplete ${cli.cliName}\n`;\n}\n\n/**\n * - Generates a PowerShell autocomplete script for your CLI.\n * - The script assumes that your CLI is available as a `.ps1` file in the environment variable. For example:\n * `cliName.ps1`.\n * - This should return a path to your script: `(Get-Command <cliName>.ps1).Source`\n * - The generated script should be added to your `profile.ps1` file:\n *\n * - Run: `notepad $profile`\n * - Add the following line: `. \"<generated script path>\"`\n * - Save and reopen powershell to take effect\n */\nexport function generatePowerShellAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n const subcommandsStr = Object.keys(mappedCommands)\n .map(key => `'${key}'`)\n .join(\", \");\n const cliOptionsStr = cli.options?.map(option => `'${transformOptionToArg(option.name)}'`).join(\", \") || \"\";\n\n let switchCase = \"switch ($subcommand) {\\n\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n const optionsStr = options.map(option => `'${option}'`).join(\", \");\n switchCase += ` '${key}' { @(${optionsStr}) }\\n`;\n aliases.forEach(a => (switchCase += ` '${a}' { @(${optionsStr}) }\\n`));\n }\n switchCase += ` default { @(${cliOptionsStr}) }\\n }`;\n\n let functionInfo = \"\";\n if (cli.description) {\n functionInfo = `<#\\n.DESCRIPTION\\n${cli.description}\\n${cli.example ? `\\n.EXAMPLE\\n${cli.example}` : \"\"}\\n#>`;\n }\n\n return `${functionInfo}\nfunction ${cli.cliName} {\n param(\n [Parameter(Position = 0, Mandatory = $false)]\n [string]$subcommand,\n [Parameter(Position = 1, ValueFromRemainingArguments = $true)]\n [string[]]$arguments\n )\n $scriptPath = (Get-Command '${cli.cliName}.ps1').Source\n if ($scriptPath) {\n
|
|
1
|
+
{"version":3,"names":["_utils","require","generateBashAutocompleteScript","_len","arguments","length","params","Array","_key","cli","subcommands","mappedCommands","reduce","acc","subcommand","name","options","map","option","transformOptionToArg","aliases","switchCase","key","Object","entries","join","cliName","keys","generatePowerShellAutocompleteScript","_len2","_key2","subcommandsStr","cliOptionsStr","optionsStr","forEach","a","functionInfo","description","example"],"sourceRoot":"../../src","sources":["autocomplete.ts"],"sourcesContent":["import { transformOptionToArg } from \"./utils.js\";\n\nimport type { Cli, Subcommand } from \"./types.js\";\n\n/**\n * - Generate bash autocomplete script for your CLI\n * - The generated script should be added to your `.bash_profile` or `.bashrc` file:\n *\n * - Run: `nano $HOME/.bash_profile` or `nano $HOME/.bashrc`\n * - Add the following line: `source <generated script path>`\n * - Save and reopen bash to take effect\n */\nexport function generateBashAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n let switchCase = \"\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n switchCase += ` ${key}${aliases.length ? \"|\" : \"\"}${aliases.join(\"|\")})\\n`;\n switchCase += ` opts=\"${options.join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n if (cli.options?.length) {\n switchCase += ` \"-\"*)\\n`;\n switchCase += ` opts=\"${cli.options.map(option => transformOptionToArg(option.name)).join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n return `_${cli.cliName}_autocomplete() {\n local cur prev commands opts subcommand used_opts filtered_opts\n\n cur=\"\\${COMP_WORDS[COMP_CWORD]}\"\n prev=\"\\${COMP_WORDS[COMP_CWORD-1]}\"\n subcommand=\"\\${COMP_WORDS[1]}\"\n\n commands=\"${Object.keys(mappedCommands).join(\" \")}\"\n\n case \"$subcommand\" in\n${switchCase}\n esac\n\n used_opts=\"\"\n if [[ \" \\${commands[@]} \" =~ \" $subcommand \" ]]; then\n for word in \"\\${COMP_WORDS[@]:2}\"; do\n if [[ \"$word\" =~ ^- ]]; then\n used_opts+=\" $word\"\n fi\n done\n fi\n\n if [[ -n \"$opts\" ]]; then\n filtered_opts=\"\"\n for opt in $opts; do\n if [[ ! \" $used_opts \" =~ \" $opt \" ]]; then\n filtered_opts+=\"$opt \"\n fi\n done\n COMPREPLY=( $(compgen -W \"$filtered_opts\" -- \"$cur\") )\n return\n fi\n\n if [[ \"\\${COMP_CWORD}\" -eq 1 ]]; then\n COMPREPLY=( $(compgen -W \"$commands\" -- \"$cur\") )\n fi\n}\n\ncomplete -F _${cli.cliName}_autocomplete ${cli.cliName}\n`;\n}\n\n/**\n * - Generates a PowerShell autocomplete script for your CLI.\n * - The script assumes that your CLI is available as a `.ps1` file in the environment variable. For example:\n * `cliName.ps1`.\n * - This should return a path to your script: `(Get-Command <cliName>.ps1).Source`\n * - The generated script should be added to your `profile.ps1` file:\n *\n * - Run: `notepad $profile`\n * - Add the following line: `. \"<generated script path>\"`\n * - Save and reopen powershell to take effect\n */\nexport function generatePowerShellAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n const subcommandsStr = Object.keys(mappedCommands)\n .map(key => `'${key}'`)\n .join(\", \");\n const cliOptionsStr = cli.options?.map(option => `'${transformOptionToArg(option.name)}'`).join(\", \") || \"\";\n\n let switchCase = \"switch ($subcommand) {\\n\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n const optionsStr = options.map(option => `'${option}'`).join(\", \");\n switchCase += ` '${key}' { @(${optionsStr}) }\\n`;\n aliases.forEach(a => (switchCase += ` '${a}' { @(${optionsStr}) }\\n`));\n }\n switchCase += ` default { @(${cliOptionsStr}) }\\n }`;\n\n let functionInfo = \"\";\n if (cli.description) {\n functionInfo = `<#\\n.DESCRIPTION\\n${cli.description}\\n${cli.example ? `\\n.EXAMPLE\\n${cli.example}` : \"\"}\\n#>`;\n }\n\n return `${functionInfo}\nfunction ${cli.cliName} {\n param(\n [Parameter(Position = 0, Mandatory = $false)]\n [string]$subcommand,\n [Parameter(Position = 1, ValueFromRemainingArguments = $true)]\n [string[]]$arguments\n )\n $scriptPath = (Get-Command '${cli.cliName}.ps1').Source\n if ($scriptPath) {\n $argumentList = @($subcommand) + ($arguments | Where-Object { $_ -notin '--', '--%' }) | Where-Object { $_ -ne '' }\n & $scriptPath @argumentList\n return\n }\n Write-Error \"Could not find '${cli.cliName}.ps1' script\"\n}\n\nRegister-ArgumentCompleter -CommandName '${cli.cliName}' -ParameterName 'subcommand' -ScriptBlock {\n param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)\n $subcommands = @(${subcommandsStr}${subcommandsStr && cliOptionsStr ? \", \" : \"\"}${cliOptionsStr})\n $subcommands | Where-Object { $_ -like \"$wordToComplete*\" }\n}\n\nRegister-ArgumentCompleter -CommandName '${cli.cliName}' -ParameterName 'arguments' -ScriptBlock {\n param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)\n $subcommand = $commandAst.CommandElements[1].Value\n $arguments = ${switchCase}\n $arguments | Where-Object { $_ -like \"$wordToComplete*\" }\n}`;\n}\n"],"mappings":"8NAAA,IAAAA,MAAA,CAAAC,OAAA,eAYO,QAAS,CAAAC,8BAA8BA,CAAA,CAA4C,SAAAC,IAAA,CAAAC,SAAA,CAAAC,MAAA,CAAxCC,MAAM,KAAAC,KAAA,CAAAJ,IAAA,EAAAK,IAAA,GAAAA,IAAA,CAAAL,IAAA,CAAAK,IAAA,IAANF,MAAM,CAAAE,IAAA,EAAAJ,SAAA,CAAAI,IAAA,GACtD,KAAM,CAACC,GAAG,CAAE,GAAGC,WAAW,CAAC,CAAGJ,MAAM,CAIpC,KAAM,CAAAK,cAAc,CAAGD,WAAW,CAACE,MAAM,CAAC,CAACC,GAAmB,CAAEC,UAAU,GAAK,CAC7ED,GAAG,CAACC,UAAU,CAACC,IAAI,CAAC,CAAG,CACrBC,OAAO,CAAEF,UAAU,CAACE,OAAO,E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C,OAAO,CAACb,cAAc,CAAC,CAAE,CACxE,KAAM,CAAAsB,UAAU,CAAGjB,OAAO,CAACC,GAAG,CAACC,MAAM,EAAI,IAAIA,MAAM,GAAG,CAAC,CAACO,IAAI,CAAC,IAAI,CAAC,CAClEJ,UAAU,EAAI,YAAYC,GAAG,SAASW,UAAU,OAAO,CACvDb,OAAO,CAACc,OAAO,CAACC,CAAC,EAAKd,UAAU,EAAI,YAAYc,CAAC,SAASF,UAAU,OAAQ,CAAC,CAC/E,CACAZ,UAAU,EAAI,uBAAuBW,aAAa,YAAY,CAE9D,GAAI,CAAAI,YAAY,CAAG,EAAE,CACrB,GAAI3B,GAAG,CAAC4B,WAAW,CAAE,CACnBD,YAAY,CAAG,qBAAqB3B,GAAG,CAAC4B,WAAW,KAAK5B,GAAG,CAAC6B,OAAO,CAAG,eAAe7B,GAAG,CAAC6B,OAAO,EAAE,CAAG,EAAE,MAAM,CAC/G,CAEA,MAAO,GAAGF,YAAY;AACxB,WAAW3B,GAAG,CAACiB,OAAO;AACtB;AACA;AACA;AACA;AACA;AACA;AACA,kCAAkCjB,GAAG,CAACiB,OAAO;AAC7C;AACA;AACA;AACA;AACA;AACA,mCAAmCjB,GAAG,CAACiB,OAAO;AAC9C;AACA;AACA,2CAA2CjB,GAAG,CAACiB,OAAO;AACtD;AACA,uBAAuBK,cAAc,GAAGA,cAAc,EAAIC,aAAa,CAAG,IAAI,CAAG,EAAE,GAAGA,aAAa;AACnG;AACA;AACA;AACA,2CAA2CvB,GAAG,CAACiB,OAAO;AACtD;AACA;AACA,mBAAmBL,UAAU;AAC7B;AACA,EAAE,CACF","ignoreList":[]}
|
|
@@ -47,7 +47,8 @@ function ${cli.cliName} {
|
|
|
47
47
|
)
|
|
48
48
|
$scriptPath = (Get-Command '${cli.cliName}.ps1').Source
|
|
49
49
|
if ($scriptPath) {
|
|
50
|
-
|
|
50
|
+
$argumentList = @($subcommand) + ($arguments | Where-Object { $_ -notin '--', '--%' }) | Where-Object { $_ -ne '' }
|
|
51
|
+
& $scriptPath @argumentList
|
|
51
52
|
return
|
|
52
53
|
}
|
|
53
54
|
Write-Error "Could not find '${cli.cliName}.ps1' script"
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["transformOptionToArg","generateBashAutocompleteScript","_len","arguments","length","params","Array","_key","cli","subcommands","mappedCommands","reduce","acc","subcommand","name","options","map","option","aliases","switchCase","key","Object","entries","join","cliName","keys","generatePowerShellAutocompleteScript","_len2","_key2","subcommandsStr","cliOptionsStr","optionsStr","forEach","a","functionInfo","description","example"],"sourceRoot":"../../src","sources":["autocomplete.ts"],"sourcesContent":["import { transformOptionToArg } from \"./utils.js\";\n\nimport type { Cli, Subcommand } from \"./types.js\";\n\n/**\n * - Generate bash autocomplete script for your CLI\n * - The generated script should be added to your `.bash_profile` or `.bashrc` file:\n *\n * - Run: `nano $HOME/.bash_profile` or `nano $HOME/.bashrc`\n * - Add the following line: `source <generated script path>`\n * - Save and reopen bash to take effect\n */\nexport function generateBashAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n let switchCase = \"\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n switchCase += ` ${key}${aliases.length ? \"|\" : \"\"}${aliases.join(\"|\")})\\n`;\n switchCase += ` opts=\"${options.join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n if (cli.options?.length) {\n switchCase += ` \"-\"*)\\n`;\n switchCase += ` opts=\"${cli.options.map(option => transformOptionToArg(option.name)).join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n return `_${cli.cliName}_autocomplete() {\n local cur prev commands opts subcommand used_opts filtered_opts\n\n cur=\"\\${COMP_WORDS[COMP_CWORD]}\"\n prev=\"\\${COMP_WORDS[COMP_CWORD-1]}\"\n subcommand=\"\\${COMP_WORDS[1]}\"\n\n commands=\"${Object.keys(mappedCommands).join(\" \")}\"\n\n case \"$subcommand\" in\n${switchCase}\n esac\n\n used_opts=\"\"\n if [[ \" \\${commands[@]} \" =~ \" $subcommand \" ]]; then\n for word in \"\\${COMP_WORDS[@]:2}\"; do\n if [[ \"$word\" =~ ^- ]]; then\n used_opts+=\" $word\"\n fi\n done\n fi\n\n if [[ -n \"$opts\" ]]; then\n filtered_opts=\"\"\n for opt in $opts; do\n if [[ ! \" $used_opts \" =~ \" $opt \" ]]; then\n filtered_opts+=\"$opt \"\n fi\n done\n COMPREPLY=( $(compgen -W \"$filtered_opts\" -- \"$cur\") )\n return\n fi\n\n if [[ \"\\${COMP_CWORD}\" -eq 1 ]]; then\n COMPREPLY=( $(compgen -W \"$commands\" -- \"$cur\") )\n fi\n}\n\ncomplete -F _${cli.cliName}_autocomplete ${cli.cliName}\n`;\n}\n\n/**\n * - Generates a PowerShell autocomplete script for your CLI.\n * - The script assumes that your CLI is available as a `.ps1` file in the environment variable. For example:\n * `cliName.ps1`.\n * - This should return a path to your script: `(Get-Command <cliName>.ps1).Source`\n * - The generated script should be added to your `profile.ps1` file:\n *\n * - Run: `notepad $profile`\n * - Add the following line: `. \"<generated script path>\"`\n * - Save and reopen powershell to take effect\n */\nexport function generatePowerShellAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n const subcommandsStr = Object.keys(mappedCommands)\n .map(key => `'${key}'`)\n .join(\", \");\n const cliOptionsStr = cli.options?.map(option => `'${transformOptionToArg(option.name)}'`).join(\", \") || \"\";\n\n let switchCase = \"switch ($subcommand) {\\n\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n const optionsStr = options.map(option => `'${option}'`).join(\", \");\n switchCase += ` '${key}' { @(${optionsStr}) }\\n`;\n aliases.forEach(a => (switchCase += ` '${a}' { @(${optionsStr}) }\\n`));\n }\n switchCase += ` default { @(${cliOptionsStr}) }\\n }`;\n\n let functionInfo = \"\";\n if (cli.description) {\n functionInfo = `<#\\n.DESCRIPTION\\n${cli.description}\\n${cli.example ? `\\n.EXAMPLE\\n${cli.example}` : \"\"}\\n#>`;\n }\n\n return `${functionInfo}\nfunction ${cli.cliName} {\n param(\n [Parameter(Position = 0, Mandatory = $false)]\n [string]$subcommand,\n [Parameter(Position = 1, ValueFromRemainingArguments = $true)]\n [string[]]$arguments\n )\n $scriptPath = (Get-Command '${cli.cliName}.ps1').Source\n if ($scriptPath) {\n
|
|
1
|
+
{"version":3,"names":["transformOptionToArg","generateBashAutocompleteScript","_len","arguments","length","params","Array","_key","cli","subcommands","mappedCommands","reduce","acc","subcommand","name","options","map","option","aliases","switchCase","key","Object","entries","join","cliName","keys","generatePowerShellAutocompleteScript","_len2","_key2","subcommandsStr","cliOptionsStr","optionsStr","forEach","a","functionInfo","description","example"],"sourceRoot":"../../src","sources":["autocomplete.ts"],"sourcesContent":["import { transformOptionToArg } from \"./utils.js\";\n\nimport type { Cli, Subcommand } from \"./types.js\";\n\n/**\n * - Generate bash autocomplete script for your CLI\n * - The generated script should be added to your `.bash_profile` or `.bashrc` file:\n *\n * - Run: `nano $HOME/.bash_profile` or `nano $HOME/.bashrc`\n * - Add the following line: `source <generated script path>`\n * - Save and reopen bash to take effect\n */\nexport function generateBashAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n let switchCase = \"\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n switchCase += ` ${key}${aliases.length ? \"|\" : \"\"}${aliases.join(\"|\")})\\n`;\n switchCase += ` opts=\"${options.join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n if (cli.options?.length) {\n switchCase += ` \"-\"*)\\n`;\n switchCase += ` opts=\"${cli.options.map(option => transformOptionToArg(option.name)).join(\" \")}\"\\n`;\n switchCase += \" ;;\\n\";\n }\n\n return `_${cli.cliName}_autocomplete() {\n local cur prev commands opts subcommand used_opts filtered_opts\n\n cur=\"\\${COMP_WORDS[COMP_CWORD]}\"\n prev=\"\\${COMP_WORDS[COMP_CWORD-1]}\"\n subcommand=\"\\${COMP_WORDS[1]}\"\n\n commands=\"${Object.keys(mappedCommands).join(\" \")}\"\n\n case \"$subcommand\" in\n${switchCase}\n esac\n\n used_opts=\"\"\n if [[ \" \\${commands[@]} \" =~ \" $subcommand \" ]]; then\n for word in \"\\${COMP_WORDS[@]:2}\"; do\n if [[ \"$word\" =~ ^- ]]; then\n used_opts+=\" $word\"\n fi\n done\n fi\n\n if [[ -n \"$opts\" ]]; then\n filtered_opts=\"\"\n for opt in $opts; do\n if [[ ! \" $used_opts \" =~ \" $opt \" ]]; then\n filtered_opts+=\"$opt \"\n fi\n done\n COMPREPLY=( $(compgen -W \"$filtered_opts\" -- \"$cur\") )\n return\n fi\n\n if [[ \"\\${COMP_CWORD}\" -eq 1 ]]; then\n COMPREPLY=( $(compgen -W \"$commands\" -- \"$cur\") )\n fi\n}\n\ncomplete -F _${cli.cliName}_autocomplete ${cli.cliName}\n`;\n}\n\n/**\n * - Generates a PowerShell autocomplete script for your CLI.\n * - The script assumes that your CLI is available as a `.ps1` file in the environment variable. For example:\n * `cliName.ps1`.\n * - This should return a path to your script: `(Get-Command <cliName>.ps1).Source`\n * - The generated script should be added to your `profile.ps1` file:\n *\n * - Run: `notepad $profile`\n * - Add the following line: `. \"<generated script path>\"`\n * - Save and reopen powershell to take effect\n */\nexport function generatePowerShellAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n type MappedCommands = Record<string, { options: string[]; aliases: string[] }>;\n\n const mappedCommands = subcommands.reduce((acc: MappedCommands, subcommand) => {\n acc[subcommand.name] = {\n options: subcommand.options?.map(option => transformOptionToArg(option.name)) ?? [],\n aliases: subcommand.aliases ?? [],\n };\n return acc;\n }, {});\n\n const subcommandsStr = Object.keys(mappedCommands)\n .map(key => `'${key}'`)\n .join(\", \");\n const cliOptionsStr = cli.options?.map(option => `'${transformOptionToArg(option.name)}'`).join(\", \") || \"\";\n\n let switchCase = \"switch ($subcommand) {\\n\";\n for (const [key, { options, aliases }] of Object.entries(mappedCommands)) {\n const optionsStr = options.map(option => `'${option}'`).join(\", \");\n switchCase += ` '${key}' { @(${optionsStr}) }\\n`;\n aliases.forEach(a => (switchCase += ` '${a}' { @(${optionsStr}) }\\n`));\n }\n switchCase += ` default { @(${cliOptionsStr}) }\\n }`;\n\n let functionInfo = \"\";\n if (cli.description) {\n functionInfo = `<#\\n.DESCRIPTION\\n${cli.description}\\n${cli.example ? `\\n.EXAMPLE\\n${cli.example}` : \"\"}\\n#>`;\n }\n\n return `${functionInfo}\nfunction ${cli.cliName} {\n param(\n [Parameter(Position = 0, Mandatory = $false)]\n [string]$subcommand,\n [Parameter(Position = 1, ValueFromRemainingArguments = $true)]\n [string[]]$arguments\n )\n $scriptPath = (Get-Command '${cli.cliName}.ps1').Source\n if ($scriptPath) {\n $argumentList = @($subcommand) + ($arguments | Where-Object { $_ -notin '--', '--%' }) | Where-Object { $_ -ne '' }\n & $scriptPath @argumentList\n return\n }\n Write-Error \"Could not find '${cli.cliName}.ps1' script\"\n}\n\nRegister-ArgumentCompleter -CommandName '${cli.cliName}' -ParameterName 'subcommand' -ScriptBlock {\n param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)\n $subcommands = @(${subcommandsStr}${subcommandsStr && cliOptionsStr ? \", \" : \"\"}${cliOptionsStr})\n $subcommands | Where-Object { $_ -like \"$wordToComplete*\" }\n}\n\nRegister-ArgumentCompleter -CommandName '${cli.cliName}' -ParameterName 'arguments' -ScriptBlock {\n param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)\n $subcommand = $commandAst.CommandElements[1].Value\n $arguments = ${switchCase}\n $arguments | Where-Object { $_ -like \"$wordToComplete*\" }\n}`;\n}\n"],"mappings":"AAAA,OAASA,oBAAoB,KAAQ,YAAY,CAYjD,MAAO,SAAS,CAAAC,8BAA8BA,CAAA,CAA4C,SAAAC,IAAA,CAAAC,SAAA,CAAAC,MAAA,CAAxCC,MAAM,KAAAC,KAAA,CAAAJ,IAAA,EAAAK,IAAA,GAAAA,IAAA,CAAAL,IAAA,CAAAK,IAAA,IAANF,MAAM,CAAAE,IAAA,EAAAJ,SAAA,CAAAI,IAAA,GACtD,KAAM,CAACC,GAAG,CAAE,GAAGC,WAAW,CAAC,CAAGJ,MAAM,CAIpC,KAAM,CAAAK,cAAc,CAAGD,WAAW,CAACE,MAAM,CAAC,CAACC,GAAmB,CAAEC,UAAU,GAAK,CAC7ED,GAAG,CAACC,UAAU,CAACC,IAAI,CAAC,CAAG,CACrBC,OAAO,CAAEF,UAAU,CAACE,OAAO,EAAEC,GAAG,CAACC,MAAM,EAAIjB,oBAAoB,CAACiB,MAAM,CAACH,IAAI,CAAC,CAAC,EAAI,EAAE,CACnFI,OAAO,CAAEL,UAAU,CAACK,OAAO,EAAI,EACjC,CAAC,CACD,MAAO,CAAAN,GAAG,CACZ,CAAC,CAAE,CAAC,CAAC,CAAC,CAEN,GAAI,CAAAO,UAAU,CAAG,EAAE,CACnB,IAAK,KAAM,CAACC,GAAG,CAAE,CAAEL,OAAO,CAAEG,OAAQ,CAAC,CAAC,EAAI,CA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AAa,YAAY,CAE9D,GAAI,CAAAI,YAAY,CAAG,EAAE,CACrB,GAAI1B,GAAG,CAAC2B,WAAW,CAAE,CACnBD,YAAY,CAAG,qBAAqB1B,GAAG,CAAC2B,WAAW,KAAK3B,GAAG,CAAC4B,OAAO,CAAG,eAAe5B,GAAG,CAAC4B,OAAO,EAAE,CAAG,EAAE,MAAM,CAC/G,CAEA,MAAO,GAAGF,YAAY;AACxB,WAAW1B,GAAG,CAACgB,OAAO;AACtB;AACA;AACA;AACA;AACA;AACA;AACA,kCAAkChB,GAAG,CAACgB,OAAO;AAC7C;AACA;AACA;AACA;AACA;AACA,mCAAmChB,GAAG,CAACgB,OAAO;AAC9C;AACA;AACA,2CAA2ChB,GAAG,CAACgB,OAAO;AACtD;AACA,uBAAuBK,cAAc,GAAGA,cAAc,EAAIC,aAAa,CAAG,IAAI,CAAG,EAAE,GAAGA,aAAa;AACnG;AACA;AACA;AACA,2CAA2CtB,GAAG,CAACgB,OAAO;AACtD;AACA;AACA,mBAAmBL,UAAU;AAC7B;AACA,EAAE,CACF","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"autocomplete.d.ts","sourceRoot":"","sources":["../../src/autocomplete.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;GAOG;AACH,wBAAgB,8BAA8B,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,MAAM,CAkExF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oCAAoC,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"autocomplete.d.ts","sourceRoot":"","sources":["../../src/autocomplete.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;GAOG;AACH,wBAAgB,8BAA8B,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,MAAM,CAkExF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oCAAoC,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,MAAM,CA4D9F"}
|
package/package.json
CHANGED
package/src/autocomplete.ts
CHANGED
|
@@ -130,7 +130,8 @@ function ${cli.cliName} {
|
|
|
130
130
|
)
|
|
131
131
|
$scriptPath = (Get-Command '${cli.cliName}.ps1').Source
|
|
132
132
|
if ($scriptPath) {
|
|
133
|
-
|
|
133
|
+
$argumentList = @($subcommand) + ($arguments | Where-Object { $_ -notin '--', '--%' }) | Where-Object { $_ -ne '' }
|
|
134
|
+
& $scriptPath @argumentList
|
|
134
135
|
return
|
|
135
136
|
}
|
|
136
137
|
Write-Error "Could not find '${cli.cliName}.ps1' script"
|