Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shebang 无法传递参数问题 #131

Closed
RaoHai opened this issue Sep 20, 2024 · 4 comments
Closed

Shebang 无法传递参数问题 #131

RaoHai opened this issue Sep 20, 2024 · 4 comments

Comments

@RaoHai
Copy link

RaoHai commented Sep 20, 2024

作者你好,我更新了最新版本的 fy 后遇到了 shebang 无法传递参数的问题,输出如下:

❯ fy hello
/usr/bin/env: ‘node --no-deprecation’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

我修改了 /usr/lib/node_modules/fanyi/bin/fanyi.js 的 shebang,去除了 --no-deprecation 参数,可以正常运行但每次都会输出模块弃用 warning 导致排版混乱。

似乎 shebang 中的 env 无法正确传递参数,或许可以修改为其他传递参数的方式或者禁用 warning?

平台:Arch Linux,Linux 6.10.10,Node 22.9.0

Copy link

maige-app bot commented Sep 20, 2024

你好,感谢你提交这个问题。看起来这是一个与 shebang 传递参数相关的问题。你可以尝试以下解决方案之一:

  1. 修改 shebang 行,使用 node 而不是 env 来调用 Node.js。例如:#!/usr/bin/node --no-deprecation
  2. 使用 env-S 选项来传递参数。例如:#!/usr/bin/env -S node --no-deprecation

请尝试这些方法,看看是否能解决你的问题。如果问题仍然存在,请告诉我们。

From Maige. How's my driving?

Copy link
Contributor

gitauto-ai bot commented Sep 20, 2024

Click the checkbox below to generate a PR!

  • Generate PR

@RaoHai, You have 5 requests left in this cycle which refreshes on 2024-10-06 01:47:00+00:00.
If you have any questions or concerns, please contact us at info@gitauto.ai.

Copy link

你好,感谢你反馈关于 shebang 无法传递参数的问题。

这个问题已经在 Issue #128PR #129 中被讨论和处理。你可以参考这些讨论和修复方案来解决你的问题。

如果你有更多问题或需要进一步的帮助,请随时 @ 我。谢谢!

@RaoHai RaoHai closed this as completed Sep 20, 2024
Copy link

dosubot bot commented Sep 20, 2024

我发现了一个类似的已关闭问题 使用报错,该问题通过一个 pull request 解决:fix: ignore punycode module is deprecated warning in node 22 [1]

要解决 shebang 无法传递参数的问题,可以修改 /usr/lib/node_modules/fanyi/bin/fanyi.js 的 shebang 行如下:

#!/usr/bin/env -S node --no-warnings --no-deprecation

这个 shebang 行包含了 --no-warnings--no-deprecation 标志,以禁用警告和弃用消息。

以下是修改后的文件内容:

#!/usr/bin/env -S node --no-warnings --no-deprecation

const { Command } = require('commander');
const chalk = require('chalk');
const updateNotifier = require('update-notifier');
const pkg = require('../package.json');
const config = require('../lib/config');
const { searchList } = require('../lib/searchHistory');

updateNotifier({ pkg }).notify();
const program = new Command();

program
  .name(pkg.name)
  .description(pkg.description)
  .version(pkg.version)
  .action(() => {
    // 如果输入是 "fanyi",没有参数,则忽略
    if (process.argv.length > 2) {
      return runFY();
    }
  });

program
  .command('config')
  .description('设置全局选项')
  .addCommand(
    new Command('list').description('查看配置项').action(async () => {
      const options = await config.load();
      console.log(`${chalk.gray(config.getConfigPath())}`);
      console.log();
      for (const [key, value] of Object.entries(options)) {
        console.log(`${chalk.cyan(key)}: ${chalk.yellow(value)}`);
      }
    }),
  )
  .addCommand(
    new Command('set')
      .description('设置配置项')
      .argument('<key>', '配置项键名')
      .argument('<value>', '配置项值')
      .action(async (key, value) => {
        const options = {};
        if (key === 'GROQ_API_KEY') {
          options[key] = value;
        } else {
          options[key] = value === 'true' ? true : value === 'false' ? false : value;
        }
        await config.write(options);
      }),
  );

program
  .command('list')
  .option('-d, --someDay <char>', '查看指定某天的查询记录')
  .option('-r, --recentDays [number]', '查看最近几天内的数据', 0)
  .option('-all --show-file [boolean]', '查看全部数据,即单词存放的位置', false)
  .action((args) => {
    searchList(args);
  });

program.on('--help', () => {
  console.log('');
  console.log(chalk.gray('示例:'));
  console.log(`${chalk.cyan('  $ ')}fanyi word`);
  console.log(`${chalk.cyan('  $ ')}fanyi world peace`);
  console.log(`${chalk.cyan('  $ ')}fanyi chinglish`);
  console.log(`${chalk.cyan('  $ ')}fanyi config set color true`);
  console.log(`${chalk.cyan('  $ ')}fanyi config set iciba true`);
  console.log(`${chalk.cyan('  $ ')}fanyi config set groq true`);
  console.log(`${chalk.cyan('  $ ')}fanyi config set GROQ_API_KEY your_api_key_here`);
  console.log(`${chalk.cyan('  $ ')}fanyi config list`);
  console.log('');
});

program.parse(process.argv);
if (!process.argv.slice(2).length) {
  program.help();
}

async function runFY(options = {}) {
  const defaultOptions = await config.load();
  const mergedOptions = { ...defaultOptions, ...options };
  const fanyi = require('..');
  fanyi(program.args.join(' '), mergedOptions);
}

这样可以解决参数传递问题并禁用警告和弃用消息 [2].

To continue talking to Dosu, mention @dosu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant