zod-args-parser 1.0.4 → 1.0.5
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.
- package/lib/commonjs/autocomplete.js +40 -0
- package/lib/commonjs/autocomplete.js.map +1 -0
- package/lib/commonjs/index.js +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/autocomplete.js +40 -0
- package/lib/module/autocomplete.js.map +1 -0
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/autocomplete.d.ts +4 -0
- package/lib/typescript/autocomplete.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/autocomplete.ts +70 -0
- package/src/index.ts +13 -3
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.generateAutocompleteScript=generateAutocompleteScript;var _utils=require("./utils.js");function generateAutocompleteScript(){for(var _len=arguments.length,params=new Array(_len),_key=0;_key<_len;_key++){params[_key]=arguments[_key];}const[cli,...subcommands]=params;const commandName=cli.cliName;const commands=subcommands.map(subcommand=>subcommand.name);const commandOptions=subcommands.reduce((acc,subcommand)=>{if(!subcommand.name||!subcommand.options?.length)return acc;acc[subcommand.name]=subcommand.options.map(option=>(0,_utils.transformOptionToArg)(option.name));return acc;},{});let switchCase="";for(const[key,value]of Object.entries(commandOptions)){switchCase+=` ${key}|${key[0]})\n`;switchCase+=` opts="${value.join(" ")}"\n`;switchCase+=" ;;\n";}if(cli.options?.length){switchCase+=` "-"*)\n`;switchCase+=` opts="${cli.options.map(option=>(0,_utils.transformOptionToArg)(option.name)).join(" ")}"\n`;switchCase+=" ;;\n";}return`_${commandName}_autocomplete() {
|
|
2
|
+
local cur prev commands opts subcommand used_opts filtered_opts
|
|
3
|
+
|
|
4
|
+
cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
5
|
+
prev="\${COMP_WORDS[COMP_CWORD-1]}"
|
|
6
|
+
subcommand="\${COMP_WORDS[1]}"
|
|
7
|
+
|
|
8
|
+
commands="${commands.join(" ")}"
|
|
9
|
+
|
|
10
|
+
case "$subcommand" in
|
|
11
|
+
${switchCase}
|
|
12
|
+
esac
|
|
13
|
+
|
|
14
|
+
used_opts=""
|
|
15
|
+
if [[ " \${commands[@]} " =~ " $subcommand " ]]; then
|
|
16
|
+
for word in "\${COMP_WORDS[@]:2}"; do
|
|
17
|
+
if [[ "$word" =~ ^- ]]; then
|
|
18
|
+
used_opts+=" $word"
|
|
19
|
+
fi
|
|
20
|
+
done
|
|
21
|
+
fi
|
|
22
|
+
|
|
23
|
+
if [[ -n "$opts" ]]; then
|
|
24
|
+
filtered_opts=""
|
|
25
|
+
for opt in $opts; do
|
|
26
|
+
if [[ ! " $used_opts " =~ " $opt " ]]; then
|
|
27
|
+
filtered_opts+="$opt "
|
|
28
|
+
fi
|
|
29
|
+
done
|
|
30
|
+
COMPREPLY=( $(compgen -W "$filtered_opts" -- "$cur") )
|
|
31
|
+
return
|
|
32
|
+
fi
|
|
33
|
+
|
|
34
|
+
if [[ "\${COMP_CWORD}" -eq 1 ]]; then
|
|
35
|
+
COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
|
|
36
|
+
fi
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
complete -F _${commandName}_autocomplete ${commandName}
|
|
40
|
+
`;}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_utils","require","generateAutocompleteScript","_len","arguments","length","params","Array","_key","cli","subcommands","commandName","cliName","commands","map","subcommand","name","commandOptions","reduce","acc","options","option","transformOptionToArg","switchCase","key","value","Object","entries","join"],"sourceRoot":"../../src","sources":["autocomplete.ts"],"sourcesContent":["import { transformOptionToArg } from \"./utils.js\";\n\nimport type { Cli, Subcommand } from \"./types.js\";\n\n/** - Generate bash autocomplete script for your CLI */\nexport function generateAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n const commandName = cli.cliName;\n const commands = subcommands.map(subcommand => subcommand.name);\n const commandOptions = subcommands.reduce((acc: Record<string, string[]>, subcommand) => {\n if (!subcommand.name || !subcommand.options?.length) return acc;\n acc[subcommand.name] = subcommand.options.map(option => transformOptionToArg(option.name));\n return acc;\n }, {});\n\n let switchCase = \"\";\n for (const [key, value] of Object.entries(commandOptions)) {\n switchCase += ` ${key}|${key[0]})\\n`;\n switchCase += ` opts=\"${value.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 `_${commandName}_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=\"${commands.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 _${commandName}_autocomplete ${commandName}\n`;\n}\n"],"mappings":"oIAAA,IAAAA,MAAA,CAAAC,OAAA,eAKO,QAAS,CAAAC,0BAA0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EAAI,eAAed,GAAG,CAACW,OAAO,CAACN,GAAG,CAACO,MAAM,EAAI,GAAAC,2BAAoB,EAACD,MAAM,CAACL,IAAI,CAAC,CAAC,CAACY,IAAI,CAAC,GAAG,CAAC,KAAK,CACxGL,UAAU,EAAI,YAAY,CAC5B,CAEA,MAAO,IAAIZ,WAAW;AACxB;AACA;AACA;AACA;AACA;AACA;AACA,cAAcE,QAAQ,CAACe,IAAI,CAAC,GAAG,CAAC;AAChC;AACA;AACA,EAAEL,UAAU;AACZ;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAeZ,WAAW,iBAAiBA,WAAW;AACtD,CAAC,CACD","ignoreList":[]}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createCli=createCli;exports.createOptions=createOptions;exports.createSubcommand=createSubcommand;Object.defineProperty(exports,"parse",{enumerable:true,get:function(){return _parser.parse;}});exports.printSubcommandHelp=exports.printCliHelp=void 0;Object.defineProperty(exports,"safeParse",{enumerable:true,get:function(){return _parser.safeParse;}});var _help=require("./help.js");var _parser=require("./parser.js");function
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createCli=createCli;exports.createOptions=createOptions;exports.createSubcommand=createSubcommand;Object.defineProperty(exports,"generateAutocompleteScript",{enumerable:true,get:function(){return _autocomplete.generateAutocompleteScript;}});Object.defineProperty(exports,"parse",{enumerable:true,get:function(){return _parser.parse;}});exports.printSubcommandHelp=exports.printCliHelp=void 0;Object.defineProperty(exports,"safeParse",{enumerable:true,get:function(){return _parser.safeParse;}});var _help=require("./help.js");var _parser=require("./parser.js");var _autocomplete=require("./autocomplete.js");function createCli(input){return Object.assign(input,{setAction:action=>input.action=action});}function createSubcommand(input){return Object.assign(input,{setAction:action=>input.action=action});}function createOptions(options){return options;}const{printCliHelp,printSubcommandHelp}=_help.help;exports.printSubcommandHelp=printSubcommandHelp;exports.printCliHelp=printCliHelp;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_help","require","_parser","
|
|
1
|
+
{"version":3,"names":["_help","require","_parser","_autocomplete","createCli","input","Object","assign","setAction","action","createSubcommand","createOptions","options","printCliHelp","printSubcommandHelp","help","exports"],"sourceRoot":"../../src","sources":["index.ts"],"sourcesContent":["import { help } from \"./help.js\";\nimport { parse, safeParse } from \"./parser.js\";\nimport { generateAutocompleteScript } from \"./autocomplete.js\";\n\nimport type {\n ActionFn,\n CheckDuplicatedOptions,\n Cli,\n Option,\n Prettify,\n Subcommand,\n UnSafeParseResult,\n} from \"./types.js\";\n\nfunction createCli<const T extends Cli>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nfunction createSubcommand<const T extends Subcommand>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nfunction createOptions<const T extends [Option, ...Option[]]>(options: T): T {\n return options;\n}\n\nconst { printCliHelp, printSubcommandHelp } = help;\n\nexport {\n createCli,\n createSubcommand,\n createOptions,\n parse,\n safeParse,\n printCliHelp,\n printSubcommandHelp,\n generateAutocompleteScript,\n};\n\nexport type * from \"./types.js\";\n"],"mappings":"6jBAAA,IAAAA,KAAA,CAAAC,OAAA,cACA,IAAAC,OAAA,CAAAD,OAAA,gBACA,IAAAE,aAAA,CAAAF,OAAA,sBAYA,QAAS,CAAAG,SAASA,CAAsBC,KAAgC,CAA6B,CACnG,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,QAAS,CAAAC,gBAAgBA,CAA6BL,KAAgC,CAA6B,CACjH,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,QAAS,CAAAE,aAAaA,CAAwCC,OAAU,CAAK,CAC3E,MAAO,CAAAA,OAAO,CAChB,CAEA,KAAM,CAAEC,YAAY,CAAEC,mBAAoB,CAAC,CAAGC,UAAI,CAACC,OAAA,CAAAF,mBAAA,CAAAA,mBAAA,CAAAE,OAAA,CAAAH,YAAA,CAAAA,YAAA","ignoreList":[]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import{transformOptionToArg}from"./utils.js";export function generateAutocompleteScript(){for(var _len=arguments.length,params=new Array(_len),_key=0;_key<_len;_key++){params[_key]=arguments[_key];}const[cli,...subcommands]=params;const commandName=cli.cliName;const commands=subcommands.map(subcommand=>subcommand.name);const commandOptions=subcommands.reduce((acc,subcommand)=>{if(!subcommand.name||!subcommand.options?.length)return acc;acc[subcommand.name]=subcommand.options.map(option=>transformOptionToArg(option.name));return acc;},{});let switchCase="";for(const[key,value]of Object.entries(commandOptions)){switchCase+=` ${key}|${key[0]})\n`;switchCase+=` opts="${value.join(" ")}"\n`;switchCase+=" ;;\n";}if(cli.options?.length){switchCase+=` "-"*)\n`;switchCase+=` opts="${cli.options.map(option=>transformOptionToArg(option.name)).join(" ")}"\n`;switchCase+=" ;;\n";}return`_${commandName}_autocomplete() {
|
|
2
|
+
local cur prev commands opts subcommand used_opts filtered_opts
|
|
3
|
+
|
|
4
|
+
cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
5
|
+
prev="\${COMP_WORDS[COMP_CWORD-1]}"
|
|
6
|
+
subcommand="\${COMP_WORDS[1]}"
|
|
7
|
+
|
|
8
|
+
commands="${commands.join(" ")}"
|
|
9
|
+
|
|
10
|
+
case "$subcommand" in
|
|
11
|
+
${switchCase}
|
|
12
|
+
esac
|
|
13
|
+
|
|
14
|
+
used_opts=""
|
|
15
|
+
if [[ " \${commands[@]} " =~ " $subcommand " ]]; then
|
|
16
|
+
for word in "\${COMP_WORDS[@]:2}"; do
|
|
17
|
+
if [[ "$word" =~ ^- ]]; then
|
|
18
|
+
used_opts+=" $word"
|
|
19
|
+
fi
|
|
20
|
+
done
|
|
21
|
+
fi
|
|
22
|
+
|
|
23
|
+
if [[ -n "$opts" ]]; then
|
|
24
|
+
filtered_opts=""
|
|
25
|
+
for opt in $opts; do
|
|
26
|
+
if [[ ! " $used_opts " =~ " $opt " ]]; then
|
|
27
|
+
filtered_opts+="$opt "
|
|
28
|
+
fi
|
|
29
|
+
done
|
|
30
|
+
COMPREPLY=( $(compgen -W "$filtered_opts" -- "$cur") )
|
|
31
|
+
return
|
|
32
|
+
fi
|
|
33
|
+
|
|
34
|
+
if [[ "\${COMP_CWORD}" -eq 1 ]]; then
|
|
35
|
+
COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
|
|
36
|
+
fi
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
complete -F _${commandName}_autocomplete ${commandName}
|
|
40
|
+
`;}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["transformOptionToArg","generateAutocompleteScript","_len","arguments","length","params","Array","_key","cli","subcommands","commandName","cliName","commands","map","subcommand","name","commandOptions","reduce","acc","options","option","switchCase","key","value","Object","entries","join"],"sourceRoot":"../../src","sources":["autocomplete.ts"],"sourcesContent":["import { transformOptionToArg } from \"./utils.js\";\n\nimport type { Cli, Subcommand } from \"./types.js\";\n\n/** - Generate bash autocomplete script for your CLI */\nexport function generateAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {\n const [cli, ...subcommands] = params;\n\n const commandName = cli.cliName;\n const commands = subcommands.map(subcommand => subcommand.name);\n const commandOptions = subcommands.reduce((acc: Record<string, string[]>, subcommand) => {\n if (!subcommand.name || !subcommand.options?.length) return acc;\n acc[subcommand.name] = subcommand.options.map(option => transformOptionToArg(option.name));\n return acc;\n }, {});\n\n let switchCase = \"\";\n for (const [key, value] of Object.entries(commandOptions)) {\n switchCase += ` ${key}|${key[0]})\\n`;\n switchCase += ` opts=\"${value.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 `_${commandName}_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=\"${commands.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 _${commandName}_autocomplete ${commandName}\n`;\n}\n"],"mappings":"AAAA,OAASA,oBAAoB,KAAQ,YAAY,CAKjD,MAAO,SAAS,CAAAC,0BAA0BA,CAAA,CAA4C,SAAAC,IAAA,CAAAC,SAAA,CAAAC,MAAA,CAAxCC,MAAM,KAAAC,KAAA,CAAAJ,IAAA,EAAAK,IAAA,GA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AAI,CAAC,GAAG,CAAC,KAAK,CACxGL,UAAU,EAAI,YAAY,CAC5B,CAEA,MAAO,IAAIX,WAAW;AACxB;AACA;AACA;AACA;AACA;AACA;AACA,cAAcE,QAAQ,CAACc,IAAI,CAAC,GAAG,CAAC;AAChC;AACA;AACA,EAAEL,UAAU;AACZ;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAeX,WAAW,iBAAiBA,WAAW;AACtD,CAAC,CACD","ignoreList":[]}
|
package/lib/module/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{help}from"./help.js";import{parse,safeParse}from"./parser.js";function
|
|
1
|
+
import{help}from"./help.js";import{parse,safeParse}from"./parser.js";import{generateAutocompleteScript}from"./autocomplete.js";function createCli(input){return Object.assign(input,{setAction:action=>input.action=action});}function createSubcommand(input){return Object.assign(input,{setAction:action=>input.action=action});}function createOptions(options){return options;}const{printCliHelp,printSubcommandHelp}=help;export{createCli,createSubcommand,createOptions,parse,safeParse,printCliHelp,printSubcommandHelp,generateAutocompleteScript};
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["help","parse","safeParse","
|
|
1
|
+
{"version":3,"names":["help","parse","safeParse","generateAutocompleteScript","createCli","input","Object","assign","setAction","action","createSubcommand","createOptions","options","printCliHelp","printSubcommandHelp"],"sourceRoot":"../../src","sources":["index.ts"],"sourcesContent":["import { help } from \"./help.js\";\nimport { parse, safeParse } from \"./parser.js\";\nimport { generateAutocompleteScript } from \"./autocomplete.js\";\n\nimport type {\n ActionFn,\n CheckDuplicatedOptions,\n Cli,\n Option,\n Prettify,\n Subcommand,\n UnSafeParseResult,\n} from \"./types.js\";\n\nfunction createCli<const T extends Cli>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nfunction createSubcommand<const T extends Subcommand>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nfunction createOptions<const T extends [Option, ...Option[]]>(options: T): T {\n return options;\n}\n\nconst { printCliHelp, printSubcommandHelp } = help;\n\nexport {\n createCli,\n createSubcommand,\n createOptions,\n parse,\n safeParse,\n printCliHelp,\n printSubcommandHelp,\n generateAutocompleteScript,\n};\n\nexport type * from \"./types.js\";\n"],"mappings":"AAAA,OAASA,IAAI,KAAQ,WAAW,CAChC,OAASC,KAAK,CAAEC,SAAS,KAAQ,aAAa,CAC9C,OAASC,0BAA0B,KAAQ,mBAAmB,CAY9D,QAAS,CAAAC,SAASA,CAAsBC,KAAgC,CAA6B,CACnG,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,QAAS,CAAAC,gBAAgBA,CAA6BL,KAAgC,CAA6B,CACjH,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,QAAS,CAAAE,aAAaA,CAAwCC,OAAU,CAAK,CAC3E,MAAO,CAAAA,OAAO,CAChB,CAEA,KAAM,CAAEC,YAAY,CAAEC,mBAAoB,CAAC,CAAGd,IAAI,CAElD,OACEI,SAAS,CACTM,gBAAgB,CAChBC,aAAa,CACbV,KAAK,CACLC,SAAS,CACTW,YAAY,CACZC,mBAAmB,CACnBX,0BAA0B","ignoreList":[]}
|
|
@@ -0,0 +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,uDAAuD;AACvD,wBAAgB,0BAA0B,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,MAAM,CAgEpF"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { parse, safeParse } from "./parser.js";
|
|
2
|
+
import { generateAutocompleteScript } from "./autocomplete.js";
|
|
2
3
|
import type { ActionFn, CheckDuplicatedOptions, Cli, Option, Prettify, Subcommand } from "./types.js";
|
|
3
|
-
declare function createSubcommand<const T extends Subcommand>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>>;
|
|
4
4
|
declare function createCli<const T extends Cli>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>>;
|
|
5
|
+
declare function createSubcommand<const T extends Subcommand>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>>;
|
|
5
6
|
declare function createOptions<const T extends [Option, ...Option[]]>(options: T): T;
|
|
6
7
|
declare const printCliHelp: (params: [Cli, ...Subcommand[]], printOptions?: import("./types.js").PrintHelpOpt) => void, printSubcommandHelp: (subcommand: Subcommand, printOptions?: import("./types.js").PrintHelpOpt, cliName?: string) => void;
|
|
7
|
-
export { createCli, createSubcommand, createOptions, parse, printCliHelp, printSubcommandHelp,
|
|
8
|
+
export { createCli, createSubcommand, createOptions, parse, safeParse, printCliHelp, printSubcommandHelp, generateAutocompleteScript, };
|
|
8
9
|
export type * from "./types.js";
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,KAAK,EACV,QAAQ,EACR,sBAAsB,EACtB,GAAG,EACH,MAAM,EACN,QAAQ,EACR,UAAU,EAEX,MAAM,YAAY,CAAC;AAEpB,iBAAS,SAAS,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,EAAE,KAAK,EAAE,sBAAsB,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAInG;AAED,iBAAS,gBAAgB,CAAC,KAAK,CAAC,CAAC,SAAS,UAAU,EAAE,KAAK,EAAE,sBAAsB,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAIjH;AAED,iBAAS,aAAa,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAE3E;AAED,QAAA,MAAQ,YAAY,8FAAE,mBAAmB,sGAAS,CAAC;AAEnD,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,aAAa,EACb,KAAK,EACL,SAAS,EACT,YAAY,EACZ,mBAAmB,EACnB,0BAA0B,GAC3B,CAAC;AAEF,mBAAmB,YAAY,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { transformOptionToArg } from "./utils.js";
|
|
2
|
+
|
|
3
|
+
import type { Cli, Subcommand } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** - Generate bash autocomplete script for your CLI */
|
|
6
|
+
export function generateAutocompleteScript(...params: [Cli, ...Subcommand[]]): string {
|
|
7
|
+
const [cli, ...subcommands] = params;
|
|
8
|
+
|
|
9
|
+
const commandName = cli.cliName;
|
|
10
|
+
const commands = subcommands.map(subcommand => subcommand.name);
|
|
11
|
+
const commandOptions = subcommands.reduce((acc: Record<string, string[]>, subcommand) => {
|
|
12
|
+
if (!subcommand.name || !subcommand.options?.length) return acc;
|
|
13
|
+
acc[subcommand.name] = subcommand.options.map(option => transformOptionToArg(option.name));
|
|
14
|
+
return acc;
|
|
15
|
+
}, {});
|
|
16
|
+
|
|
17
|
+
let switchCase = "";
|
|
18
|
+
for (const [key, value] of Object.entries(commandOptions)) {
|
|
19
|
+
switchCase += ` ${key}|${key[0]})\n`;
|
|
20
|
+
switchCase += ` opts="${value.join(" ")}"\n`;
|
|
21
|
+
switchCase += " ;;\n";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (cli.options?.length) {
|
|
25
|
+
switchCase += ` "-"*)\n`;
|
|
26
|
+
switchCase += ` opts="${cli.options.map(option => transformOptionToArg(option.name)).join(" ")}"\n`;
|
|
27
|
+
switchCase += " ;;\n";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return `_${commandName}_autocomplete() {
|
|
31
|
+
local cur prev commands opts subcommand used_opts filtered_opts
|
|
32
|
+
|
|
33
|
+
cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
34
|
+
prev="\${COMP_WORDS[COMP_CWORD-1]}"
|
|
35
|
+
subcommand="\${COMP_WORDS[1]}"
|
|
36
|
+
|
|
37
|
+
commands="${commands.join(" ")}"
|
|
38
|
+
|
|
39
|
+
case "$subcommand" in
|
|
40
|
+
${switchCase}
|
|
41
|
+
esac
|
|
42
|
+
|
|
43
|
+
used_opts=""
|
|
44
|
+
if [[ " \${commands[@]} " =~ " $subcommand " ]]; then
|
|
45
|
+
for word in "\${COMP_WORDS[@]:2}"; do
|
|
46
|
+
if [[ "$word" =~ ^- ]]; then
|
|
47
|
+
used_opts+=" $word"
|
|
48
|
+
fi
|
|
49
|
+
done
|
|
50
|
+
fi
|
|
51
|
+
|
|
52
|
+
if [[ -n "$opts" ]]; then
|
|
53
|
+
filtered_opts=""
|
|
54
|
+
for opt in $opts; do
|
|
55
|
+
if [[ ! " $used_opts " =~ " $opt " ]]; then
|
|
56
|
+
filtered_opts+="$opt "
|
|
57
|
+
fi
|
|
58
|
+
done
|
|
59
|
+
COMPREPLY=( $(compgen -W "$filtered_opts" -- "$cur") )
|
|
60
|
+
return
|
|
61
|
+
fi
|
|
62
|
+
|
|
63
|
+
if [[ "\${COMP_CWORD}" -eq 1 ]]; then
|
|
64
|
+
COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
|
|
65
|
+
fi
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
complete -F _${commandName}_autocomplete ${commandName}
|
|
69
|
+
`;
|
|
70
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { help } from "./help.js";
|
|
2
2
|
import { parse, safeParse } from "./parser.js";
|
|
3
|
+
import { generateAutocompleteScript } from "./autocomplete.js";
|
|
3
4
|
|
|
4
5
|
import type {
|
|
5
6
|
ActionFn,
|
|
@@ -11,13 +12,13 @@ import type {
|
|
|
11
12
|
UnSafeParseResult,
|
|
12
13
|
} from "./types.js";
|
|
13
14
|
|
|
14
|
-
function
|
|
15
|
+
function createCli<const T extends Cli>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {
|
|
15
16
|
return Object.assign(input, {
|
|
16
17
|
setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),
|
|
17
18
|
});
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
function
|
|
21
|
+
function createSubcommand<const T extends Subcommand>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {
|
|
21
22
|
return Object.assign(input, {
|
|
22
23
|
setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),
|
|
23
24
|
});
|
|
@@ -29,6 +30,15 @@ function createOptions<const T extends [Option, ...Option[]]>(options: T): T {
|
|
|
29
30
|
|
|
30
31
|
const { printCliHelp, printSubcommandHelp } = help;
|
|
31
32
|
|
|
32
|
-
export {
|
|
33
|
+
export {
|
|
34
|
+
createCli,
|
|
35
|
+
createSubcommand,
|
|
36
|
+
createOptions,
|
|
37
|
+
parse,
|
|
38
|
+
safeParse,
|
|
39
|
+
printCliHelp,
|
|
40
|
+
printSubcommandHelp,
|
|
41
|
+
generateAutocompleteScript,
|
|
42
|
+
};
|
|
33
43
|
|
|
34
44
|
export type * from "./types.js";
|