Search code examples
javascriptlinuxyargs

yargs argument not binding correctly in linux


I am trying to run a node application which uses yargs to read arguments. Following is the code for that.

export class RunCommand {
    public async run(): Promise<string> {
        let commandMap: { [key: string]: any; } = {}
        commandMap['command1'] = new Command1();
        commandMap['command2'] = new Command2();

        const argv = yargs.argv;
        let command = commandMap[argv.command];

        if ( command != null) {
            await command.execute();
        } else {
            if ( argv.command != null ) {
                console.log('Command not found:' + argv.command + '.');
            }
            console.log('Available Commands');
            for(let key in commandMap) {
                console.log(commandMap[key].getCommandName());
            }
        }

        return 'Success: ' + argv.command;
    }
}

let runCommand: RunCommand = new RunCommand();
runCommand.run();

I am invoking this command on Amazon linux using a shell script. The shell script looks like this. Filename command1.sh

export PARAM1=VALUE1
export PARAM2=VALUE2
node RunCommand.js --command command1

Invoking this shell file using ./command1.sh should set the two environment variables and then should invoke the RunCommand class with command1 as input to command param.

The terminal is printing following out from console logs.

.ommand not found: command1
Available Commands
command1
command2

If I try to invoke the class directly from the terminal then there is no issue. The respecting command in invoked without any problem.

What could be the issue here? The fullstop in the console log is replacing 'C' when I am printing the name of the command.


Solution

  • There was line termination issue. "\r" was getting appended at the end.

    Fixed it using sed -i -e 's/\r$//' command1.sh