Skip to content
Snippets Groups Projects
Commit ba0e8cac authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

command line example

parent e9105b7b
No related branches found
No related tags found
No related merge requests found
# only relevant for Mac systems
.DS_STORE
#ingore npm packages
node_modules
#!/usr/bin/env node
// require('../')()
var index = require('../index')
index()
//console.log("Another way to run a node application");
\ No newline at end of file
console.log("executing the foo command");
\ No newline at end of file
const menus = {
main: `
weather [command] <options>
today .............. show weather for today
version ............ show package version
help ............... show help menu for a command`,
today: `
weather today <options>
--location, -l ..... the location to use`,
}
module.exports = (args) => {
console.log("***********")
console.log(args._[0])
const subCmd = args._[0] === 'help' ? args._[1] : args._[0]
console.log(subCmd)
console.log(menus[subCmd] || menus.main)
}
\ No newline at end of file
module.exports = (args) => {
console.log('today is sunny')
}
const { version } = require('../package.json')
module.exports = (args) => {
console.log(`v${version}`)
}
const minimist = require('minimist')
module.exports = () => {
const args = minimist(process.argv.slice(2))
console.log("args is: ");
console.log(args);
let cmd = args._[0] || 'help'
if (args.version || args.v) {
cmd = 'version'
}
if (args.help || args.h) {
cmd = 'help'
}
console.log("------------")
console.log(cmd);
switch (cmd) {
case 'today':
require('./cmds/today')(args)
break
case 'foo':
require('./cmds/foo')
break
case 'version':
require('./cmds/version')(args)
break
case 'help':
require('./cmds/help')(args)
break
default:
console.error(`"${cmd}" is not a valid command!`)
break
}
}
{
"name": "command_line_example",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="
}
}
}
{
"name": "command_line_example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"devstart": "node bin/weather",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"bin": {
"weather": "bin/weather"
},
"dependencies": {
"minimist": "^1.2.0"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment