Giter VIP home page Giter VIP logo

clapp's Introduction

clapp NPM version Build Status Dependency Status Coverage percentage

A tool for building command line apps that aren't necessarily built for the command line.

Average time to resolve an issue Percentage of issues still open PRs Welcome

Installation

$ npm install --save clapp

Features

  • Input isn't restricted to node's process.argv, unlike other libraries, so you may use Clapp for a node cli app, or use it for anything alse. For instance, you may use it for a discord bot, where an user message is the input, and the bot behaves like a command line app.
  • Clapp also handles command execution. When using other command parsing libraries, you'd need to manually execute the command function by evaluating the user input. Clapp will execute the command for you if the user input satisfies your needs, or let the user know the error otherwise.
  • Clapp allows you to have precise control of user input: it takes care of required arguments, default values, data types, and data validation for you.
  • It also handles documentation. Clapp takes an "if you don't document it, go fuck yourself" apporach, meaning that it will simply not work if you don't document your app. For example, if you don't declare an argument that you expect the user to pass, Clapp will ignore it and not pass it back to you. But because of that, Clapp is able to easily print documentation to the user when they pass the --help flag.
  • Clapp handles the --version flag as well.

Usage

const Clapp = require('clapp');

let app = new Clapp.App({
	name: "Test App",
	desc: "An app that does the thing",
	prefix: "-testapp",
	version: "1.0",
	onReply: function(msg, context) {
		// Clapp will use this function to
		// communicate with the end user
		console.log(msg);
	}
});

app.addCommand(new Clapp.Command({
	name: "foo",
	desc: "An example command",
	fn: function(argv, context) {
		return "Foo was executed!"
	}
}));

let userInput = "-testapp foo";

// Checks if the user input is an acceptable command
if (app.isCliSentence(userInput)) {
	app.parseInput(userInput); // logs "Foo was executed!"
}

Please check out the docs for the full instructions!

License

Apache-2.0 © Pablo Rodríguez

clapp's People

Contributors

danielruf avatar glorat avatar jjjollyjim avatar marcopadillo avatar mellamopablo avatar zeragamba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

clapp's Issues

npm install failing on node 10

There's multiple issues

  • gulp failing due to natives missing on node 10
  • nsp failing because it is deprecated
  • dependencies generally being out of date

I'll create a PR for this so the environment works before I get to look at issue #22

--help command throws error when a registered command is set to "caseSensitive: false"

issue in question is caused by the following block of code

App.js, line 213

// Find whether or not the requested command exists
var cmd = null;
var userInputCommand = argv._[0];
for (var name in this.commands) {
	var command = this.commands[name];
	var validCommandName = command.caseSensitive ? name : name.toLowerCase();
	var validUserInput = command.caseSensitive ? userInputCommand : userInputCommand.toLowerCase();
	if (validCommandName === validUserInput) {
		cmd = command;
		break;
	}	
}

When doing something like "!prefix --help" while a registered command has caseSensitive set to false, userInputCommand is undefined, as argv._ has nothing inside of it. When the loop reaches the command where the caseSensitive flag is set, it tries to do .toLowerCase() on an undefined value, causing the error.

Suggestion: Define whether a command, argument or flag is case sensitive.

Suggestion: Define whether a command, argument or flag is case sensitive.
For example I might have a command mycommand but the user always enters myCommand instead of mycommand. perhaps there could be a way of defining a command, argument or flag as case sensitive or not to avoid these problems.

Number as parameters are not passed correctly to fn

Clapp Version: 1.1.1

When you use a command with a number as parameter. Accessing the property gives you the number lowered with the amount of chars in front of it. ( Took me a while to realise what was happening here).

Codesnippet from my Discord Bot:

var Clapp = require('../modules/clapp-discord');

module.exports = new Clapp.Command({
  name: "listinfo",
  desc: "lets zupa list some info",
  fn: (argv, context) => {
    if(context.msg.author.id == '111897789654421504') {
      console.log(argv.args.guildId);
      context.zbot.listInfo(argv.args.guildId, context.msg.channel);
      //context.zbot.listInfo(context.msg.toString().replace('!zbot listinfo ', '').trim(), context.msg.channel);
    } else {
      context.zbot.printLongMessage('Only Zupa can use this command.', context.msg.channel);
    }
  },
  args: [
    {
      name: 'guildId',
      desc: 'guildId',
      type: 'string',
      required: true,
      default: ''
    }
  ],
  flags: [ ]
});

So if I call the following command:

!zbot listinfo 256158903245471754

The console.log returns me the following

256158903245471740

The number is lowered by 15 ( which is the amount of chars of '!zbot listinfo ').

Command.args ordering relying on undocumented behaviour

If I am reading the code correct, Command is correctly taking in an array of arguments that are necessarily ordered. However, the implementation then puts those args into a map in the for block of line 122 of Command.js, which is technically unordered.

During the parsing in line 258 of App.js, we call
for (let i in cmd.args) {
which iterates through the args object, thus relying on the implementation specific behaviour that for/in will "usually" return args in the order they were added but is not specified to necessarily do so.

If I've read this right, the fix would be to maintain args as an array in Command.

p.s. Many thanks for this library! The auto-documentation/auto-validation are awesome

Prefix without Space.

First of all thanks for the awesome piece of software, it's solid!

It would be great if you could place the separator as a setting ( the space ) so that a command can have a prefix "/" and the command goes immediately after, without space separating.

For example:

/command

Duplicate commands cause crash

Currently using Clapp with Discord.js when I ran into a strange issue.

  1. Say I have a command foo with a flag that takes in a string, something along the lines of --test <string>,
  2. Some user inputs ~mybotidentifier foo --test somestring1 --test somestring2.
  3. The bot crashes, with the error messages pointing me to the _convertType function in App.js in the dist folder
  4. After messing around with console.log for a bit in App.js, the error seems to be stemming from arg being an array of strings with no switch case to handle it.

Help text not working - issues with for (var x in array)

This seems to be environment specific but in a couple of environments, the unfulfilled_args help text is not being displayed properly. It seems to relate to this piece of code at https://github.com/MeLlamoPablo/clapp/blob/master/lib/App.js#L268

					for (let i in unfulfilled_args) {
						r += unfulfilled_args[i.name] + "\n";
					}

The for loop assumes that i will be an Argument but in some enviroments it is simply the into the array. E.g.

> for (var x in [10,20,30]) {console.log(x)}
0
1
2

I'm not an expert in Javascript but a quick google suggests that using for/in over an array is apparently a bad idea, presumably because of this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.