tact icon indicating copy to clipboard operation
tact copied to clipboard

Double dash in CLI doesn't stop argument processing

Open novusnota opened this issue 1 year ago • 2 comments
trafficstars

Problem

For example, consider the following instruction:

npx tact -e -- '-1 + 1'

The expected result would be 0, but instead one gets an exit code 2 and the following:

Unknown flag
-1 + 1

Notes

Did some research, and it's probably related to poorly resolved closed and unresolved stale issues regarding this case in yargs-parser, which meow (our CLI toolkit) uses for parsing arguments. It may be time to consider moving to another CLI toolkit. Commander.js is quite nice and is rather friendly towards #446, see https://github.com/tj/commander.js/issues/2008 (or we'll just duplicate some flags here and there, no biggie)

As a temporary (ha!) workaround one may just put 0 + in front of any expression and remove the --. Like so:

npx tact -e '0 + -1 + 1'

novusnota avatar Jul 16 '24 15:07 novusnota

It may be time to consider moving to another CLI toolkit. Commander.js is quite nice

is there a native typescript cli parser?

anton-trunov avatar Jul 16 '24 15:07 anton-trunov

is there a native typescript cli parser?

No — there's no argument parser in Node.js APIs and TypeScript doesn't add APIs on top. One just gets process.argv and has to start from there :/

What works - works, and it's not a pressing issue considering that leading dashes are much less likely to occur in the context of files than in the context of evaluating expressions after -e/--expr. So the 0 + ... workaround is good enough for a while

novusnota avatar Jul 16 '24 16:07 novusnota

The double dash signifies the end of command-line options and the start of positional arguments. So, it does not makes sense for the example in the OP: tact -e -- '-1 + 1'.

But, it does make sense for the following example:

$ touch -- --config.tact
$ tact -- --config.tact

cc @verytactical

anton-trunov avatar Jan 23 '25 07:01 anton-trunov