Skip to content

Commit

Permalink
fix: add exceptions
Browse files Browse the repository at this point in the history
- Added handling of errors

Closes #32
  • Loading branch information
ifedchankau authored and itekaf committed Jul 17, 2018
1 parent 83aadbb commit 69da387
Showing 1 changed file with 84 additions and 51 deletions.
135 changes: 84 additions & 51 deletions src/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,110 @@ const argumentTemplate = {

const handle = (help) => {
const context = {
section: {
options: "Options:",
usage: "Usage:",
examples: "Examples:"
},
section: [
{
name: "Options:",
data: [],
required: true,
func: parseOptions
},
{
name: "Usage:",
data: [],
required: false,
func: parseUsage
},
{
name: "Examples:",
data: [],
required: false,
func: parseExamples
},
],
options: [],
delimiter: "",
delimiter: ""
};

parseSection(context.section.options, help).forEach(section => {
try {
context.section.forEach(section => {
section.data = parseSection(section.name, help);
if (section.data) {
section.data.forEach(data => section.func(data, context));
} else if (section.required) {
throw `Required section ${section.name} not found`;
}
});
return context;
} catch (error) {
throw error;
}
};

const parseOptions = (section, context) => {
try {
splitSection(section).forEach(option => {
if (option.indexOf("-") === 0) {
context.options.push(parseOption(option));
let argument = Object.assign({}, argumentTemplate);

option.trim().split(/\s\s+/, 2).map((section, index) => {
index === 0 ? setArgument(section, argument) : setDescription(section, argument);
});
context.options.push(argument);
}
});
});
} catch (error) {
throw error;
}
};

parseSection(context.section.usage, help).forEach(section => {
let argument = Object.assign({}, argumentTemplate);
argument.longName = "";
section.match(/file|path/i) ? context.options.push(argument) : "";
});
const parseUsage = (section, context) => {
let argument = Object.assign({}, argumentTemplate);
argument.longName = "";
context.options.push(section.match(/file|path/i) ? argument : {});
};

parseSection(context.section.examples, help).forEach(section => {
const defaultDelimiter = "=";
const delimitersTemplate = [" ", "="];
delimitersTemplate.forEach(delimiter => {
const regularExp = new RegExp(`-[^ \t\n]+${delimiter}[^ \t\n-]`);
section.match(regularExp) ? context.delimiter = delimiter : "";
});
context.delimiter === "" ? context.delimiter = defaultDelimiter : "";
const parseExamples = (section, context) => {
const delimiterDefault = "=";
const delimitersTemplate = [" ", "="];
delimitersTemplate.forEach(delimiter => {
const regularExp = new RegExp(`-[^ \t\n]+${delimiter}[^ \t\n-]`);
section.match(regularExp) ? context.delimiter = delimiter : "";
});

return context;
context.delimiter = context.delimiter ? context.delimiter : delimiterDefault;
};

const parseSection = (sectionName, help) => {
let regularExp = new RegExp(`[^\n]*${sectionName}[^\n]*\n?(?:[ \t].*?(?:\n|$))*`);
let matches = help.match(regularExp);

return matches.map(match => match.trim());
try {
const regularExp = new RegExp(`[^\n]*${sectionName}[^\n]*\n?(?:[ \t].*?(?:\n|$))*`);
const matches = help.match(regularExp);
return matches ? matches.map(match => match.trim()) : [];
} catch (error) {
throw error;
}
};

const splitSection = (section) => {
let split = [];
let splitTmp = section.split("\n").slice(1);
try {
let result = [];
const strings = section.split("\n").slice(1);

let j = 0;
let j = 0;

splitTmp.forEach(splitTmp => {
splitTmp = splitTmp.trim();

if (splitTmp.indexOf("-") === 0) {
split.push(splitTmp);
j++;
} else if (j > 0) {
split[j - 1] = split[j - 1] + splitTmp;
}
});

return split;
};
strings.forEach(str => {
str = str.trim();

const parseOption = (option) => {
let argument = Object.assign({}, argumentTemplate);

option.trim().split(/\s\s+/, 2).map((section, index) => {
index === 0 ? setArgument(section, argument) : setDescription(section, argument);
});
if (str.indexOf("-") === 0) {
result.push(str);
j++;
} else if (j > 0) {
result[j - 1] = result[j - 1] + str;
}
});

return argument;
return result;
} catch (error) {
throw error;
}
};

const setArgument = (section, argument) => {
Expand Down

0 comments on commit 69da387

Please sign in to comment.