简体   繁体   English

yargs 参数在 linux 中未正确绑定

[英]yargs argument not binding correctly in linux

I am trying to run a node application which uses yargs to read arguments.我正在尝试运行一个使用 yargs 读取 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.我正在使用 shell 脚本在 Amazon linux 上调用此命令。 The shell script looks like this. shell 脚本如下所示。 Filename command1.sh文件名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.使用./command1.sh 调用此 shell 文件应设置两个环境变量,然后应使用 command1 作为命令参数的输入调用 RunCommand class。

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.如果我尝试直接从终端调用 class 则没有问题。 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.当我打印命令的名称时,控制台日志中的句号正在替换“C”。

There was line termination issue.存在线路终止问题。 "\r" was getting appended at the end. "\r" 在最后被附加。

Fixed it using sed -i -e 's/\r$//' command1.sh使用sed -i -e 's/\r$//' command1.sh修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM