Gathering detailed insights and metrics for yargs-command-wrapper
Gathering detailed insights and metrics for yargs-command-wrapper
npm install yargs-command-wrapper
Typescript
Module System
Node Version
NPM Version
72.1
Supply Chain
99
Quality
77.6
Maintenance
100
Vulnerability
100
License
TypeScript (99.58%)
JavaScript (0.26%)
Shell (0.16%)
Total Downloads
978
Last Day
1
Last Week
6
Last Month
32
Last Year
420
1 Stars
116 Commits
1 Watching
3 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.7-1
Package Id
yargs-command-wrapper@1.0.7-1
Unpacked Size
114.57 kB
Size
23.37 kB
File Count
118
NPM Version
10.8.2
Node Version
20.10.0
Publised On
24 Sept 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-50%
6
Compared to previous week
Last month
88.2%
32
Compared to previous month
Last year
-24.7%
420
Compared to previous year
2
This wrapper enables the parsing of commands and subcommands into a unified set of types that can be handled independently. Additionally, it simplifies the process of creating a handler for these commands
The inferred union type of the parsed arguments is as follows:
1type ParsedArgs = 2| { command: "server"; subcommand: "start"; argv: { port: number; } } 3| { command: "client"; subcommand: "connect"; argv: { host: string; port: number; } } 4| { command: "client"; subcommand: "config"; subsubcommand: "get"; argv: { key?: string; file: string; } } 5| { command: "client"; subcommand: "config"; subsubcommand: "set"; argv: { key: string; value: string; file: string; } }
1npm install --save yargs-command-wrapper
API might change in the future.
https://github.com/nktknshn/yargs-command-wrapper/tree/master/examples
https://github.com/nktknshn/typescript-money-tracker/blob/master/src/cli.ts
1import { 2 buildAndParse, 3 command, 4 composeCommands, 5 createHandlerFor, 6 Either, 7 failClient, 8 subcommands, 9} from "yargs-command-wrapper"; 10 11const config = composeCommands( 12 _ => 13 _.options({ 14 file: { alias: "f", type: "string", default: "config.json" }, 15 }), 16 command( 17 ["get [key]", "g"], 18 "get config value", 19 _ => _.positional("key", { type: "string" }), 20 ), 21 command( 22 ["set <key> <value>", "s"], 23 "set config key", 24 _ => 25 _ 26 .positional("value", { type: "string", demandOption: true }) 27 .positional("key", { type: "string", demandOption: true }), 28 ), 29); 30 31const serverStart = command( 32 ["start", "sta"], 33 "start server", 34 _ => _.option("port", { type: "number", default: 8080 }), 35); 36 37const serverStop = command( 38 ["stop", "sto"], 39 "stop server", 40 _ => _.option("force", { type: "boolean", default: false }), 41); 42 43const cmd = composeCommands( 44 _ => _.option("debug", { alias: "d", type: "boolean", default: false }), 45 serverStart, 46 serverStop, 47 subcommands(["config", "c"], "config management", config), 48 // allow command without a subcommand 49).selfHandle(true); 50 51const { result, yargs } = buildAndParse(cmd, process.argv.slice(2)); 52 53if (Either.isLeft(result)) { 54 failClient(yargs, result); 55} 56 57if (result.right.argv.debug) { 58 console.log("debug mode enabled"); 59} 60 61if (result.right.command === undefined) { 62 console.log(`cli was called without any commands`); 63 yargs.showHelp(); 64} 65else if (result.right.command === "start") { 66 console.log(`starting server on port ${result.right.argv.port}`); 67} 68else if (result.right.command === "stop") { 69 console.log(`stopping server ${result.right.argv.force ? "forcefully" : ""}`); 70} 71else if (result.right.command === "config") { 72 if (result.right.subcommand === "get") { 73 console.log(`getting config key ${result.right.argv.key ?? "all"}`); 74 } 75 else { 76 console.log( 77 `setting config key ${result.right.argv.key} to ${result.right.argv.value}`, 78 ); 79 } 80} 81 82// or by using createHandlerFor: 83const handler = createHandlerFor(cmd, { 84 "$self": ({ debug }) => { 85 console.log( 86 `cli was called without any commands. debug: ${debug ? "yes" : "no"}`, 87 ); 88 yargs.showHelp(); 89 }, 90 config: { 91 get: ({ key, file, debug }) => { 92 console.log(`getting config ${file} key ${key ?? "all"}`); 93 }, 94 set: ({ key, value, file, debug }) => { 95 console.log(`setting config ${file} key ${key} to ${value}`); 96 }, 97 }, 98 start: ({ port, debug }) => { 99 console.log(`starting server on port ${port}`); 100 }, 101 stop: ({ force, debug }) => { 102 console.log(`stopping server ${force ? "forcefully" : ""}`); 103 }, 104}); 105 106handler.handle(result.right);
No vulnerabilities found.
No security vulnerabilities found.