Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Commit

Permalink
Update test. Throw error when using reserved name.
Browse files Browse the repository at this point in the history
  • Loading branch information
CapMousse committed Mar 1, 2017
1 parent 663d663 commit e793411
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 45 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,63 +52,63 @@ cli.run();

## API

> **require('clifier')**
#### `require('clifier')`

return a `Cli` instance.

### Cli API

> **.version(string) : Cli**
#### `.version(string) : Cli`

Set the version of your cli utility

> **.name(string) : Cli**
#### `.name(string) : Cli`

Set the name of your cli utility

> **.description(string) : Cli**
#### `.description(string) : Cli`

Set the description of your cli utility

> **.write(string)**
#### `.write(string)`

Write something on the stdout

> **.style(content, style)**
#### `.style(content, style)`

Transform content with asked style. Need to be send to `write`.

> **.success(string)**
#### `.success(string)`

Write a success text on the stdout

> **.warning(string)**
#### `.warning(string)`

Write a warning text on the stdout

> **.error(string)**
#### `.error(string)`

Write an error text on the stderr

> **.log(string)**
#### `.log(string)`

Write a log text on the stdout if verbose enabled

> **.command(name, description) : Command**
#### `.command(name, description) : Command`

Create a new command with name and description.

### Command API

> **.argument(name, description, defaultValue, filter) : Command**
#### `.argument(name, description, defaultValue, filter) : Command`

Add an argument to the command.
- **name** (string) : name of the argument *(--arg, -arg)*
- **description** (string) : description of the argument
- **defaultValue** (mixed) : default value of the argument if empty
- **filter** (function) : filter function for the argument

> **.action(function) : Command**
#### `.action(function) : Command`

Action of the command when executed. Function will receive all arguments in orders as parameters.

Expand Down
17 changes: 17 additions & 0 deletions src/Argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@ class Argument {
* @param {Function} filter argument callback when called
*/
constructor (name, description, defaultValue, filter) {
this.checkNameValidity(name);

this._name = name;
this._description = description;
this._defaultValue = defaultValue;
this._filter = filter;
}


/**
* Check if argument name is valid
* @param {String} name
*/
checkNameValidity (name) {
const invalid = ['--quiet', '--verbose', '-v'];

let nameArray = name.split(',');

for (let i of nameArray) {
if (invalid.indexOf(i) != -1) throw new Error(i + " is a reserved argument");
}
}

/**
* Parse argument
* @return {mixed}
Expand Down
21 changes: 10 additions & 11 deletions src/Cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,9 @@ class Cli extends Text {
* @return {Command}
*/
command (name, description) {
var command;

if ("Command" === name.constructor.name){
command = name;
} else {
command = new Command(name, description);
}
if (name == "help") throw new Error("help is a reserved command");

let command = new Command(name, description);
this._commands[command.getName()] = command;

return command;
Expand Down Expand Up @@ -141,11 +136,15 @@ class Cli extends Text {
* Launch Cli
*/
run () {
var _this = this;
let helpCmd = new Command("help", "Show help");

this._commands[helpCmd.getName()] = helpCmd;

this.command('help', 'Show help').action(() => {
new Help(_this);
_this.end();
helpCmd.action(_ => {
let help = new Help(this);

this.write(help.getHelp());
this.end();
});

var args = Object.keys(arguments).length ? Array.prototype.slice.call(arguments) : process.argv.slice(2);
Expand Down
26 changes: 21 additions & 5 deletions src/Stdout/Help.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ class Help extends Text {
*/
constructor (cli) {
super();

this._cli = cli;
this._output = "\n";
this._length = 0;

this.setIntro();
this.setCommands();
this.setOptions();

this.write(this._output);
}


/**
* Get help intro
*/
setIntro () {
this._output += this._cli.getName();

Expand All @@ -38,6 +39,9 @@ class Help extends Text {
this._output += this.style(" [options]", "blue");
}

/**
* Get command preformated
*/
getCommands () {
var parsedCommands = [],
commands = this._cli.getCommands();
Expand Down Expand Up @@ -73,6 +77,9 @@ class Help extends Text {
return parsedCommands;
}

/**
* Get command list
*/
setCommands () {
var commands = this.getCommands();

Expand All @@ -87,14 +94,23 @@ class Help extends Text {
});
}

/**
* Get option list
*/
setOptions () {

this._output += "\n" + this.style("OPTIONS :", "bold") + " \n\n";
this._output += " " + this.style("--quiet", "green") + ' '.repeat(this._length - 8) + "\tQuiet node\n";
this._output += " " + this.style("-v, --verbose", "green") + ' '.repeat(this._length - 14) + "\tVerbose node\n";

this._output += "\n";
}

/**
* Get help output
*/
getHelp () {
return this._output;
}
}

module.exports = Help;
38 changes: 22 additions & 16 deletions test/Clifier_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cli

cli.command('trycommand', 'description')
.argument('-t, --test', 'test argument')
.argument('-v, --var', 'var argument', null, function(parse){
.argument('-j, --jar', 'var argument', null, function(parse){
return parse === "test" ? true : false;
})
.action(function(argTest, argVar){
Expand All @@ -26,8 +26,18 @@ cli.command('testSimpleTable', 'try table display')
);
});

exports.testInvalidArgument = function (test) {
test.expect(1);

test.throws(() => {
cli.command('test').argument('-v');
}, Error, "-v is a reserved argument");

test.done();
}

exports.testLog = function(test) {
test.expect(4);
test.expect(2);

var log = [];
var random;
Expand All @@ -39,14 +49,6 @@ exports.testLog = function(test) {
cli.write(random);
test.equal(log[0], random);

random = Math.random().toString(16).substring(2);
cli.error(random);
test.equal(log[1], '\u001b[1m\u001b[31m'+random+'\r\n\u001b[39m\u001b[22m');

random = Math.random().toString(16).substring(2);
cli.warning(random);
test.equal(log[2], '\u001b[1m\u001b[33m'+random+'\r\n\u001b[39m\u001b[22m');

random = Math.random().toString(16).substring(2);
test.equal(cli.style(random, 'white'), '\u001b[37m'+random+'\u001b[39m');

Expand All @@ -65,10 +67,14 @@ exports.testClifer = function(test) {
};

exports.testCommand = function(test) {
test.expect(4);
test.expect(5);

test.throws(_ => {
cli.command("help");
}, Error, "help is a reserved command");

var commands = cli.getCommands();
test.equal(Object.keys(commands).length, 2, 'should be 2.');
test.equal(Object.keys(commands).length, 3, 'should be 3.');

var command = commands[Object.keys(commands)[0]];
test.equal(command.getName(), 'trycommand', 'should be trycommand.');
Expand All @@ -94,7 +100,7 @@ exports.testArguments = function(test) {
test.equal(firstArg.getFilter(), void(0), "Should be undefined");

var secondArg = args[Object.keys(args)[1]];
test.equal(secondArg.getName(), "-v, --var", "Should be var");
test.equal(secondArg.getName(), "-j, --jar", "Should be var");
test.equal(secondArg.getDescription(), "var argument", "Should be var argument");
test.equal(secondArg.getDefaultValue(), null, "Should be null");
test.notEqual(secondArg.getFilter(), void(0), "Should be defined");
Expand All @@ -115,19 +121,19 @@ exports.testRun = function(test) {


cli.run("help");
test.equal(log[0], '\ntest v0.0.1\ntest command\n\nUsage : test [command] [options]\n\nCommand list : \ntrycommand [options]\tdescription\n -t, --test\ttest argument\n -v, --var \tvar argument\ntestSimpleTable \ttry table display\nhelp \tShow help for test\n');
test.equal(log[0], '\ntest v0.0.1\ntest command\n\n\u001b[1mUSAGE : \u001b[22m\n\n test\u001b[33m [command]\u001b[39m\u001b[34m [options]\u001b[39m\n\n\u001b[1mCOMMANDS :\u001b[22m \n\n \u001b[33mtrycommand\u001b[39m \u001b[34m-t\u001b[39m \u001b[34m-j\u001b[39m\tdescription\n \u001b[34m-t\u001b[39m \ttest argument\n \u001b[34m-j\u001b[39m \tvar argument\n \u001b[33mtestSimpleTable\u001b[39m \ttry table display\n \u001b[33mtest\u001b[39m \tfalse\n \u001b[33mhelp\u001b[39m \tShow help\n\n\u001b[1mOPTIONS :\u001b[22m \n\n \u001b[32m--quiet\u001b[39m \tQuiet node\n \u001b[32m-v, --verbose\u001b[39m \tVerbose node\n\n');

cli.run("undefined");
test.equal(log[1], 'The command undefined doesn\'t exists. Try help to get the list of available commands');

cli.run("trycommand");
test.equal(log[2], 'data : undefined, undefined');

cli.run("trycommand", "-v", "test");
cli.run("trycommand", "-j", "test");
test.equal(log[3], 'data : undefined, true');

var random = Math.random().toString(16).substring(2);
cli.run("trycommand", "-v", "test", "-t", random);
cli.run("trycommand", "-j", "test", "-t", random);
test.equal(log[4], 'data : ' + random + ', true');

process.exit = oldExit;
Expand Down

0 comments on commit e793411

Please sign in to comment.