Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@holkerveen/command-bootstrap
Advanced tools
This library implements a convenient way to jumpstart your CLI toolbox programmed in node. Based on the `symfony/console` component for PHP, you can use this library to quickly generate a CLI application containing any number of scripts, and the generated
This library implements a convenient way to jumpstart your CLI toolbox programmed in node. Based on the symfony/console
component for PHP, you can use this library to quickly generate a CLI application containing any number of scripts, and the generated application will have a comprehensive help system which adheres many CLI best practices.
To install this library, run:
npm i @holkerveen/command-bootstrap
Let's dive in by creating a traditional hello world command.
At its core, all you need to do is create a class that implements CommandInterface
and then register your command!
// /src/Hello.ts
import {CommandInterface} from "@holkerveen/command-bootstrap";
export class Hello implements CommandInterface {
description(): string {
return "Show message";
}
help(): string {
return "This command will show a message";
}
configure(): void {
// empty
}
execute(): number {
console.log(`Hello world!`);
return 0;
}
}
Now, create a base app. Its task will be to register all your commands and then invoke the bootstrap code.
// /src/cli.ts
import {Hello} from "./Hello";
import {Cli} from "@holkerveen/command-bootstrap";
const cli = new Cli();
cli.add('hello', Hello);
cli.run();
When done, you can compile (transpile) and run your command. Use any build tool you like. Example:
npx webpack
Now run the cli script:
node index.js
As you have not specified a command, it will show the list of available commands. If all is well, your commmand should be in that list as well! Congratulations :-). If you want to see the extended help message (which we defined in the description function), run:
node index.js help hello
Finally, to run your command itself:
node index.js hello
The empty configure
function is where you can define your cli arguments and options. For that, we need to include our helper and use addArgument to add an argument:
class Hello {
// ...
private helper: CommandHelper;
constructor() {
this.helper = new CommandHelper;
}
configure(): void {
this.helper.addArgument('name', true, 'Name to say hello to');
}
// ...
}
In this example, 'name'
is the handle by which we can reference the argument in our code, true means that the argument is required (use false
otherwise), and the third parameter is a short help text explaining its meaning.
Next, we can modify your console.log
function to update include the argument value in our message:
class Hello {
// ...
execute(): number {
console.log(`Hello ${this.helper.getArgumentValue('name')}`);
return 0;
}
// ...
}
The same helper can be used to include a command line option:
class Hello {
// ...
configure(): void {
this.helper.addArgument('name', true, 'Name to say hello to');
helper.addOption('shout','s', "bool", false, "Shout your hello");
}
// ...
}
In this example, 'shout' is the name of the switch, 's' is the short version, "bool" indicates that this is a boolean option, false
is the default value, and the last parameter is again a quick explanation of the option.
In the execute
function, use the getOptionValue
function to retreive its value:
class Hello {
// ...
execute(): number {
let message = `Hello ${this.helper.getArgumentValue('name')}`;
if(this.helper.getOptionValue('shout')) {
message = message.toUpperCase();
}
console.log(message);
return 0;
}
// ...
}
Finally, there is a helper function that will automatically generate usage instructions for your extended help text.
While completely optional, you could modify your help
function as follows to get proper, standardized usage instructions:
class Hello {
// ...
help(): string {
return "Show message\n\n" + this.helper.getUsage();
}
// ...
}
I would like to hear from you! Please drop me a line if this lib is to your liking, or if you have ideas to improve on it!
FAQs
This library implements a convenient way to jumpstart your CLI toolbox programmed in node. Based on the `symfony/console` component for PHP, you can use this library to quickly generate a CLI application containing any number of scripts, and the generated
We found that @holkerveen/command-bootstrap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.