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

Fixed: Add retain inputs #136

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [4, 6, 8, 10, 12, 14, 16, 17]
node-version: [^14.15.x, ^16.10.x, 17, 18, 20, 22]

steps:
- uses: actions/checkout@v2
Expand Down
31 changes: 28 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ class ArgvParser {
}

const _value = new WeakMap();
const _inputs = new WeakMap();

/**
* Encapsulates behaviour (defined by an OptionDefinition) when setting values
Expand All @@ -1178,6 +1179,10 @@ class Option {
return _value.get(this)
}

getInput () {
return _inputs.get(this)
}

set (val) {
this._set(val, 'set');
}
Expand All @@ -1189,6 +1194,10 @@ class Option {
if (val !== null && val !== undefined) {
const arr = this.get();
if (this.state === 'default') arr.length = 0;
else {
const inputs = this.getInput();
inputs.push(this instanceof FlagOption ? null : val);
}
arr.push(def.type(val));
this.state = state;
}
Expand All @@ -1202,12 +1211,14 @@ class Option {
throw err
} else if (val === null || val === undefined) {
_value.set(this, val);
_inputs.set(this, val);
// /* required to make 'partial: defaultOption with value equal to defaultValue 2' pass */
// if (!(def.defaultOption && !def.isMultiple())) {
// this.state = state
// }
} else {
_value.set(this, def.type(val));
_inputs.set(this, this instanceof FlagOption ? null : val);
this.state = state;
}
}
Expand All @@ -1227,6 +1238,11 @@ class Option {
_value.set(this, null);
}
}
if (this.definition.isMultiple()) {
_inputs.set(this, []);
} else {
_inputs.set(this, null);
}
this.state = 'default';
}

Expand Down Expand Up @@ -1263,6 +1279,7 @@ class Output extends Map {

/* by default, an Output has an `_unknown` property and any options with defaultValues */
this.set('_unknown', Option.create({ name: '_unknown', multiple: true }));
this.set('_inputs', Option.create({ name: '_inputs', defaultValue: {}, type: (val) => val }));
for (const def of this.definitions.whereDefaultValueSet()) {
this.set(def.name, Option.create(def));
}
Expand All @@ -1272,27 +1289,34 @@ class Output extends Map {
options = options || {};
const output = {};
for (const item of this) {
const name = options.camelCase && item[0] !== '_unknown' ? camelCase(item[0]) : item[0];
const name = options.camelCase && item[0] !== '_unknown' && item[0] !== '_inputs' ? camelCase(item[0]) : item[0];
const option = item[1];
if (name === '_unknown' && !option.get().length) continue
output[name] = option.get();
if (options.skipInputs !== true && name !== '_unknown' && name !== '_inputs')
output._inputs[name] = option.getInput();
}

if (options.skipInputs) delete output._inputs;
if (options.skipUnknown) delete output._unknown;

return output
}
}

class GroupedOutput extends Output {
toObject (options) {
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown });
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown, skipInputs: true });
const superOutput = super.toObject(options);
const unknown = superOutput._unknown;
delete superOutput._unknown;
const inputs = superOutput._inputs;
delete superOutput._inputs;
const grouped = {
_all: superOutput
};
if (unknown && unknown.length) grouped._unknown = unknown;
if (inputs && Object.keys(inputs).length) grouped._inputs = inputs;

this.definitions.whereGrouped().forEach(def => {
const name = options.camelCase ? camelCase(def.name) : def.name;
Expand Down Expand Up @@ -1331,6 +1355,7 @@ class GroupedOutput extends Output {
* @param {string[]} [options.argv] - An array of strings which, if present will be parsed instead of `process.argv`.
* @param {boolean} [options.partial] - If `true`, an array of unknown arguments is returned in the `_unknown` property of the output.
* @param {boolean} [options.stopAtFirstUnknown] - If `true`, parsing will stop at the first unknown argument and the remaining arguments returned in `_unknown`. When set, `partial: true` is also implied.
* @param {boolean} [options.retainInputs] - If `true`, then will retain the original string inputs in `_inputs`. These names are also affected by the `camelCase` option.
* @param {boolean} [options.camelCase] - If `true`, options with hypenated names (e.g. `move-to`) will be returned in camel-case (e.g. `moveTo`).
* @param {boolean} [options.caseInsensitive] - If `true`, the case of each option name or alias parsed is insignificant. In other words, both `--Verbose` and `--verbose`, `-V` and `-v` would be equivalent. Defaults to false.
* @returns {object}
Expand Down Expand Up @@ -1393,7 +1418,7 @@ function commandLineArgs (optionDefinitions, options) {
}
}

return output.toObject({ skipUnknown: !options.partial, camelCase: options.camelCase })
return output.toObject({ skipUnknown: !options.partial, camelCase: options.camelCase, skipInputs: !options.retainInputs })
}

module.exports = commandLineArgs;
31 changes: 28 additions & 3 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ class ArgvParser {
}

const _value = new WeakMap();
const _inputs = new WeakMap();

/**
* Encapsulates behaviour (defined by an OptionDefinition) when setting values
Expand All @@ -1174,6 +1175,10 @@ class Option {
return _value.get(this)
}

getInput () {
return _inputs.get(this)
}

set (val) {
this._set(val, 'set');
}
Expand All @@ -1185,6 +1190,10 @@ class Option {
if (val !== null && val !== undefined) {
const arr = this.get();
if (this.state === 'default') arr.length = 0;
else {
const inputs = this.getInput();
inputs.push(this instanceof FlagOption ? null : val);
}
arr.push(def.type(val));
this.state = state;
}
Expand All @@ -1198,12 +1207,14 @@ class Option {
throw err
} else if (val === null || val === undefined) {
_value.set(this, val);
_inputs.set(this, val);
// /* required to make 'partial: defaultOption with value equal to defaultValue 2' pass */
// if (!(def.defaultOption && !def.isMultiple())) {
// this.state = state
// }
} else {
_value.set(this, def.type(val));
_inputs.set(this, this instanceof FlagOption ? null : val);
this.state = state;
}
}
Expand All @@ -1223,6 +1234,11 @@ class Option {
_value.set(this, null);
}
}
if (this.definition.isMultiple()) {
_inputs.set(this, []);
} else {
_inputs.set(this, null);
}
this.state = 'default';
}

Expand Down Expand Up @@ -1259,6 +1275,7 @@ class Output extends Map {

/* by default, an Output has an `_unknown` property and any options with defaultValues */
this.set('_unknown', Option.create({ name: '_unknown', multiple: true }));
this.set('_inputs', Option.create({ name: '_inputs', defaultValue: {}, type: (val) => val }));
for (const def of this.definitions.whereDefaultValueSet()) {
this.set(def.name, Option.create(def));
}
Expand All @@ -1268,27 +1285,34 @@ class Output extends Map {
options = options || {};
const output = {};
for (const item of this) {
const name = options.camelCase && item[0] !== '_unknown' ? camelCase(item[0]) : item[0];
const name = options.camelCase && item[0] !== '_unknown' && item[0] !== '_inputs' ? camelCase(item[0]) : item[0];
const option = item[1];
if (name === '_unknown' && !option.get().length) continue
output[name] = option.get();
if (options.skipInputs !== true && name !== '_unknown' && name !== '_inputs')
output._inputs[name] = option.getInput();
}

if (options.skipInputs) delete output._inputs;
if (options.skipUnknown) delete output._unknown;

return output
}
}

class GroupedOutput extends Output {
toObject (options) {
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown });
const superOutputNoCamel = super.toObject({ skipUnknown: options.skipUnknown, skipInputs: true });
const superOutput = super.toObject(options);
const unknown = superOutput._unknown;
delete superOutput._unknown;
const inputs = superOutput._inputs;
delete superOutput._inputs;
const grouped = {
_all: superOutput
};
if (unknown && unknown.length) grouped._unknown = unknown;
if (inputs && Object.keys(inputs).length) grouped._inputs = inputs;

this.definitions.whereGrouped().forEach(def => {
const name = options.camelCase ? camelCase(def.name) : def.name;
Expand Down Expand Up @@ -1327,6 +1351,7 @@ class GroupedOutput extends Output {
* @param {string[]} [options.argv] - An array of strings which, if present will be parsed instead of `process.argv`.
* @param {boolean} [options.partial] - If `true`, an array of unknown arguments is returned in the `_unknown` property of the output.
* @param {boolean} [options.stopAtFirstUnknown] - If `true`, parsing will stop at the first unknown argument and the remaining arguments returned in `_unknown`. When set, `partial: true` is also implied.
* @param {boolean} [options.retainInputs] - If `true`, then will retain the original string inputs in `_inputs`. These names are also affected by the `camelCase` option.
* @param {boolean} [options.camelCase] - If `true`, options with hypenated names (e.g. `move-to`) will be returned in camel-case (e.g. `moveTo`).
* @param {boolean} [options.caseInsensitive] - If `true`, the case of each option name or alias parsed is insignificant. In other words, both `--Verbose` and `--verbose`, `-V` and `-v` would be equivalent. Defaults to false.
* @returns {object}
Expand Down Expand Up @@ -1389,7 +1414,7 @@ function commandLineArgs (optionDefinitions, options) {
}
}

return output.toObject({ skipUnknown: !options.partial, camelCase: options.camelCase })
return output.toObject({ skipUnknown: !options.partial, camelCase: options.camelCase, skipInputs: !options.retainInputs })
}

export default commandLineArgs;
Loading
Loading