- Source:
The Command-Prompt Interfaces for WSH (Windows Script Host). Similar to Commander.js.
Requires
Methods
(static) _getCommandObjects() → {Array.<typeProgramObj>}
- Source:
Gets the inner objects defined with Wsh.Commander.addProgram.
Example
var cmd = Wsh.Commander;
cmd.addProgram({
// The program schema
});
if (debug) console.dir(cmd._getCommandObjects());
cmd.parse(process.argv);
Returns:
- The inner objects defined with .addProgram().
- Type
- Array.<typeProgramObj>
(static) addProgram(schemaObj) → {void}
- Source:
Defines the program. Similar to Commander.js Options
Examples
var cmd = Wsh.Commander;
// Ex. Command and Action
cmd.addProgram({
command: 'play <consoleName> [gameTitle]',
action: function (consoleName, gameTitle) {
if (typeof gameTitle === 'string') {
console.log('play ' + gameTitle + ' on ' + consoleName);
} else {
console.log('play ' + consoleName);
}
}
});
cmd.parse(process.argv);
// D:\>cscript Run.wsf
// ERROR: Set a command.
// where <command> is one of:
// play
// D:\>cscript Run.wsf play
// error: missing required argument "consoleName"
// Usage: play <consoleName> [gameTitle]
//
// Options:
// -h, --help Output usage information
// D:\>cscript Run.wsf play "PC-Engine"
// play PC-Engine
// Ex. Any-arguments
cmd.addProgram({
command: 'createZip <srcDir> <destDir> [excludes...]',
action: function (srcDir, destDir, excludes) {
if (excludes) {
return 'srcDir: "' + srcDir + '", destDir: "' + destDir + '", '
+ 'excludes: ' + excludes.join(', ');
}
return 'srcDir: "' + srcDir + '", destDir: "' + destDir + '"';
}
});
var str = cmd.parse(process.argv);
console.log(str);
// D:\>cscript Run.wsf createZip C:\\Users D:\\BackUp tmp temp cache
// srcDir: "C:\\Users", destDir: "D:\\BackUp", excludes: tmp,temp,cache
// Ex. Commands
cmd.addProgram({
command: 'play',
action: function () { console.log('Your command is `play`'); }
});
cmd.addProgram({
command: 'study',
action: function () { console.log('Your command is `study`'); }
});
cmd.parse(process.argv);
// D:\>cscript Run.wsf study
// Your command is `study`
// Ex. Option Switch
cmd.addProgram({
options: [
['-C, --close1', 'Normally opened switch'], // default: false
['-O, --no-close2', 'Normally closed switch'] // default: true
]
});
cmd.parse(process.argv);
// `D:\>cscript Run.wsf`
cmd.opt.close1; // false
cmd.opt.close2; // true
// `D:\>cscript Run.wsf -O -C`
// or `D:\>cscript Run.wsf --close1 --no-close2`
cmd.opt.close1; // true
cmd.opt.close2; // false
// Ex. Option Flag value
cmd.addProgram({
options: [
['-f, --flag [name]', 'Flag name'], // default: undefined
['-d, --flag-def [name]', 'Flag name(default: "My Name")', 'My Name'],
]
});
cmd.parse(process.argv);
// `D:\>cscript Run.wsf`
cmd.opt.flag; // undefined
cmd.opt.flagDef; // 'My Name'
// `D:\>cscript Run.wsf -f -d`
// or `D:\>cscript Run.wsf --flag --flag-def`
cmd.opt.flag; // true
cmd.opt.flagDef; // 'My Name'
// `D:\>cscript Run.wsf -f "Flag A" -d "Flag B"`
// or `D:\>cscript Run.wsf --flag "Flag A" --flag-def "Flag B"`
cmd.opt.flag; // 'Flag A'
cmd.opt.flagDef; // 'Flag B'
// Ex. Option Pair with a value
cmd.addProgram({
options: [
['-r, --required <Foo>', 'Empty <Foo> is not allowed'],
['-R, --def-word <Bar>', 'default value is "Def Name"', 'Def Name']
]
});
cmd.parse(process.argv); // No Error
// `D:\>cscript Run.wsf`
cmd.opt.required; // undefined
cmd.opt.defWord; // 'Def Name'
// `D:\>cscript Run.wsf -r`
// or `D:\>cscript Run.wsf --required`
// Outputs the help
// `D:\>cscript Run.wsf -r "Val A"`
// or `D:\>cscript Run.wsf --required "Val A"`
cmd.opt.required; // 'Val A'
// Ex. Option Array values
cmd.addProgram({
options: [
['-f, --flags [name...]', 'Flag name'],
['-v, --values <val...>', 'Specify your value', 'Val 0']
]
});
cmd.parse(process.argv);
// `D:\>cscript Run.wsf`
cmd.opt.flags; // []
cmd.opt.values; // ['Val 0']
// `D:\>cscript Run.wsf -f "Name 0" -v "Val 1"`
// or `D:\>cscript Run.wsf --flags "Name 0" --values "Val 1"`
cmd.opt.flags; // ['Name 0']
cmd.opt.values; // ['Val 0', 'Val 1']
// `D:\>cscript Run.wsf -v "Val 1" "Val 2" "Val 3"`
cmd.opt.values; // ['Val 0', 'Val 1', 'Val 2', 'Val 3']
Parameters:
| Name | Type | Description |
|---|---|---|
schemaObj |
typeCommanderSchema | A schema of the program. |
Returns:
- Type
- void
(static) addPrograms(schemaObjs) → {void}
- Source:
Defines the programs.
Parameters:
| Name | Type | Description |
|---|---|---|
schemaObjs |
Array.<typeCommanderSchema> | Schemas of the programs. |
Returns:
- Type
- void
(static) clearPrograms() → {void}
- Source:
Clears the programs.
Example
var cmd = Wsh.Commander;
cmd.addProgram({
// The program schema
});
if (debug) {
cmd.clearPrograms();
cmd.addProgram({
// The program schema to debug
});
}
cmd.parse(process.argv);
Returns:
- Type
- void
(static) help(callbackopt) → {void}
- Source:
Shows the help of programs and exit with returning 1.
Example
var cmd = Wsh.Commander;
cmd.addProgram({
options: [
['-c, --console-name <name>', 'The game console name'],
['-G, --game-title [title]', 'The game name']
]
});
cmd.parse(process.argv);
if (cmd.opt.consoleName === 'DUO') cmd.help();
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
callback |
function |
<optional> |
A Function to run before showing help. |
Returns:
- Type
- void
(static) parse(processArgv) → {void}
- Source:
Parses the arguments of a WSH script with the program schemas.
Example
var cmd = Wsh.Commander;
cmd.addProgram({
options: [
['-c, --console-name <name>', 'The game console name'],
['-G, --game-title [title]', 'The game name']
]
});
cmd.parse(process.argv);
// `cscript .\Run.wsf -c "SEGA Saturn" -G "StreetFighter ZERO"`
console.log(cmd.opt.consoleName); // 'SEGA Saturn'
console.log(cmd.opt.gameTitle); // 'StreetFighter ZERO'
Parameters:
| Name | Type | Description |
|---|---|---|
processArgv |
Array.<string> | The arguments of WSH (wscript/cscript). |
Returns:
- Type
- void