Skip to content

Commit

Permalink
feat: add more enum patterns
Browse files Browse the repository at this point in the history
- Add more enum patterns
- Delete unnecessary values (path, file, etc.) from parsing as enum

Closes #142
  • Loading branch information
ifedchankau committed Aug 15, 2018
1 parent 3f9c9dc commit edcf53c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
23 changes: 16 additions & 7 deletions src/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,20 @@ const getDefaultValue = (description, regexp) => {
*/
const getEnum = (string, context) => {
const result = getValueByRegexp(string, context.regexp.enumValues.enum);
return result ? result[1].split(context.regexp.enumValues.split) : null;
return result ? removeExtraEnumValues(
result[2].split(context.regexp.enumValues.split), context) : null;
};

/**
* Remove extra enum values (such as 'file', 'path', 'folder', etc.)
* @param {array} enumArray - array with enum values
* @param {object} context - internal config
* @return {array} enumArray - enum without extra values or null if it empty
*/
const removeExtraEnumValues = (enumArray, context) => {
const result = enumArray.filter((value) =>
value.toLowerCase().match(context.regexp.path) === null);
return result.length === 0 ? null : result;
};

/**
Expand Down Expand Up @@ -121,9 +134,7 @@ const getValueByRegexp = (string, regexp) => {
* @return {string} - result string
*/
const removeExtraCharacters = (string) => {
string = string.replace(/=/g, ' ');
string = string.replace(/,/g, ' ');
return string;
return string.replace(/=/g, ' ').replace(/,/g, ' ');
};

/**
Expand All @@ -132,9 +143,7 @@ const removeExtraCharacters = (string) => {
* @return {string} - result string
*/
const removeExtraSpaces = (string) => {
string = string.replace(/\t/g, ' ');
string = string.replace(/[\s]+/g, ' ');
return string;
return string.replace(/\t/g, ' ').replace(/[\s]+/g, ' ');
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const options = (section, context) => {
* @param {object} context - internal config
*/
const usage = (section, context) => {
const result = arguments.getValueByRegexp(section, context.regexp.filePath);
const result = arguments.getValueByRegexp(section, context.regexp.path);
if (result) {
let argument = context.get.template.argument();
argument.longName = '';
Expand Down
7 changes: 4 additions & 3 deletions src/template/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ const context = {
start: '\\s[\\s]+',
end: '[(\n|\r\n)]+(\n|\r\n)?(?:[ \t].*?(?:(\n|\r\n)|$))*',
},
filePath: 'file|path',
path: 'file|path|folder|dir|directory',
delimiter: '-[^ \t(\n|\r\n)]+(\\s|=)[^ \t(\n|\r\n)-]',
enumValues: {
enum: '<(([\\S]+\\|)+[\\S]+)>',
split: '|',
enum: '(<|\\(|\\")(([\\S]+(\\||\\",\\s\\"|\\sor\\s))+[\\S]+)' +
'(>|\\)|\\"|,\\s)',
split: /[\|]|\",\s\"|\"\sor\s\"/,
},
argument: {
short: '(\\s|^)-[^-]*?(\\s|=|$)',
Expand Down

0 comments on commit edcf53c

Please sign in to comment.