Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Option Constructor using config file #1642

Closed
wants to merge 3 commits into from
Closed

New Option Constructor using config file #1642

wants to merge 3 commits into from

Conversation

the-unfactoring-guru
Copy link

Pull Request

Problem

Adding a new form of creating an option object. This is to simplify work when creating multiple options

Solution

Use option object to specify the new option with its fields. Later on we can implement a function that can create multiple options from one config object array `[ config1, config2, config3] and save time when creating a new program option.

index.d.ts

export class Option {
//...
    constructor(config: { flags: string; description?: string });
//...
}

option.js

constructor(config) { // Config object that contains flags and description?
    this.flags = config['flags']; //Adding flags
    this.description = config['description'] || ''; //Adding description

    this.required = flags.includes('<'); // A value must be supplied when the option is specified.
    this.optional = flags.includes('['); // A value is optional when the option is specified.
    // variadic test ignores <value,...> et al which might be used to describe custom splitting of single argument
    this.variadic = /\w\.\.\.[>\]]$/.test(flags); // The option can take multiple values.
    this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
    const optionFlags = splitOptionFlags(flags);
    this.short = optionFlags.shortFlag;
    this.long = optionFlags.longFlag;
    this.negate = false;
    if (this.long) {
      this.negate = this.long.startsWith('--no-');
    }
    this.defaultValue = undefined;
    this.defaultValueDescription = undefined;
    this.envVar = undefined;
    this.parseArg = undefined;
    this.hidden = false;
    this.argChoices = undefined;
  }

ChangeLog

@shadowspawn
Copy link
Collaborator

Related: #1359

@shadowspawn
Copy link
Collaborator

  1. This is not valid JavaScript! Maybe you modified the existing constructor when you were playing about, and then added it to the original without testing again.

  2. Some code formatter has modified lots of the source code, changing quote marks, reformatting et al.

Already closed by OP.

@the-unfactoring-guru
Copy link
Author

Okay thank you, I will try to make corrections and see what I did wrong, sorry for the formatting, forgot it was on in my editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants