Comparing version 2.0.1 to 2.0.2
@@ -114,8 +114,17 @@ import chalk from 'chalk'; | ||
var _a = inArgs._, command = _a[0], subcommand = _a[1], rest = _a.slice(2); | ||
var outArgs = { command: command, subcommand: subcommand }; | ||
var outArgs = {}; | ||
if (command) { | ||
outArgs.command = command; | ||
} | ||
if (subcommand) { | ||
outArgs.subcommand = subcommand; | ||
} | ||
if (rest.length > 0) { | ||
outArgs.rest = rest; | ||
outArgs.args = rest; | ||
} | ||
delete inArgs._; | ||
return __assign({}, outArgs, inArgs); | ||
if (Object.keys(inArgs).length > 0) { | ||
return __assign({}, outArgs, { flags: __assign({}, inArgs) }); | ||
} | ||
return __assign({}, outArgs); | ||
}); | ||
@@ -168,22 +177,35 @@ }); | ||
var versionFunc = new versionCommand["default"](); | ||
var _a = this.args, version$$1 = _a.version, help = _a.help; | ||
var _b = this.args, command = _b.command, subcommand = _b.subcommand; | ||
var commandNames = Object.keys(this.commands); | ||
if (version$$1) { | ||
if (this.args.flags && this.args.flags.version) { | ||
versionFunc.run(); | ||
} | ||
if (help) { | ||
if (this.args.flags && this.args.flags.help) { | ||
helpFunc.run(this.options, commandNames); | ||
} | ||
if (!this.commands.hasOwnProperty(command)) { | ||
var runnable = this.findCommand(); | ||
if (runnable) { | ||
runnable.run(this.args); | ||
} | ||
else { | ||
helpFunc.run(this.options, commandNames); | ||
} | ||
else { | ||
if (subcommand) { | ||
this.commands[command].commands[subcommand].run(this.args); | ||
}; | ||
CLI.prototype.findCommand = function () { | ||
var _a = this.args, command = _a.command, subcommand = _a.subcommand; | ||
delete this.args.command; | ||
delete this.args.subcommand; | ||
var c1 = this.commands[command]; | ||
var c2 = c1 && c1.commands ? c1.commands[subcommand] : null; | ||
if (!c2) { | ||
if (this.args.args) { | ||
this.args.args.push(subcommand); | ||
} | ||
else { | ||
this.commands[command].run(this.args); | ||
this.args.args = [subcommand]; | ||
} | ||
} | ||
if (c2) { | ||
return c2; | ||
} | ||
return c1; | ||
}; | ||
@@ -190,0 +212,0 @@ CLI.Command = baseCommand["default"]; |
@@ -118,8 +118,17 @@ 'use strict'; | ||
var _a = inArgs._, command = _a[0], subcommand = _a[1], rest = _a.slice(2); | ||
var outArgs = { command: command, subcommand: subcommand }; | ||
var outArgs = {}; | ||
if (command) { | ||
outArgs.command = command; | ||
} | ||
if (subcommand) { | ||
outArgs.subcommand = subcommand; | ||
} | ||
if (rest.length > 0) { | ||
outArgs.rest = rest; | ||
outArgs.args = rest; | ||
} | ||
delete inArgs._; | ||
return __assign({}, outArgs, inArgs); | ||
if (Object.keys(inArgs).length > 0) { | ||
return __assign({}, outArgs, { flags: __assign({}, inArgs) }); | ||
} | ||
return __assign({}, outArgs); | ||
}); | ||
@@ -172,22 +181,35 @@ }); | ||
var versionFunc = new versionCommand["default"](); | ||
var _a = this.args, version$$1 = _a.version, help = _a.help; | ||
var _b = this.args, command = _b.command, subcommand = _b.subcommand; | ||
var commandNames = Object.keys(this.commands); | ||
if (version$$1) { | ||
if (this.args.flags && this.args.flags.version) { | ||
versionFunc.run(); | ||
} | ||
if (help) { | ||
if (this.args.flags && this.args.flags.help) { | ||
helpFunc.run(this.options, commandNames); | ||
} | ||
if (!this.commands.hasOwnProperty(command)) { | ||
var runnable = this.findCommand(); | ||
if (runnable) { | ||
runnable.run(this.args); | ||
} | ||
else { | ||
helpFunc.run(this.options, commandNames); | ||
} | ||
else { | ||
if (subcommand) { | ||
this.commands[command].commands[subcommand].run(this.args); | ||
}; | ||
CLI.prototype.findCommand = function () { | ||
var _a = this.args, command = _a.command, subcommand = _a.subcommand; | ||
delete this.args.command; | ||
delete this.args.subcommand; | ||
var c1 = this.commands[command]; | ||
var c2 = c1 && c1.commands ? c1.commands[subcommand] : null; | ||
if (!c2) { | ||
if (this.args.args) { | ||
this.args.args.push(subcommand); | ||
} | ||
else { | ||
this.commands[command].run(this.args); | ||
this.args.args = [subcommand]; | ||
} | ||
} | ||
if (c2) { | ||
return c2; | ||
} | ||
return c1; | ||
}; | ||
@@ -194,0 +216,0 @@ CLI.Command = baseCommand["default"]; |
@@ -13,2 +13,3 @@ import baseCommand from './baseCommand'; | ||
run(): void; | ||
private findCommand(); | ||
} |
/// <reference types="minimist" /> | ||
export interface CommandArgs { | ||
command: string; | ||
subcommand: string; | ||
export interface Flags { | ||
[argName: string]: string | boolean | string[]; | ||
} | ||
export interface CommandArgs { | ||
command?: string; | ||
subcommand?: string; | ||
args?: string[]; | ||
flags?: Flags; | ||
} | ||
declare const _default: () => { | ||
[x: string]: any; | ||
'--'?: string[]; | ||
_: string[]; | ||
command: string; | ||
subcommand: string; | ||
flags: { | ||
[x: string]: any; | ||
'--'?: string[]; | ||
_: string[]; | ||
}; | ||
command?: string; | ||
subcommand?: string; | ||
args?: string[]; | ||
} | { | ||
command?: string; | ||
subcommand?: string; | ||
args?: string[]; | ||
flags?: Flags; | ||
}; | ||
export default _default; |
@@ -1,4 +0,2 @@ | ||
export interface CommandArgs { | ||
[argName: string]: string | boolean | string[]; | ||
} | ||
import { CommandArgs } from './parseArgs'; | ||
export interface Commands { | ||
@@ -5,0 +3,0 @@ [commandName: string]: Command; |
{ | ||
"name": "storbi", | ||
"description": "An opinionated CLI application framework", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/index.es.js", |
@@ -73,3 +73,3 @@ # storbi | ||
```javascript | ||
{ command: '', subcommand: '', rest: ['', ''], flag: value } | ||
{ args: ['one', 'two'], flags: { flag: value }} | ||
``` | ||
@@ -81,9 +81,9 @@ | ||
$> node index.js command --force | ||
# { command: command, force: true } | ||
# { flags: { force: true } } | ||
$> node index.js config init -r=20 | ||
# { command: 'config', subcommand: 'init', r: '20' } | ||
# { flags: { r: '20' } } | ||
$> node index.js new test.js | ||
# { command: 'new', rest: ['test.js'] } | ||
# { args: ['test.js'] } | ||
``` | ||
@@ -90,0 +90,0 @@ |
@@ -62,2 +62,11 @@ /* tslint:disable */ | ||
it('calls the command with correct args if second arg is not subcommand', () => { | ||
process.argv = ['', '', 'command', 'file.js'] | ||
const cli = new CLI(Command1) | ||
cli.run() | ||
expect(cli.commands.command.run).toHaveBeenCalledWith({ | ||
args: ['file.js'], | ||
}) | ||
}) | ||
it('calls default help if cmd not found', () => { | ||
@@ -64,0 +73,0 @@ process.argv = ['', '', 'foo'] |
@@ -22,22 +22,36 @@ "use strict"; | ||
var versionFunc = new versionCommand_1["default"](); | ||
var _a = this.args, version = _a.version, help = _a.help; | ||
var _b = this.args, command = _b.command, subcommand = _b.subcommand; | ||
var commandNames = Object.keys(this.commands); | ||
if (version) { | ||
if (this.args.flags && this.args.flags.version) { | ||
versionFunc.run(); | ||
} | ||
if (help) { | ||
if (this.args.flags && this.args.flags.help) { | ||
helpFunc.run(this.options, commandNames); | ||
} | ||
if (!this.commands.hasOwnProperty(command)) { | ||
var runnable = this.findCommand(); | ||
if (runnable) { | ||
runnable.run(this.args); | ||
} | ||
else { | ||
helpFunc.run(this.options, commandNames); | ||
} | ||
else { | ||
if (subcommand) { | ||
this.commands[command].commands[subcommand].run(this.args); | ||
}; | ||
CLI.prototype.findCommand = function () { | ||
var _a = this.args, command = _a.command, subcommand = _a.subcommand; | ||
delete this.args.command; | ||
delete this.args.subcommand; | ||
var c1 = this.commands[command]; | ||
var c2 = c1 && c1.commands ? c1.commands[subcommand] : null; | ||
if (!c2) { | ||
if (this.args.args) { | ||
; | ||
this.args.args.push(subcommand); | ||
} | ||
else { | ||
this.commands[command].run(this.args); | ||
this.args.args = [subcommand]; | ||
} | ||
} | ||
if (c2) { | ||
return c2; | ||
} | ||
return c1; | ||
}; | ||
@@ -44,0 +58,0 @@ CLI.Command = baseCommand_1["default"]; |
@@ -37,24 +37,43 @@ import baseCommand from './baseCommand' | ||
const versionFunc = new Version() | ||
const { version, help } = this.args | ||
const { command, subcommand } = this.args | ||
const commandNames = Object.keys(this.commands) | ||
if (version) { | ||
if (this.args.flags && this.args.flags.version) { | ||
versionFunc.run() | ||
} | ||
if (help) { | ||
if (this.args.flags && this.args.flags.help) { | ||
helpFunc.run(this.options, commandNames) | ||
} | ||
if (!this.commands.hasOwnProperty(command)) { | ||
const runnable = this.findCommand() | ||
if (runnable) { | ||
runnable.run(this.args) | ||
} else { | ||
helpFunc.run(this.options, commandNames) | ||
} else { | ||
if (subcommand) { | ||
this.commands[command].commands[subcommand].run(this.args) | ||
} | ||
} | ||
private findCommand(): Command { | ||
const { command, subcommand } = this.args | ||
delete this.args.command | ||
delete this.args.subcommand | ||
const c1 = this.commands[command] | ||
const c2 = c1 && c1.commands ? c1.commands[subcommand] : null | ||
if (!c2) { | ||
if (this.args.args) { | ||
;(this.args.args as string[]).push(subcommand) | ||
} else { | ||
this.commands[command].run(this.args) | ||
this.args.args = [subcommand] | ||
} | ||
} | ||
if (c2) { | ||
return c2 | ||
} | ||
return c1 | ||
} | ||
} |
@@ -17,7 +17,7 @@ import parseArgs from '../parseArgs' | ||
it('puts anything else in rest', () => { | ||
it('puts anything else in args', () => { | ||
process.argv = ['', '', 'foo', 'bar', 'baz', 'qux'] | ||
expect(parseArgs()).toEqual({ | ||
command: 'foo', | ||
rest: ['baz', 'qux'], | ||
args: ['baz', 'qux'], | ||
subcommand: 'bar', | ||
@@ -31,3 +31,3 @@ }) | ||
command: 'foo', | ||
i: true, | ||
flags: { i: true }, | ||
}) | ||
@@ -40,4 +40,6 @@ }) | ||
command: 'foo', | ||
d: true, | ||
i: true, | ||
flags: { | ||
d: true, | ||
i: true, | ||
}, | ||
}) | ||
@@ -50,3 +52,3 @@ }) | ||
command: 'foo', | ||
f: 'file.js', | ||
flags: { f: 'file.js' }, | ||
}) | ||
@@ -59,3 +61,3 @@ }) | ||
command: 'foo', | ||
force: true, | ||
flags: { force: true }, | ||
}) | ||
@@ -68,3 +70,3 @@ }) | ||
command: 'foo', | ||
file: 'file.js', | ||
flags: { file: 'file.js' }, | ||
}) | ||
@@ -77,6 +79,8 @@ }) | ||
command: 'foo', | ||
force: true, | ||
i: true, | ||
flags: { | ||
force: true, | ||
i: true, | ||
}, | ||
}) | ||
}) | ||
}) |
import * as minimist from 'minimist' | ||
export interface CommandArgs { | ||
command: string | ||
subcommand: string | ||
export interface Flags { | ||
[argName: string]: string | boolean | string[] | ||
} | ||
export interface CommandArgs { | ||
command?: string | ||
subcommand?: string | ||
args?: string[] | ||
flags?: Flags | ||
} | ||
export default () => { | ||
@@ -13,6 +18,14 @@ const inArgs = minimist(process.argv.slice(2)) | ||
const outArgs: CommandArgs = { command, subcommand } | ||
const outArgs: CommandArgs = {} | ||
if (command) { | ||
outArgs.command = command | ||
} | ||
if (subcommand) { | ||
outArgs.subcommand = subcommand | ||
} | ||
if (rest.length > 0) { | ||
outArgs.rest = rest | ||
outArgs.args = rest | ||
} | ||
@@ -22,3 +35,7 @@ | ||
return { ...outArgs, ...inArgs } | ||
if (Object.keys(inArgs).length > 0) { | ||
return { ...outArgs, flags: { ...inArgs } } | ||
} | ||
return { ...outArgs } | ||
} |
@@ -1,4 +0,2 @@ | ||
export interface CommandArgs { | ||
[argName: string]: string | boolean | string[] | ||
} | ||
import { CommandArgs } from './parseArgs' | ||
@@ -5,0 +3,0 @@ export interface Commands { |
233246
1153