Skip to content

Commit

Permalink
Fixing Typo
Browse files Browse the repository at this point in the history
  • Loading branch information
siokas committed Mar 24, 2021
1 parent c94b363 commit c102d0f
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 40 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -154,19 +154,19 @@ program
});
```

#### Option choises
#### Option choices

You may define a list (array) of accepted choises for each option. If the user
You may define a list (array) of accepted choices for each option. If the user
enters anything that is not in this list, a validation error
(OPTION_CHOISE_ERROR) is throwned. To define accepted choises, you have to
create a custom option object and call the `choises()` method passing the array
of the accepted choises:
(OPTION_CHOICE_ERROR) is throwned. To define accepted choices, you have to
create a custom option object and call the `choices()` method passing the array
of the accepted choices:

```typescript
const fruits = new Option({
flags: "-f --fruits",
description: "Choose one of accepted choises",
}).choises(["apple", "banana", "orange"]);
description: "Choose one of accepted choices",
}).choices(["apple", "banana", "orange"]);

program
.command("choose")
Expand Down
4 changes: 2 additions & 2 deletions examples/option_processing.ts
Expand Up @@ -70,8 +70,8 @@ program.command("find").addOption(nameOption, ageOption).action(() =>

const fruits = new Option({
flags: "-f --fruits",
description: "Choose one of accepted choises",
}).choises(["apple", "banana", "orange"]);
description: "Choose one of accepted choices",
}).choices(["apple", "banana", "orange"]);

program.command("choose").addOption(fruits).action(() => {
console.log(`You choose ${program.fruits}`);
Expand Down
4 changes: 2 additions & 2 deletions src/Command.ts
Expand Up @@ -36,7 +36,7 @@ export class Command {
constructor(params: CommandParams) {
this.params = Object.assign({
description: "",
action: () => {},
action: () => { },
}, params);

this.addOption({
Expand All @@ -59,7 +59,7 @@ export class Command {
command: this,
callback: customOption._callback || undefined,
defaultValue: customOption.defaultValue || undefined,
choises: customOption.all_choises || undefined,
choices: customOption.all_choices || undefined,
};

const option: Option = new Option(optionParams);
Expand Down
6 changes: 3 additions & 3 deletions src/CustomOption.ts
Expand Up @@ -23,7 +23,7 @@ export class CustomOption {
/** Holds the default value if passed */
public defaultValue?: any;

public all_choises?: Array<any>;
public all_choices?: Array<any>;

/** Holds the short flag (-p). One letter command */
protected _letter_option?: string;
Expand Down Expand Up @@ -70,8 +70,8 @@ export class CustomOption {
return this;
}

public choises(choises: Array<any>): CustomOption {
this.all_choises = choises;
public choices(choices: Array<any>): CustomOption {
this.all_choices = choices;
return this;
}
}
2 changes: 1 addition & 1 deletion src/Executor.ts
Expand Up @@ -90,7 +90,7 @@ export class Executor {
rules: [
ValidationRules.NON_DECLEARED_ARGS,
ValidationRules.REQUIRED_OPTIONS,
ValidationRules.OPTION_CHOISES,
ValidationRules.OPTION_CHOICES,
],
throw_errors: this.throw_errors,
}).validate();
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel.ts
Expand Up @@ -78,7 +78,7 @@ export abstract class Kernel {
REQUIRED_OPTION_NOT_FOUND: "Required option is not specified!",
REQUIRED_VALUE_NOT_FOUND: "Required command value is not specified!",
TOO_MANY_PARAMS: "You have passed too many parameters",
OPTION_CHOISE: "Invalid option choise!",
OPTION_CHOICE: "Invalid option choice!",
};

/** User have the option to throw the errors. by default it is not enabled */
Expand Down
6 changes: 3 additions & 3 deletions src/Option.ts
Expand Up @@ -28,7 +28,7 @@ export class Option {
/** Holds the default value if passed */
public defaultValue?: any;

public choises?: Array<any>;
public choices?: Array<any>;

/** Holds the short flag (-p). One letter command */
protected _letter_option?: string;
Expand All @@ -53,8 +53,8 @@ export class Option {
this.value = this.defaultValue;
}

if (params.choises) {
this.choises = params.choises;
if (params.choices) {
this.choices = params.choices;
}

this.splitFlags();
Expand Down
8 changes: 4 additions & 4 deletions src/Util.ts
Expand Up @@ -71,8 +71,8 @@ export class Util {
helpText = helpText + `(default: ${option.defaultValue})`;
}

if (option.choises) {
helpText = helpText + `(choises: ${option.choises.toString()})`;
if (option.choices) {
helpText = helpText + `(choices: ${option.choices.toString()})`;
}
console.log(helpText);
});
Expand All @@ -88,8 +88,8 @@ export class Util {
helpText = helpText + `(default: ${option.defaultValue})`;
}

if (option.choises) {
helpText = helpText + `(choises: ${option.choises.toString()})`;
if (option.choices) {
helpText = helpText + `(choices: ${option.choices.toString()})`;
}
console.log(helpText);
}
Expand Down
17 changes: 8 additions & 9 deletions src/Validator.ts
Expand Up @@ -76,8 +76,8 @@ export class Validator implements ValidatorContract {
return this.validateOnCommands();
case ValidationRules.BASE_COMMAND_OPTIONS:
return this.validateBaseCommandOptions();
case ValidationRules.OPTION_CHOISES:
return this.validateOptionChoises();
case ValidationRules.OPTION_CHOICES:
return this.validateOptionChoices();
default:
return {
passed: false,
Expand Down Expand Up @@ -274,21 +274,20 @@ export class Validator implements ValidatorContract {
return result;
}

protected validateOptionChoises(): ValidationResult {
protected validateOptionChoices(): ValidationResult {
let result: ValidationResult = { passed: true };

this.app.commands.map((command: Command) => {
command.options.map((option: Option) => {
if (option.choises) {
if (option.choices) {
for (const key in this.args.options) {
if (!option.choises.includes(this.args.options[key])) {
if (!option.choices.includes(this.args.options[key])) {
result = {
passed: false,
error: new Error(this.app.errors.OPTION_CHOISE),
error: new Error(this.app.errors.OPTION_CHOICE),
command: `(${key})`,
rest: `Argument '${
this.args.options[key]
}' is invalid. Allowed choices are: ${option.choises.toString()}`,
rest: `Argument '${this.args.options[key]
}' is invalid. Allowed choices are: ${option.choices.toString()}`,
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Expand Up @@ -73,7 +73,7 @@ export type CommandOption = {
isRequired?: boolean;
callback?: Function;
defaultValue?: any;
choises?: Array<any>;
choices?: Array<any>;
};

export type OptionParameters = {
Expand All @@ -83,7 +83,7 @@ export type OptionParameters = {
isRequired?: boolean;
callback?: Function;
defaultValue?: any;
choises?: Array<any>;
choices?: Array<any>;
};

/** Defines the version setter */
Expand Down Expand Up @@ -117,7 +117,7 @@ export type DenomanderErrors = {
REQUIRED_OPTION_NOT_FOUND: string;
REQUIRED_VALUE_NOT_FOUND: string;
TOO_MANY_PARAMS: string;
OPTION_CHOISE: string;
OPTION_CHOICE: string;
};

/* Enum containing the Validation Rules */
Expand All @@ -128,5 +128,5 @@ export enum ValidationRules {
ON_COMMANDS,
ACTIONS,
BASE_COMMAND_OPTIONS,
OPTION_CHOISES,
OPTION_CHOICES,
}
8 changes: 4 additions & 4 deletions tests/validations.test.ts
Expand Up @@ -61,19 +61,19 @@ test("validation_command_with_required_argument_throws_error", function () {
);
});

test("validation_option_choises", function () {
test("validation_option_choices", function () {
const program = new Denomander({ throw_errors: true });
const args = ["choose", "-c", "five"];
const customOption = new Option({
flags: "-c --choise",
flags: "-c --choice",
description: "Choose one of the following",
}).choises(["one", "two", "three"]);
}).choices(["one", "two", "three"]);

assertThrows(
() => {
program.command("choose").addOption(customOption).parse(args);
},
Error,
program.errors.OPTION_CHOISE,
program.errors.OPTION_CHOICE,
);
});

0 comments on commit c102d0f

Please sign in to comment.