Skip to content

Commit

Permalink
Basics of a flag parser - flag.js
Browse files Browse the repository at this point in the history
This is set up to support and require certain args per flag, so I'll be makin an embed command with fields support to demonstrate this later
  • Loading branch information
Crenshaw committed Mar 14, 2021
1 parent 42db699 commit e06e337
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions commands/Owner/flagparse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { MessageEmbed } = require("discord.js");
const { flagParse } = require("../../config/flagParser.js");

module.exports = {
name: "flagparse",
description: "Evaluate entered code",
usage: "invite",
groups: ["utilites"],
DM: true,
cooldown: {type: "map", time: 0},
aliases: ["fparse"],
run: async (client, message, args) => {

let parse = await flagParse([
{name: "good", args: [String]},
{name: "bad", args: [String]}
], args.join(" "));

const embed = new MessageEmbed()
.setTitle('Flag Parse')
.setDescription(`noFlags: ${parse.get("noFlag")}\nGood: ${parse.get("good")}\nbad: ${parse.get("bad")}\n`)
.setColor(0x4B0082);
message.reply(embed);
}
}
34 changes: 34 additions & 0 deletions config/flagParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
exports.flagParse = async (flags, string) => {
// find all groups
let located = string.match(/("[^"]*")|([^"\s]*)/ig).filter(str => str !== '');
const map = new Map();
let currentFlag = "";
console.log(located);
// combine two that are next to eachother
for (let current of located) {
// make and set currentFlag
if (current.match(/^-/ig)) {
for (let flag of flags) {
if (flag.name == current.slice(1)) {
map.set(flag.name, []);
currentFlag = current.slice(1);
}
};
// add to an exsisting flag
} else if (currentFlag) {
map.get(currentFlag).push(current)
// There was no flag defiend to add to
} else {
if (!map.get("noFlag")) {
map.set("noFlag", [current]);
currentFlag = "noFlag"
continue
}
map.get("noFlag").push(current);
}
}


console.log(map);
return map;
}

0 comments on commit e06e337

Please sign in to comment.