Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ The `@basd/cli` module is designed to be intuitive for developers familiar with

The `commander` module in `@basd/cli` is used for building command-line interfaces with support for commands, options, and sub-commands.

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:

For simpler CLI applications, where everything fits easily in one file:

```js
const { program } = require('@basd/cli')

Expand All @@ -54,6 +58,41 @@ program
program.parse(process.argv)
```

For more complex application, where you have the need to spread logic out across multiple files:

```js
// main.js
const { Command } = require('@basd/cli')
const program = new Command()

// Attach subcommand
const { addServeCommand } = require("./serve")
addServeCommand(program);

// Parse command-line arguments
program.parse(process.argv)
```

```js
// serve.js
function addServeCommand(program) {
// Define a new command with options
return program
.command('serve [port]')
.description('Start the server')
.option('-d, --debug', 'output extra debugging')
.action((port, options) => {
const portNumber = port || 3000
console.log(`Server running on port ${portNumber}`)
if (options.debug) console.log('Debugging mode is on')
})
}

module.exports = {
addServeCommand
}
```

### ShellJS

`ShellJS` is integrated for executing shell commands in a Unix shell-style, but within your Node.js scripts.
Expand Down
6 changes: 3 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const colors = require('cli-color')
* @link https://npmjs.com/package/commander
* @type {Object}
*/
const Commander = require('commander')
const { program, Command } = require('commander')


/**
Expand All @@ -53,8 +53,8 @@ const { Spinner, spinner, Progress } = require('@basd/spinner')
// Module Augmentation

// Assigning imported modules to the cli object for unified export.
cli.Commander = Commander
cli.program = Commander.program // Export Commander.js singleton instance
cli.Command = Command
cli.program = program
cli.Progress = Progress
cli.Spinner = Spinner
cli.spinner = spinner
Expand Down