Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
52c3a2e
Proof of concept
shadowspawn May 14, 2022
5ec71b1
Return originalArgs and indices
shadowspawn May 14, 2022
f711187
Change short in AST to boolean
shadowspawn May 24, 2022
cce90bb
Store inlineValue rather than valueIndex
shadowspawn May 24, 2022
e4bc5fe
Rename symbol property in AST to kind
shadowspawn May 24, 2022
87624a1
Rename returned ast property to parseElements
shadowspawn May 24, 2022
2925639
Refactor to use parseElements to test for errors and store values
shadowspawn May 29, 2022
412287e
Merge remote-tracking branch 'upstream/main' into feature/ast
shadowspawn May 29, 2022
1897dac
Build positionals from parseElements
shadowspawn May 29, 2022
45d12e7
Replace .push with primordial
shadowspawn May 29, 2022
1d19fb2
forEach replaced with primordial
shadowspawn May 29, 2022
9d1267d
Swap isShort for optionUsed
shadowspawn May 30, 2022
c9c4d4c
Rework checkOptionLikeValue
shadowspawn May 30, 2022
ed4168d
Consistent property order and renames
shadowspawn May 30, 2022
16a9f51
Comment out new returned property until naming and tests ready
shadowspawn May 30, 2022
915e6c3
Refactor tokenize into own function
shadowspawn May 31, 2022
641197e
First test pass, get tests working by ignoring tokens in results
shadowspawn May 31, 2022
18470bd
Less magical check now checking kind
shadowspawn May 31, 2022
38d1d6c
Add some token tests
shadowspawn May 31, 2022
cfbd436
Fix index after space-separated option value
shadowspawn May 31, 2022
397dcf1
Make tokens opt-in
shadowspawn Jun 1, 2022
938bdb0
Simplify more tests with opt-in details
shadowspawn Jun 1, 2022
c5da345
Rename token.optionName to name, and restore longOption/shortOption …
shadowspawn Jun 1, 2022
4e1ec2d
Add example
shadowspawn Jun 1, 2022
bd3f574
Add side-note
shadowspawn Jun 2, 2022
b703ee8
Add example of #52, limit long syntax
shadowspawn Jun 2, 2022
82339f9
Add ordered example
shadowspawn Jun 2, 2022
fab1dc4
Add example for no repeated options
shadowspawn Jun 2, 2022
99165c9
Comment wording
shadowspawn Jun 2, 2022
9bd039d
Switch from details to tokens for configuration
shadowspawn Jun 3, 2022
6a3f637
Simplify example by removing "library" support
shadowspawn Jun 3, 2022
4cfbc29
Remove comments on how well-advised the examples goals are
shadowspawn Jun 3, 2022
9a9a740
Switch from optionUser to rawName
shadowspawn Jun 3, 2022
7cd685c
Merge remote-tracking branch 'upstream/main' into feature/ast
shadowspawn Jun 3, 2022
6f93632
Use modern syntax
shadowspawn Jun 4, 2022
642d8c0
Validate new input property
shadowspawn Jun 4, 2022
e70609a
Move strict check outside check routines
shadowspawn Jun 4, 2022
fdaa553
Tidy object setup
shadowspawn Jun 4, 2022
c293307
Simplify test
shadowspawn Jun 4, 2022
041459d
Indentation and match filename
shadowspawn Jun 4, 2022
7aafaf6
Rework with strict:true
shadowspawn Jun 4, 2022
1b6e585
Longer but simpler
shadowspawn Jun 4, 2022
20eacb0
Add textual description, and some fixes
shadowspawn Jun 4, 2022
81239d6
Add example output for tokens
shadowspawn Jun 4, 2022
84dde8e
Add example used in new documentation
shadowspawn Jun 4, 2022
40a6201
Refactor documentation
shadowspawn Jun 4, 2022
07fbb91
Update .editorconfig from Node.js
shadowspawn Jun 4, 2022
35c16f1
rework token property descriptions
shadowspawn Jun 4, 2022
038480a
Minor refactor of remaining arg processing
shadowspawn Jun 7, 2022
d4f96cc
Replace switch with if/else
shadowspawn Jun 14, 2022
122727e
Remove side-affect, per feedback
shadowspawn Jun 15, 2022
5b3518d
Add tricky case of short option group to token expansion
shadowspawn Jun 15, 2022
c8c2f84
Rework description of token.index after token example.
shadowspawn Jun 15, 2022
2b119b0
Be less clever with script naming in example
shadowspawn Jun 15, 2022
1b3c076
Remove superfluous colons in docs
shadowspawn Jun 17, 2022
e9e30a7
Upstream lint
shadowspawn Jun 17, 2022
46ab295
Merge branch 'main' into feature/ast
shadowspawn Jun 23, 2022
01baee5
Merge branch 'main' into feature/ast
shadowspawn Jun 24, 2022
46903af
Improve description
shadowspawn Jul 4, 2022
9b74c05
Merge branch 'feature/ast' of github.com:shadowspawn/parseargs into f…
shadowspawn Jul 4, 2022
89e4532
Use negate as example for tokens. Rework description a little.
shadowspawn Jul 4, 2022
3914949
Lint and feedback
shadowspawn Jul 4, 2022
d8126d1
Expand small token tests and remove uber tests
shadowspawn Jul 5, 2022
87bab7b
Use kEmptyObject per upstream suggestion. Move node lookalikes to int…
shadowspawn Jul 5, 2022
02ea585
Reworks tokens documentation for config
shadowspawn Jul 5, 2022
56fe4ca
Add version changes to YAML.
shadowspawn Jul 5, 2022
722f05e
Update README with upstream changes
shadowspawn Jul 5, 2022
ae9eca4
Update negate example calls to match documentation
shadowspawn Jul 16, 2022
f9dcc4f
Remove extra examples
shadowspawn Jul 16, 2022
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
Prev Previous commit
Next Next commit
Return originalArgs and indices
  • Loading branch information
shadowspawn committed May 14, 2022
commit 5ec71b17ea4a47787df4cd10e7fa0fbf8b40ebf7
41 changes: 24 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,21 @@ const parseArgs = (config = { __proto__: null }) => {

const result = {
values: { __proto__: null },
positionals: []
positionals: [],
};
const ast = [];
result.originalArgs = ArrayPrototypeSlice(args);
let argIndex = -1;
let groupCount = 0;

let remainingArgs = ArrayPrototypeSlice(args);
let originalArgs = ArrayPrototypeSlice(args); // Before splitting groups
while (remainingArgs.length > 0) {
const arg = ArrayPrototypeShift(remainingArgs);
const nextArg = remainingArgs[0];
const originalArg = ArrayPrototypeShift(originalArgs);
if (groupCount > 0)
groupCount--;
else
argIndex++;

// Check if `arg` is an options terminator.
// Guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
Expand All @@ -223,9 +228,9 @@ const parseArgs = (config = { __proto__: null }) => {
result.positionals,
remainingArgs
);
ast.push({ symbol: 'option-terminator' });
ast.push({ symbol: 'option-terminator', argIndex });
remainingArgs.forEach((arg) =>
ast.push({ symbol: 'positional', value: arg }));
ast.push({ symbol: 'positional', value: arg, argIndex: ++argIndex }));
break; // Finished processing args, leave while loop.
}

Expand All @@ -234,25 +239,26 @@ const parseArgs = (config = { __proto__: null }) => {
const shortOption = StringPrototypeCharAt(arg, 1);
const longOption = findLongOptionForShort(shortOption, options);
let optionValue;
let valueIndex;
if (optionsGetOwn(options, longOption, 'type') === 'string' &&
isOptionValue(nextArg)) {
// e.g. '-f', 'bar'
optionValue = ArrayPrototypeShift(remainingArgs);
valueIndex = argIndex + 1;
checkOptionLikeValue(longOption, optionValue, arg, strict);
}
checkOptionUsage(longOption, optionValue, options,
arg, strict, allowPositionals);
storeOption(longOption, optionValue, options, result.values);
ast.push({ symbol: 'option', optionName: longOption,
usage: arg, value: optionValue ?? null, originalArg,
valueSource: optionValue != null ? 'arg' : 'none' });
short: shortOption, argIndex,
value: optionValue, valueIndex });
continue;
}

if (isShortOptionGroup(arg, options)) {
// Expand -fXzy to -f -X -z -y
const expanded = [];
const expandedOriginal = [];
for (let index = 1; index < arg.length; index++) {
const shortOption = StringPrototypeCharAt(arg, index);
const longOption = findLongOptionForShort(shortOption, options);
Expand All @@ -266,10 +272,9 @@ const parseArgs = (config = { __proto__: null }) => {
ArrayPrototypePush(expanded, `-${StringPrototypeSlice(arg, index)}`);
break; // finished short group
}
ArrayPrototypePush(expandedOriginal, originalArg);
}
remainingArgs = ArrayPrototypeConcat(expanded, remainingArgs);
originalArgs = ArrayPrototypeConcat(expandedOriginal, originalArgs);
groupCount = expanded.length;
continue;
}

Expand All @@ -281,27 +286,29 @@ const parseArgs = (config = { __proto__: null }) => {
checkOptionUsage(longOption, optionValue, options, `-${shortOption}`, strict, allowPositionals);
storeOption(longOption, optionValue, options, result.values);
ast.push({ symbol: 'option', optionName: longOption,
usage: `-${shortOption}`, value: optionValue, originalArg,
valueSource: 'embedded' });
short: shortOption, argIndex,
value: optionValue, valueIndex: argIndex });
continue;
}

if (isLoneLongOption(arg)) {
// e.g. '--foo'
const longOption = StringPrototypeSlice(arg, 2);
let optionValue;
let valueIndex;
if (optionsGetOwn(options, longOption, 'type') === 'string' &&
isOptionValue(nextArg)) {
// e.g. '--foo', 'bar'
optionValue = ArrayPrototypeShift(remainingArgs);
valueIndex = argIndex + 1;
checkOptionLikeValue(longOption, optionValue, arg, strict);
}
checkOptionUsage(longOption, optionValue, options,
arg, strict, allowPositionals);
storeOption(longOption, optionValue, options, result.values);
ast.push({ symbol: 'option', optionName: longOption,
value: optionValue ?? null, usage: arg, originalArg,
valueSource: optionValue != null ? 'arg' : 'none' });
short: null, argIndex,
value: optionValue, valueIndex });
continue;
}

Expand All @@ -313,8 +320,8 @@ const parseArgs = (config = { __proto__: null }) => {
checkOptionUsage(longOption, optionValue, options, `--${longOption}`, strict, allowPositionals);
storeOption(longOption, optionValue, options, result.values);
ast.push({ symbol: 'option', optionName: longOption,
value: optionValue, usage: `--${longOption}`, originalArg,
valueSource: 'embedded' });
short: null, argIndex,
value: optionValue, valueIndex: argIndex });
continue;
}

Expand All @@ -324,7 +331,7 @@ const parseArgs = (config = { __proto__: null }) => {
}

ArrayPrototypePush(result.positionals, arg);
ast.push({ symbol: 'positional', value: arg });
ast.push({ symbol: 'positional', value: arg, argIndex });
}

result.ast = ast;
Expand Down