Last active
December 16, 2019 18:47
-
-
Save sbrl/82afea7e3a253c45ca502d90b682bdad to your computer and use it in GitHub Desktop.
[CLI.mjs] A CLI template for Node.js #template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Requires Ansi.mjs, which can be found here: https://gist.github.com/8c0bb5e172438b6e62dd48587cfeba84#file-ansi-mjs | |
import a from './Ansi.mjs'; | |
// 1: Setup | |
const settings = { | |
program_name: "", | |
version: "v0.1", | |
description: "" | |
}; | |
// 2: CLI Argument Parsing | |
let args = process.argv.slice(2); // Slice out the node binary name and the filename | |
let extras = []; | |
for(let i = 0; i < args.length; i++) { | |
if(!args[i].startsWith("-")) { | |
extras.push(args[i]); | |
continue; | |
} | |
switch(args[i]) { | |
case "--help": | |
case "-h": | |
console.log(`${settings.program_name}, ${settings.version} | |
${a.locol}By Starbeamrainbowlabs${a.reset} | |
${a.hicol}This program ${settings.description}.${a.reset} | |
${a.fblue}${a.hicol}Usage:${a.reset} | |
node ${process.argv[1]} | |
${a.fblue}${a.hicol}Options:${a.reset} | |
${a.fyellow}-h --help ${a.reset}Show this message | |
${a.fyellow}-v --version ${a.reset}Show the version of this program | |
${a.fblue}${a.hicol}Environment Variables:${a.reset} | |
(none yet) | |
`); | |
process.exit(); | |
break; | |
case "--version": | |
case "-v": | |
console.log(settings.version); | |
break; | |
// Add more arguments here | |
} | |
} | |
// 3: Environment Variable Parsing | |
// process.env.XYZ | |
// 4: Run | |
run(settings); | |
// 5: Cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment