You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39Lines changed: 39 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,10 @@ The `@basd/cli` module is designed to be intuitive for developers familiar with
36
36
37
37
The `commander` module in `@basd/cli` is used for building command-line interfaces with support for commands, options, and sub-commands.
38
38
39
+
Keeping inline with Commander's own recommendations for [declaring the `program` variable](https://github.com/tj/commander.js/tree/v11.1.0?tab=readme-ov-file#declaring-program-variable), this presents two ways to create an instance:
40
+
41
+
For simpler CLI applications, where everything fits easily in one file:
42
+
39
43
```js
40
44
const { program } =require('@basd/cli')
41
45
@@ -54,6 +58,41 @@ program
54
58
program.parse(process.argv)
55
59
```
56
60
61
+
For more complex application, where you have the need to spread logic out across multiple files:
62
+
63
+
```js
64
+
// main.js
65
+
const { Command } =require('@basd/cli')
66
+
constprogram=newCommand()
67
+
68
+
// Attach subcommand
69
+
const { addServeCommand } =require("./serve")
70
+
addServeCommand(program);
71
+
72
+
// Parse command-line arguments
73
+
program.parse(process.argv)
74
+
```
75
+
76
+
```js
77
+
// serve.js
78
+
functionaddServeCommand(program) {
79
+
// Define a new command with options
80
+
return program
81
+
.command('serve [port]')
82
+
.description('Start the server')
83
+
.option('-d, --debug', 'output extra debugging')
84
+
.action((port, options) => {
85
+
constportNumber= port ||3000
86
+
console.log(`Server running on port ${portNumber}`)
87
+
if (options.debug) console.log('Debugging mode is on')
88
+
})
89
+
}
90
+
91
+
module.exports= {
92
+
addServeCommand
93
+
}
94
+
```
95
+
57
96
### ShellJS
58
97
59
98
`ShellJS` is integrated for executing shell commands in a Unix shell-style, but within your Node.js scripts.
0 commit comments