75lb / command-line-args

A mature, feature-complete library to parse command-line options.
MIT License
679 stars 107 forks source link

New feature: option to retain inputs #135

Open zanerock opened 6 days ago

zanerock commented 6 days ago

Because inputs are transformed, the original user input can get lost. It can be useful to retain the user input for multiple reasons.

One example in particular, a companion validation library might, at the very least, wish to reflect the invalid value back to the user. E.g., "Option '--foo' value 'a' is not a valid number." With more sophisticated processing, there might be different error reports based on the original input. E.g., if we had option def { name: 'num', type: (val) => parseInt(val) } then both --num '' and --num ab would fail, but we might respond "Option 'num' is required." in the first instance and "Option 'num' value 'ab' is must be an integer." Even worse, --num 1.2 would result in a value of 1. In this case, it's necessary to inspect the original input to know that it's invalid input.

So, we add retainInput to commandLineArgs. When true, an _inputs field is included in the result. The result is a mapping between the option name and the input or inputs (where multiple: true). _inputs respets camelCase.

75lb commented 5 days ago

Code you do me a favour please and paste some reproduction code I can run to quickly demonstrate the issue. Cheers!