node-dashdash icon indicating copy to clipboard operation
node-dashdash copied to clipboard

Bash completion error with multiple dashes in subcommands

Open r-didato opened this issue 1 year ago • 0 comments

Describe the bug

The bash completion script doesn't suggest short and long options when using a subcommand containing more than one dash.

To Reproduce

Create a simple node program

var dashdash = require('dashdash');

var specExtra='
local cmd_subcmds="my-test-subcommand"
local cmd_allsubcmds="my-test-subcommand"

local cmd__my_test_subcommand_shortopts="-f"
local cmd__my_test_subcommand_longopts="--file"
local cmd__my_test_subcommand_optargs=""
';

var code = dashdash.bashCompletionFromOptions({
    name: 'mycli',
    options: [],
    specExtra: specExtra
});

fs.writeFileSync( 'bashCompletionTest', code );

This generates a bash completion file 'bashCompletionTest', this file should include the following lines that define a 'my-test-subcommand' subcommand with options -f and --file

local cmd_subcmds="my-test-subcommand"
local cmd_allsubcmds="my-test-subcommand"

local cmd__my_test_subcommand_shortopts="-f"
local cmd__my_test_subcommand_longopts="--file"
local cmd__my_test_subcommand_optargs=""

Just source the file we just created with

source bashCompletionTest

Expected behavior

Tabbing on 'mycli my-test-subcommand -' i should see -f and --file as options, instead i don't see any possible completion

Additional context

I checked the logs and i found out that the completion script doesn't replace correctly all dashes and changes only the first one, so it searches for options of command my_test-subcommand (to be more precise checks variable cmd__my_test-subcommand_shortopts)

Possible fix

To fix just replace all instances of '-' instead of the first one only.

At line 196 of etc/dashdash.bash_completion.in :

- _dashdash_complete $(( $i + 1 )) "${context}__${arg/-/_}"
+ _dashdash_complete $(( $i + 1 )) "${context}__${arg//-/_}"

r-didato avatar Feb 27 '23 13:02 r-didato